2016-09-12 2 views
9

Ich möchte einige Metadaten an die kwic-Ausgabe anhängen, wie zB eine Kunden-ID (siehe unten), so dass es einfach ist, nach einer Master-Datei zu suchen. Ich habe versucht, Daten mit Cbind anhängen, aber nichts stimmt nicht überein.Quanteda kwic fügt Daten an die Ausgabe an

Wenn dies möglich ist, würden Beispiele sehr geschätzt werden.

 docname position contextPre  keyword contextPost   CustID 
    text3790  5 nothing at all looks good and sounds great   1 
    text3801 11 think the offer is a good value and has a lot  3 
    text3874 10 not so sure thats a good word to use    5 

Ursprung data.frame

 CustID Comment 
     1  nothing at all looks good and sounds great 
     2  did not see anything that was very appealing 
     3  I think the offer is a good value and has a lot of potential 
     4  these items look terrible how are you still in business 
     5  not so sure thats a good word to use 
     6  having a hard time believing some place would sell an item so low 
     7  it may be worth investing in some additional equipment 

Antwort

5

Zuerst dachte ich, ist die ideale Lösung docvars verwenden , aber kwic scheint keine Option zu haben, sie zu zeigen. Ich muss noch eine ID-Doc Mapping-Tabelle mit dem kwic Ergebnis zusammenführen.

library(data.table) 
library(quanteda) 

s <- "CustID, Comment 
1,  nothing at all looks good and sounds great 
2,  did not see anything that was very appealing 
3,  I think the offer is a good value and has a lot of potential 
4,  these items look terrible how are you still in business 
5,  not so sure thats a good word to use 
6,  having a hard time believing some place would sell an item so low 
7,  it may be worth investing in some additional equipment" 

# I'm using data.table mainly to read the data easily. 
dt <- fread(s, data.table=FALSE) 

# all operations below apply to data frame 
myCorpus <- corpus(df$Comment) 
# the Corpus and CustID came from same data frame, 
# thus ensured the mapping is correct 
docvars(myCorpus, "CustID") <- df$CustID 
summary(myCorpus) 
# build the mapping table of docname and CustID. 
# The docname is in row.names, have to make an 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, by = "docname") 

Ergebnis:

> id_result 
    docname position contextPre keyword  contextPost CustID 
1 text1  5 at all looks good and sounds great  1 
2 text3  7 offer is a good value and has   3 
3 text5  6 sure thats a good word to use   5 
+0

Danke dafür! Ich kam langsam zu der Erkenntnis, dass eine Art Merge oder Join erforderlich sein würde. Es wäre ein nettes Feature in KWIC, wenn Sie auch andere Spalten aus dem ursprünglichen Datenrahmen als Teil der Ausgabe hinzufügen könnten. Prost. – Atwp67

+0

KWIC wird wahrscheinlich andere Spalten im ursprünglichen Datenrahmen nicht kennen, weil es am Korpus arbeitet, und Corpus wurde entworfen, um von anderen Arten von Daten anstelle von Datenrahmen zu lesen. Wir können Spalten zu "docvar" hinzufügen, auf die KWIC zugreifen kann, wenn der Autor dies wünscht. Obwohl der Paketautor sie nur für Plot oder Berichte über Corpus benutzte. – dracodoc

+1

danke @Pierre Lafortune, kannte die Option 'data.table = FALSE' vorher nicht! – dracodoc

1

Es ist ein data.frame Objekt, so dass Sie Spalten die reguläre Art und Weise hinzufügen:

library(quanteda) 
h <- head(kwic(inaugTexts, "secure*", window = 3, valuetype = "glob")) 

#Add new ID column 
h$CustID <- 1:nrow(h) 
+0

Danke für das Feedback. Hinzufügen einer neuen Spalte funktioniert, aber letztlich bin ich nach dem Zuweisen der richtigen Kundennummer zu ihrer Aussage (n). – Atwp67

+0

Sie können das auch tun. Warum hast du das ID-Mapping nicht in der Frage angegeben? –

+0

Das ist was ich frage, wie man diese Teile des Puzzles zusammenfügt. – Atwp67

Verwandte Themen