我试图通过使用 stri_replace_all_fixed 函数来阻止R中的~4000个文档。但是,它非常慢,因为我的词汇词典由约。 300k字。我这样做是因为文件是丹麦语,因此Porter Stemmer Algortihm没用(它过于激进)。
我已经发布了以下代码。有没有人知道这样做的替代方案?
逻辑:查看每个文档中的每个单词 - >如果word =来自voc-table的单词,则替换为tran-word。
##Read in the dictionary
voc <- read.table("danish.csv", header = TRUE, sep=";")
#Using the library 'stringi' to make the stemming
library(stringi)
#Split the voc corpus and put the word and stem column into different corpus
word <- Corpus(VectorSource(voc))[1]
tran <- Corpus(VectorSource(voc))[2]
#Using stri_replace_all_fixed to stem words
## !! NOTE THAT THE FOLLOWING STEP MIGHT TAKE A FEW MINUTES DEPENDING ON THE SIZE !! ##
docs <- tm_map(docs, function(x) stri_replace_all_fixed(x, word, tran, vectorize_all = FALSE))
&#34; voc&#34;的结构数据框:
Word Stem
1 abandonnere abandonner
2 abandonnerede abandonner
3 abandonnerende abandonner
...
313273 åsyns åsyn
答案 0 :(得分:0)
要使字典快速进行,您需要实现一些聪明的数据结构,例如前缀树。 300000x搜索和替换只是不扩展。
我认为这在R中效率不高,但您需要编写C或C ++扩展。你在那里有许多微小的操作,当在纯R中尝试这样做时,R解释器的开销会杀了你。