繁体   English   中英

根据词典数据框替换语料库中的单词

[英]Replace words in corpus according to dictionary data frame

我有兴趣根据由两列数据帧组成的字典替换tm Corpus对象中的所有单词,其中第一列是要匹配的单词,第二列是替换单词。

我被translate功能所困扰。 我看到了这个答案,但无法将其转换为要传递给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

我建议不要data.frame用于字典,因为默认情况下R的基本对象(向量)是字典。

      dict  <- c('primo', 'secondo', 'testo')
names(dict) <- c('first', 'second', 'text')

然后将"tanslate" x ,其中x可能是"second" ,您只需使用:

   dict[[x]]

您甚至不需要包装函数。


如果要以相反的方向平移,请使用

   name(dict)[names(dict) %in% x]

或者你可以翻字典

         dict.flip  <- names(dict)
   names(dict.flip) <- dict

tm软件包的tm_map函数结合使用,可以使用stri_replace_all_fixed软件包中的stringi 例如:

library(tm)
library(stringi)

docs <- c("first text", "second text")
corp <- Corpus(VectorSource(docs))

word <- c('first', 'second', 'text')
tran <- c('primo', 'secondo', 'testo')

corp <- tm_map(corp, function(x) stri_replace_all_fixed(x, word, tran, vectorize_all = FALSE))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM