嗨:我有一本由他人准备的否定词典。我不确定他们如何进行除草,但似乎他们使用了Porter Stemer以外的其他东西。该字典具有通配符(*),我认为该通配符可以阻止词干的发生。但是我不知道如何在R上下文中通过grep()或tm包来使用它,因此我将其删除,希望找到一种grep部分匹配的方法。所以原来的字典看起来像这样
#load libraries
library(tm)
#sample dictionary terms for polarize and outlaw
negative<-c('polariz*', 'outlaw*')
#strip out wildcard
negative<-gsub('*', '', negative)
#test corpus
test<-c('polarize', 'polarizing', 'polarized', 'polarizes', 'outlaw', 'outlawed', 'outlaws')
#Here is how R's porter stemmer stems the text
stemDocument(test)
因此,如果我用R的词干对我的语料进行词干处理,则会在字典中找到“不合法”之类的词,但不会与“极化”之类的词相匹配,因为它们的词干与字典中所发现的词干会有所不同。
因此,我想拥有一种使tm包仅匹配每个单词的精确部分的方法。因此,我希望在不删除我的文档的情况下,能够在“违法”和“违法”一词中选择“非法”,并在“两极化”,“两极化和两极化”中选择“极化”。这可能吗?
#Define corpus
test.corp<-Corpus(VectorSource(test))
#make Document Term Matrix
dtm<-documentTermMatrix(test.corp, control=list(dictionary=negative))
#inspect
inspect(dtm)
我还没有看到任何tm答案,所以这里有一个使用Quanteda软件包作为替代方案的答案。它允许你使用“水珠在你的字典项,这是默认的”通配符值valuetype
的quanteda的字典功能。(请参阅参考资料?dictionary
。)使用这种方法,您不需要阻止文本。
library(quanteda)
packageVersion("quanteda")
## [1] ‘0.9.6.2’
# create a quanteda dictionary, essentially a named list
negative <- dictionary(list(polariz = 'polariz*', outlaw = 'outlaw*'))
negative
## Dictionary object with 2 key entries.
## - polariz: polariz*
## - outlaw: outlaw*
test <- c('polarize', 'polarizing', 'polarized', 'polarizes', 'outlaw', 'outlawed', 'outlaws')
dfm(test, dictionary = negative, valuetype = "glob", verbose = FALSE)
## Document-feature matrix of: 7 documents, 2 features.
## 7 x 2 sparse Matrix of class "dfmSparse"
## features
## docs polariz outlaw
## text1 1 0
## text3 1 0
## text2 1 0
## text4 1 0
## text5 0 1
## text6 0 1
## text7 0 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句