Ersetzen Sie Wörter im Korpus entsprechend dem Wörterbuchdatenrahmen
Ich bin daran interessiert, alle Wörter in a zu ersetzentm
Korpusobjekt nach einem Wörterbuch aus einem zweispaltigen Datenrahmen, wobei die erste Spalte das abzugleichende Wort und die zweite Spalte das Ersatzwort ist.
Ich stecke fest mit demtranslate
Funktion. ich sahdiese Antwort aber ich kann es nicht in eine Funktion umwandeln, die übergeben werden solltm_map
.
Bitte beachten Sie die folgenden MWE
library(tm)
docs <- c("first text", "second text")
corp <- Corpus(VectorSource(docs))
dictionary <- data.frame(word = c('first', 'second', 'text'),
translation = c('primo', 'secondo', 'testo'))
translate <- function(text, dictionary) {
# Would like to replace each word of text with corresponding word in dictionary
}
corp_translated <- tm_map (corp, translate)
inspect(corp_translated)
# Expected result
# A corpus with 2 text documents
#
# The metadata consists of 2 tag-value pairs and a data frame
# Available tags are:
# create_date creator
# Available variables in the data frame are:
# MetaID
# [[1]]
# primo testo
# [[2]]
# secondo testo