2013-04-17 36 views
9

tm -packageでRでテキストマイニングを行っています。すべてが非常にスムーズに動作します。しかし、ステミング後に1つの問題が発生します(http://en.wikipedia.org/wiki/Stemming)。明らかに、同じ幹を持ついくつかの単語がありますが、それらが「一緒に投げ込まれていない」ことは重要です(これらの単語は異なることを意味します)。tm-packageによるテキストマイニング - 単語のステミング

例については、以下の4つのテキストを参照してください。ここでは、 "講師"または "講義"( "関連"と "関連")を交換することはできません。しかし、これは手順4で行われたことです。

いくつかのケース/単語に対してこれを手動で実装する方法はありますか?(例:「講師」と「講義」は2つの異なるものとして保持されます)

texts <- c("i am member of the XYZ association", 
"apply for our open associate position", 
"xyz memorial lecture takes place on wednesday", 
"vote for the most popular lecturer") 

# Step 1: Create corpus 
corpus <- Corpus(DataframeSource(data.frame(texts))) 

# Step 2: Keep a copy of corpus to use later as a dictionary for stem completion 
corpus.copy <- corpus 

# Step 3: Stem words in the corpus 
corpus.temp <- tm_map(corpus, stemDocument, language = "english") 

inspect(corpus.temp) 

# Step 4: Complete the stems to their original form 
corpus.final <- tm_map(corpus.temp, stemCompletion, dictionary = corpus.copy) 

inspect(corpus.final) 
+2

これはステミングのポイントです。あなたは根本的な言葉を得るためにそれを行います。相違を保持したい場合は、相殺しないでください。 –

+1

私は知っています。しかし、場合によってはそれを元に戻すエレガントな方法はありませんか? – majom

答えて

9

私はあなたが後にしているものを100%じゃないと完全にどのようにtm_map作品を得ることはありません。私が理解すれば、次のような作品があります。私が理解しているように、あなたは抜け落ちてはならない単語のリストを提供したいと思います。私はqdapパッケージを使用しています。なぜなら、私は怠惰で、それには機能があります。mgsub私は好きです。

私はmgsubtm_mapを使用して不満を覚えていますが、それはエラーを投げかけているので、代わりにlapplyを使用しました。

texts <- c("i am member of the XYZ association", 
    "apply for our open associate position", 
    "xyz memorial lecture takes place on wednesday", 
    "vote for the most popular lecturer") 

library(tm) 
# Step 1: Create corpus 
corpus.copy <- corpus <- Corpus(DataframeSource(data.frame(texts))) 

library(qdap) 
# Step 2: list to retain and indentifier keys 
retain <- c("lecturer", "lecture") 
replace <- paste(seq_len(length(retain)), "SPECIAL_WORD", sep="_") 

# Step 3: sub the words you want to retain with identifier keys 
corpus[seq_len(length(corpus))] <- lapply(corpus, mgsub, pattern=retain, replacement=replace) 

# Step 4: Stem it 
corpus.temp <- tm_map(corpus, stemDocument, language = "english") 

# Step 5: reverse -> sub the identifier keys with the words you want to retain 
corpus.temp[seq_len(length(corpus.temp))] <- lapply(corpus.temp, mgsub, pattern=replace, replacement=retain) 

inspect(corpus)  #inspect the pieces for the folks playing along at home 
inspect(corpus.copy) 
inspect(corpus.temp) 

# Step 6: complete the stem 
corpus.final <- tm_map(corpus.temp, stemCompletion, dictionary = corpus.copy) 
inspect(corpus.final) 

基本的にそれがで動作します。付属の "NO STEM" の言葉(mgsub

  • の一意の識別子キーを下塗り

    1. あなたは(stemDocumentを使用して)
    2. 次に、それを逆にして、 "NO STEM"という単語(mgsub)で識別子キーをサブ入力します
    3. 最後

    stemCompletion)ステムを完了ここでの出力です:

    ## >  inspect(corpus.final) 
    ## A corpus with 4 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` 
    ## i am member of the XYZ associate 
    ## 
    ## $`2` 
    ## for our open associate position 
    ## 
    ## $`3` 
    ## xyz memorial lecture takes place on wednesday 
    ## 
    ## $`4` 
    ## vote for the most popular lecturer 
    
  • +0

    ご協力ありがとうございます。よく働く。 – majom

    0

    あなたはまた、言葉steemingために、以下のパッケージを使用することができます:https://cran.r-project.org/web/packages/SnowballC/SnowballC.pdfが。

    wordStemを使用するだけで、単語のベクトルと扱う言語が渡されます。使用する必要のある正確な言語文字列を知るには、getStemLanguagesメソッドを参照してください。これにより、可能なすべてのオプションが返されます。

    親切にお答えします

    関連する問題