【发布时间】:2014-01-02 00:26:58
【问题描述】:
我有兴趣根据由两列数据框组成的字典替换 tm Corpus 对象中的所有单词,其中第一列是要匹配的单词,第二列是替换单词。
我被translate 函数卡住了。我看到了this answer,但我无法将其转换为要传递给tm_map 的函数。
请考虑以下 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
【问题讨论】: