11

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

例として、以下の 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)
4

2 に答える 2

13

私はあなたが求めているものを 100% 理解しているわけではありませんtm_map。私が理解すれば、次の作品。私が理解しているように、ステミングしてはならない単語のリストを提供したいと考えています。qdap パッケージを使用しているのは、主に怠け者であり、mgsub好きな機能があるためです。

mgsubandを使用するとtm_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)

基本的には次のように機能します。

  1. 提供された「NO STEM」単語の一意の識別子キーをサブアウトする ( mgsub)
  2. stemDocumentその後、あなたは(を使用して)ステム
  3. 次に、それを逆にして、識別子キーを「NO STEM」の単語 ( mgsub)でサブします。
  4. 最後にステムを完成させます ( 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
于 2013-04-18T00:01:27.987 に答える