2016-07-26 1 views
0

コーパスを指定すると、単語のステミングとステム補完のためにRにtm (Text Mining) packageを使用して、正規化する必要がありますが、stemCompletionステップには0.6.xバージョンのパッケージに問題があります。 tm 0.6-2でR 3.3.1を使用する。StemCompletionとPlainTextDocumentを呼び出すR

この質問は以前に尋ねられましたが、実際に動作する完全な回答は見られませんでした。問題を適切に示すための完全なコードは次のとおりです。ここで

require(tm) 
txt <- c("Once we have a corpus we typically want to modify the documents in it", 
      "e.g., stemming, stopword removal, et cetera.", 
      "In tm, all this functionality is subsumed into the concept of a transformation.") 

myCorpus <- Corpus(VectorSource(txt)) 

myCorpus <- tm_map(myCorpus, content_transformer(tolower)) 
myCorpus <- tm_map(myCorpus, removePunctuation) 
myCorpusCopy <- myCorpus 

# *Removing common word endings* (e.g., "ing", "es") 
myCorpus <- tm_map(myCorpus, stemDocument, language = "english") 

# Next, we remove all the empty spaces generated by isolating the 
# word stems in the previous step. 
myCorpus <- tm_map(myCorpus, content_transformer(stripWhitespace)) 

tdm <- TermDocumentMatrix(myCorpus, control = list(wordLengths = c(3, Inf))) 
print(tdm) 
print(dimnames(tdm)$Terms) 

が出力されます。

<<TermDocumentMatrix (terms: 19, documents: 2)>> 
Non-/sparse entries: 20/18 
Sparsity   : 47% 
Maximal term length: 9 
Weighting   : term frequency (tf) 
[1] "all"  "cetera" "concept" "corpus" "document" 
[6] "function" "have"  "into"  "modifi" "onc"  
[11] "remov"  "stem"  "stopword" "subsum" "the"  
[16] "this"  "transform" "typic"  "want"  

用語のいくつかは食い止められている: "MODIFI"、 "remov"、 "subsum"、 "typic"、および "ONC"。

次に、ステミングを完了します。この段階で

myCorpus = tm_map(myCorpus, stemCompletion, dictionary=myCorpusCopy) 

、コーパスは、もは​​やTextDocumentではないとTermDocumentMatrixを作成すると、エラーで失敗します(DOC、「TextDocument」)を継承TRUEではありません。次に関数PlainTextDocument()を適用することが文書化されています。ここで

myCorpus <- tm_map(myCorpus, PlainTextDocument) 

tdm <- TermDocumentMatrix(myCorpus, control = list(wordLengths = c(3, Inf))) 
print(tdm) 
print(dimnames(tdm)$Terms) 

が出力されます。

<TermDocumentMatrix (terms: 2, documents: 2)>> 
Non-/sparse entries: 4/0 
Sparsity   : 0% 
Maximal term length: 7 
Weighting   : term frequency (tf) 
[1] "content" "meta" 

コーパスを破損したPlainTextDocumentを呼び出します。

ステム付きの単語が完成することを期待してください。 "modifi" => "修飾子"、 "onc" => "once"など

+1

[Rの警告は、stemCompletionとTermDocumentMatrixのエラーで警告されます](http://stackoverflow.com/questions/30321770/r-warning-in-stemcompletion-and-error-in-termdocumentmatrix) –

+0

前述のように質問では、この質問は繰り返されましたが、完全な回答は見られず、より多くの場合、質問は完全に自己完結型ではありませんでした(例:テキストファイルを読み込みました)。 – JasonM1

+0

http://stackoverflow.com/questions/25206049/stemcompletion-is-not-working –

答えて

2

PlainTextDocumentを呼び出すとコーパスが壊れませんでした。

あなたがライン

myCorpus = tm_map(myCorpus, stemCompletion, dictionary=myCorpusCopy) 

を実行したときに、いくつかの警告メッセージだということに気づいたかもしれません:

Warning messages: 
1: In grep(sprintf("^%s", w), dictionary, value = TRUE) : 
    argument 'pattern' has length > 1 and only the first element will be used 
2: In grep(sprintf("^%s", w), dictionary, value = TRUE) : 
    argument 'pattern' has length > 1 and only the first element will be used 
3: In grep(sprintf("^%s", w), dictionary, value = TRUE) : 
    argument 'pattern' has length > 1 and only the first element will be used 

ものを言及する価値があった。

これはどのように実行するかです)あなたのデータを使用して茎の完成をもたらす:

  stems  completed  
all  "all"  "all"   
cetera "cetera" "cetera"   
concept "concept" "concept"  
corpus "corpus" "corpus"   
document "document" "documents"  
function "function" "functionality" 
have  "have"  "have"   
into  "into"  "into"   
modifi "modifi" "modify"    
onc  "onc"  "once"   
remov  "remov"  "removal"  
stem  "stem"  "stemming"  
stopword "stopword" "stopword"  
subsum "subsum" "subsumed"  
the  "the"  "the"   
this  "this"  "this"   
transform "transform" "transformation" 
typic  "typic"  "typically"  
want  "want"  "want" 

永久に戻ってTDMに変更書き込むには:ハック-Rのソリューションに関して

stemCompletion_mod <- function(x,dict=dictCorpus) { 
    PlainTextDocument(stripWhitespace(paste(stemCompletion(unlist(strsplit(as.character(x)," ")), 
                 dictionary=dict, type="shortest"),sep="", 
              collapse=" ")))} 

tdm <- stemCompletion_mod(rownames(tdm), myCorpus) 


tdm$content 

[1] "all cetera concept corpus documents functionality have into NA once removal stemming stopword subsumed the this transformation typically want"

+0

ステム完成語の便利なリストを取得していますが、TermDocumentMatrixにはまだこの時点でアンステミングされていない用語があり、ワードクラウドやその他のパッケージではtdmを使用してもまだステートメントがありません。 – JasonM1

+0

@ JasonM1 OK変更をTDMに書き戻すように更新しました。私はここから関数を持っています:http://stackoverflow.com/questions/25206049/stemcompletion-is-not-working –

+0

答えのために@Hack-Rに感謝しますが、上記のステップの後にtdmを使ってwordcloudを使用すると、まだwordcloudにunstemmed termsを表示しています。 \tまた、上記のコードを実行すると、 "modifi"の "modify"がリストされている空の文字列があります。 Windows上でR 3.3.1とtm 0.6-2を使用する方法。 – JasonM1

1

を、私は「StemCompletedを持っていると思ったジェイソン、同じ問題がありました「ワードクラウドで使用するための単語、およびTDMの一部としての単語。

stemCompletionはTDMを返さないため、TDMから「terms」を抽出し、stemCompletionを実行しました。

[1] "all"  "cetera" "concept" "corpus" "document" 
[6] "function" "have"  "into"  "modifi" "onc"  
[11] "remov"  "stem"  "stopword" "subsum" "the"  
[16] "this"  "transform" "typic"  "want"  

stemCompletionは、文字テーブルを返すように見えるので、私はちょうど置き換え:この出力を与える

require(tm) 
txt <- c("Once we have a corpus we typically want to modify the documents in it", 
     "e.g., stemming, stopword removal, et cetera.", 
     "In tm, all this functionality is subsumed into the concept of a transformation.") 

myCorpus <- Corpus(VectorSource(txt)) 

myCorpus <- tm_map(myCorpus, content_transformer(tolower)) 
myCorpus <- tm_map(myCorpus, removePunctuation) 
myCorpusCopy <- myCorpus 

# *Removing common word endings* (e.g., "ing", "es") 
myCorpus <- tm_map(myCorpus, stemDocument, language = "english") 

# Next, we remove all the empty spaces generated by isolating the 
# word stems in the previous step. 
myCorpus <- tm_map(myCorpus, content_transformer(stripWhitespace)) 

tdm <- TermDocumentMatrix(myCorpus, control = list(wordLengths = c(3, Inf))) 
print(tdm) 
print(dimnames(tdm)$Terms) 

は(私がテストしていた間、私は別の変数にこれらを壊しました) stemCompletedバージョンの「tdm」の語句部分:

tdm$dimnames$Terms <- as.character(stemCompletion(tdm$dimnames$Terms, myCorpusCopy, type = "prevalent")) 
print(tdm$dimnames$Terms) 

は、これは私を与える:

[1] "all"   "cetera"   "concept"  "corpus"   
[5] "documents"  "functionality" "have"   "into"   
[9] ""    "once"   "removal"  "stemming"  
[13] "stopword"  "subsumed"  "the"   "this"   
[17] "transformation" "typically"  "want"   

あなたは、明らかに、しかし、少なくともこの時間はあなたがstemCompletedのバージョンで動作することができ、それは(「MODIFI」)をどうするかを知らない言葉の空白のフィールドを取得します。 ..

関連する問題