2016-05-01 7 views
6

Ich mache einige Text Mining (PCA, HC, K-Means) und bis jetzt habe ich es geschafft, alles richtig zu kodieren. Es gibt jedoch einen kleinen Fehler, den ich gerne beheben würde.Stämmen Wörter mit tm-Paket in R funktioniert nicht richtig?

Wenn ich versuche, mein Korpus zu stemmen, funktioniert es nicht richtig, da es verschiedene Wörter mit demselben Radikal gibt, die nicht auf die richtige Weise identifiziert werden. Dies sind die Worte, die ich bin besonders daran interessiert, (es ist in Spanisch und sie bedeuten „Kinder“ oder ähnlichen):

niñera, niños, niñas, niña, niño 

Aber wenn ich den Code ausführen ich, dass diese Worte immer noch die gleichen sind, außer für

niña, niño --> niñ 

Aber die anderen bleiben gleich, so dass ich am Ende nur für niña/niño stamme, aber nicht für die anderen.

Dies ist mein Code für die Erstellung des Korpus:

corp <- Corpus(DataframeSource(data.frame(x$service_name))) 
docs <- tm_map(corp, removePunctuation) 
docs <- tm_map(docs, removeNumbers) 
docs <- tm_map(docs, tolower) 
docs <- tm_map(docs, removeWords, stopwords("spanish")) 
docs <- tm_map(docs, stemDocument, language = "spanish") 
docs <- tm_map(docs, PlainTextDocument) 
dtm <- DocumentTermMatrix(docs) 
dtm 

Ich würde wirklich schätzen einige Vorschläge! Vielen Dank

Antwort

15

Es scheint, dass die Stemming-Transformation nur auf PlainTextDocument-Typen angewendet werden kann. Siehe ? stemDocument.

sp.corpus = Corpus(VectorSource(c("la niñera. los niños. las niñas. la niña. el niño."))) 
docs <- tm_map(sp.corpus, removePunctuation) 
docs <- tm_map(docs, removeNumbers) 
docs <- tm_map(docs, tolower) 
docs <- tm_map(docs, removeWords, stopwords("spanish")) 
docs <- tm_map(docs, PlainTextDocument) # needs to come before stemming 
docs <- tm_map(docs, stemDocument, "spanish") 
print(docs[[1]]$content) 

# " niñer niñ niñ niñ niñ" 

gegen

# WRONG 
sp.corpus = Corpus(VectorSource(c("la niñera. los niños. las niñas. la niña. el niño."))) 
docs <- tm_map(sp.corpus, removePunctuation) 
docs <- tm_map(docs, removeNumbers) 
docs <- tm_map(docs, tolower) 
docs <- tm_map(docs, removeWords, stopwords("spanish")) 
docs <- tm_map(docs, stemDocument, "spanish") # WRONG: apply PlainTextDocument first 
docs <- tm_map(docs, PlainTextDocument) 
print(docs[[1]]$content) 

# " niñera niños niñas niña niñ" 

Meiner Meinung nach ist dieses Detail ist nicht offensichtlich, und es wäre schön, zumindest eine Warnung zu erhalten, wenn stemDocument auf einem Nicht-PlainTextDocument aufgerufen wird.

1

Ich wechselte von

corpus <- tm_map(corpus, tolower) 

zu

corpus <- tm_map(corpus, content_transformer(tolower)) 

und dann stemDocument gearbeitet.