I am interested in replacing all words in a tm
Corpus object according to a dictionary made of a two columns data frame, where the first column is the word to be matched and the second column is the replacement word.
I am stuck with the translate
function. I saw this answer but I can't transform it in a function to be passed to tm_map
.
Please consider the following 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