12

Rでステミングをしようとしていますが、個々のドキュメントでしか機能しないようです。私の最終的な目標は、ドキュメント内の各用語の頻度を示す用語ドキュメント マトリックスです。

次に例を示します。

require(RWeka)
require(tm)
require(Snowball)

worder1<- c("I am taking","these are the samples",
"He speaks differently","This is distilled","It was placed")
df1 <- data.frame(id=1:5, words=worder1)

> df1
  id                 words
1  1           I am taking
2  2 these are the samples
3  3 He speaks differently
4  4     This is distilled
5  5         It was placed

この方法は、ステミング部分では機能しますが、用語ドキュメント マトリックス部分では機能しません。

> corp1 <- Corpus(VectorSource(df1$words))
> inspect(corp1)
A corpus with 5 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 taking

[[2]]
these are the samples

[[3]]
He speaks differently

[[4]]
This is distilled

[[5]]
It was placed

> corp1 <- tm_map(corp1, SnowballStemmer)
> inspect(corp1)
A corpus with 5 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]]
[1] I am tak

[[2]]
[1] these are the sampl

[[3]]
[1] He speaks differ

[[4]]
[1] This is distil

[[5]]
[1] It was plac

>  class(corp1)
[1] "VCorpus" "Corpus"  "list"   
> tdm1 <- TermDocumentMatrix(corp1)
Error in UseMethod("Content", x) : 
  no applicable method for 'Content' applied to an object of class "character"

代わりに、最初に用語ドキュメント マトリックスを作成しようとしましたが、今回は単語が語幹処理されません。

> corp1 <- Corpus(VectorSource(df1$words))
> tdm1 <- TermDocumentMatrix(corp1, control=list(stemDocument=TRUE))
>  as.matrix(tdm1)
             Docs
Terms         1 2 3 4 5
  are         0 1 0 0 0
  differently 0 0 1 0 0
  distilled   0 0 0 1 0
  placed      0 0 0 0 1
  samples     0 1 0 0 0
  speaks      0 0 1 0 0
  taking      1 0 0 0 0
  the         0 1 0 0 0
  these       0 1 0 0 0
  this        0 0 0 1 0
  was         0 0 0 0 1

ここでは、単語は明らかにステミングされていません。

助言がありますか?

4

4 に答える 4

9

CRANのRTextToolsパッケージを使用すると、これを行うことができます。

library(RTextTools)
worder1<- c("I am taking","these are the samples",
"He speaks differently","This is distilled","It was placed")
df1 <- data.frame(id=1:5, words=worder1)

matrix <- create_matrix(df1, stemWords=TRUE, removeStopwords=FALSE, minWordLength=2)
colnames(matrix) # SEE THE STEMMED TERMS

DocumentTermMatrixこれは、 package で使用できるa を返しますtm。必要な結果を得るために、他のパラメーター (ストップワードの削除、最小単語長の変更、別の言語のステマーの使用など) をいじることができます。この例を表示as.matrixすると、次の用語行列が生成されます。

                         Terms
Docs                      am are differ distil he is it place sampl speak take the these this was
  1 I am taking            1   0      0      0  0  0  0     0     0     0    1   0     0    0   0
  2 these are the samples  0   1      0      0  0  0  0     0     1     0    0   1     1    0   0
  3 He speaks differently  0   0      1      0  1  0  0     0     0     1    0   0     0    0   0
  4 This is distilled      0   0      0      1  0  1  0     0     0     0    0   0     0    1   0
  5 It was placed          0   0      0      0  0  0  1     1     0     0    0   0     0    0   1
于 2012-08-14T19:46:59.923 に答える
3

これは、バージョン 0.6Rで期待どおりに機能します。tmステミングが正しく機能するのを妨げるいくつかの小さなエラーがありました。おそらく、古いバージョンのtm? とにかく、これを機能させる方法は次のとおりです。

require(RWeka)
require(tm)

ステミング パッケージはあなたのものではありませんSnowballSnowballC:

require(SnowballC)

worder1<- c("I am taking","these are the samples",
            "He speaks differently","This is distilled","It was placed")
df1 <- data.frame(id=1:5, words=worder1)
corp1 <- Corpus(VectorSource(df1$words))
inspect(corp1)

次の行で次のようにSnowballStemmer変更します。stemDocument

corp1 <- tm_map(corp1, stemDocument)
inspect(corp1)

予想どおり、単語は語幹処理されます。

<<VCorpus (documents: 5, metadata (corpus/indexed): 0/0)>>

[[1]]
<<PlainTextDocument (metadata: 7)>>
I am take

[[2]]
<<PlainTextDocument (metadata: 7)>>
these are the sampl

[[3]]
<<PlainTextDocument (metadata: 7)>>
He speak differ

[[4]]
<<PlainTextDocument (metadata: 7)>>
This is distil

[[5]]
<<PlainTextDocument (metadata: 7)>>
It was place

ここで、ドキュメント マトリックスという用語を実行します。

corp1 <- Corpus(VectorSource(df1$words))

に変更stemDocumentstemmingます。

tdm1 <- TermDocumentMatrix(corp1, control=list(stemming=TRUE))
as.matrix(tdm1)

そして、予想どおり、語幹抽出された単語の tdm を取得します。

        Docs
Terms    1 2 3 4 5
  are    0 1 0 0 0
  differ 0 0 1 0 0
  distil 0 0 0 1 0
  place  0 0 0 0 1
  sampl  0 1 0 0 0
  speak  0 0 1 0 0
  take   1 0 0 0 0
  the    0 1 0 0 0
  these  0 1 0 0 0
  this   0 0 0 1 0
  was    0 0 0 0 1

では、どうぞ。おそらく、tmドキュメントをもっと注意深く読むことで、これで少し時間を節約できたかもしれません;)

于 2014-11-02T06:15:50.550 に答える
1

はい、必要なコーパスのドキュメントの単語をステミングしRweka、パッケージ化Snowballします。tm

次の指示を使用してください

> library (tm)
#set your directory Suppose u have set "F:/St" then next command is 
> a<-Corpus(DirSource("/st"), 
            readerControl=list(language="english")) # "/st" it is path of your directory
> a<-tm_map(a, stemDocument, language="english")
> inspect(a)

あなたが望む結果を見つけることを確認してください。

于 2012-08-23T05:12:22.440 に答える