r - Quanteda kwic append data to output -
i'd append metadata kwic output such customer id (see below) it's easy lookup against master file. i've tried appending data using cbind nothing matches correctly.
if possible examples appreciated.
docname position contextpre keyword contextpost custid text3790 5 nothing @ looks , sounds great 1 text3801 11 think offer value , has lot 3 text3874 10 not sure thats word use 5
originating data.frame
custid comment 1 nothing @ looks , sounds great 2 did not see appealing 3 think offer value , has lot of potential 4 these items terrible how still in business 5 not sure thats word use 6 having hard time believing place sell item low 7 may worth investing in additional equipment
at first thought ideal solution use docvars
, kwic don't seem have option show them. still need merge id-doc mapping table kwic result.
library(data.table) library(quanteda) s <- "custid, comment 1, nothing @ looks , sounds great 2, did not see appealing 3, think offer value , has lot of potential 4, these items terrible how still in business 5, not sure thats word use 6, having hard time believing place sell item low 7, may worth investing in additional equipment" # i'm using data.table read data easily. dt <- fread(s, data.table=false) # operations below apply data frame mycorpus <- corpus(df$comment) # corpus , custid came same data frame, # ensured mapping correct docvars(mycorpus, "custid") <- df$custid summary(mycorpus) # build mapping table of docname , custid. # docname in row.names, have make explicit column dv_table <- docvars(mycorpus) id_table <- data.frame(docname = row.names(dv_table), custid = dv_table$custid) result <- kwic(mycorpus, "good", window = 3, valuetype = "glob") id_result <- merge(result, id_table, = "docname")
result:
> id_result docname position contextpre keyword contextpost custid 1 text1 5 @ looks , sounds great 1 2 text3 7 offer value , has 3 3 text5 6 sure thats word use 5
Comments
Post a Comment