7

R の tm パッケージを使用して、プレーン テキスト ドキュメントのコーパス内のドキュメントをステミングしたいと考えています。コーパスのすべてのドキュメントに SnowballStemmer 関数を適用すると、各ドキュメントの最後の単語のみがステミングされます。

library(tm)
library(Snowball)
library(RWeka)
library(rJava)
path <- c("C:/path/to/diretory")
corp <- Corpus(DirSource(path),
               readerControl = list(reader = readPlain, language = "en_US",
                                    load = TRUE))
tm_map(corp,SnowballStemmer) #stemDocument has the same problem

文書がコーパスに読み込まれる方法に関係していると思います。これをいくつかの簡単な例で説明します。

> vec<-c("running runner runs","happyness happies")
> stemDocument(vec) 
   [1] "running runner run" "happyness happi" 

> vec2<-c("running","runner","runs","happyness","happies")
> stemDocument(vec2)
   [1] "run"    "runner" "run"    "happy"  "happi" <- 

> corp<-Corpus(VectorSource(vec))
> corp<-tm_map(corp, stemDocument)
> inspect(corp)
   A corpus with 2 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]]
   run runner run

   [[2]]
   happy happi

> corp2<-Corpus(DirSource(path),readerControl=list(reader=readPlain,language="en_US" ,  load=T))
> corp2<-tm_map(corp2, stemDocument)
> inspect(corp2)
   A corpus with 2 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.txt`
   running runner runs

   $`2.txt`
   happyness happies
4

2 に答える 2

3

私が見ている問題は、wordStemが単語のベクトルを取り込むことですが、Corpus plainTextReaderは、読み取るドキュメントでは、各単語が独自の行にあると想定しています。つまり、ドキュメントに3つの「単語」が含まれるため、plainTextReaderが混乱します。

From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes

代わりに、ドキュメントは

From
ancient
grudge
break
to
new
mutiny
where 
civil
...etc...

句読点もwordStemを混乱させるため、句読点も削除する必要があることにも注意してください。

実際のドキュメントを変更せずにこれを行う別の方法は、単語の前後に表示される英数字以外の文字を分離して削除する関数を定義することです。これが簡単なものです:

wordStem2 <- function(x) {
    mywords <- unlist(strsplit(x, " "))
    mycleanwords <- gsub("^\\W+|\\W+$", "", mywords, perl=T)
    mycleanwords <- mycleanwords[mycleanwords != ""]
    wordStem(mycleanwords)
}

corpA <- tm_map(mycorpus, wordStem2);
corpB <- Corpus(VectorSource(corpA));

ここで、corpBを通常のコーパスとして使用します。

于 2011-09-04T22:19:29.737 に答える