2016-05-06 9 views
1

こんにちは:他の人が作成した否定的な用語の辞書があります。彼らがどうやってステミングをやっているのかは分かりませんが、彼らはPorter Stemer以外のものを使ったようです。この辞書には、ステミングが起きると思われるワイルドカード文字(*)があります。しかし、Rコンテキストでgrep()やtmパッケージを使用する方法はわからないので、部分一致をgrepする方法を見つけたいと考えています。 だから、元の辞書には、私はRのステマと私のコーパスを茎のであれば、「非合法」などの用語が辞書に見出されるこのR:grepとtmパッケージを使用した部分一致辞書の用語

#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) 

のように見えますが、それは「偏光」などの用語が一致しませんそのようなものは、辞書にあるものとは異なる形で生じるからです。

私は、tmパッケージを各単語の正確な部分だけに一致させる方法をいくつか持っています。だから、私の書類の形を損なうことなく、「無法者」と「無法者」という用語で「無法者」を選び、「偏極」、「偏極」、「偏極」の中で「偏極」を選ぶことができるようにしたい。これは可能ですか?

#Define corpus 
test.corp<-Corpus(VectorSource(test)) 
#make Document Term Matrix 
dtm<-documentTermMatrix(test.corp, control=list(dictionary=negative)) 
#inspect 
inspect(dtm) 

答えて

1

私はどのTM答えを見ていないので、ここでの代替としてquantedaパッケージを使用して1です。辞書エントリに "glob"のワイルドカード値を使用できます。これは、quantedaの辞書機能のデフォルトのvaluetypeです。 (?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 
+0

これは実際には完全に機能します。今は別の辞書を持っていますが、複数の単語を含むエントリがあるところです。 – spindoctor

関連する問題