2012-08-09 35 views
11

Tôi đang cố gắng thực hiện một số xuất phát trong R nhưng dường như nó chỉ hoạt động trên các tài liệu riêng lẻ. Mục tiêu cuối cùng của tôi là một ma trận tài liệu thuật ngữ cho biết tần suất của mỗi thuật ngữ trong tài liệu.R xuất phát một chuỗi/tài liệu/corpus

Dưới đây là một ví dụ:

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 

Phương pháp này làm việc cho một phần bắt nguồn nhưng không phải là một phần ma trận tài liệu hạn:

> 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" 

Vì vậy, thay vào đó tôi cố gắng tạo ra các ma trận tài liệu nhiệm kỳ đầu tiên nhưng lần này các từ không được bắt nguồn:

> 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 

Đây là các từ rõ ràng không bắt nguồn.

Mọi đề xuất?

+0

Việc bắt đầu chỉ làm việc trên từ cuối cùng của tài liệu của bạn, phải không? Bởi vì '" nói "" đã không được bắt nguồn, trong khi tôi nghĩ nó nên. Ý kiến ​​của tôi là hàm khởi đầu trong R có nhiều vấn đề. Tôi và các đồng nghiệp của tôi chưa bao giờ có thể làm cho nó hoạt động được. Chúng tôi đã chạy tập lệnh python thay vì ... – Pop

+0

@AllenR .: Bạn nói đúng. Tôi không nhận ra điều đó. Tôi sẽ cung cấp cho python một cái nhìn. Cảm ơn. – screechOwl

+1

Tôi không biết nếu bạn đã nghe nói về gói ** nltk ** trong python mà loại điều này. – Pop

Trả lời

9

Gói RTextTools trên CRAN cho phép bạn thực hiện việc này.

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 

này trả về một DocumentTermMatrix có thể được sử dụng với gói tm. Bạn có thể chơi xung quanh với các thông số khác (ví dụ: loại bỏ các từ dừng, thay đổi độ dài từ tối thiểu, sử dụng trình dịch gốc cho một ngôn ngữ khác) để nhận kết quả bạn cần. Khi hiển thị as.matrix ví dụ tạo ra ma trận hạn sau đây:

      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 
+1

Cảm ơn bạn rất nhiều. – screechOwl

+0

Cảm ơn rất nhiều. Tuy nhiên, RTextTools không bao gồm ngôn ngữ Thổ Nhĩ Kỳ để bắt đầu. Do đó tôi đã thêm một giải pháp khác. –

1

Có cho steming lời của tài liệu trong một Corpus bạn cần Rweka, Snowballtm gói.

sử dụng sau đây hướng dẫn

> 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) 

chắc chắn bạn sẽ tìm thấy kết quả mong muốn của bạn.

3

Điều này hoạt động trong R như mong đợi với tm phiên bản 0.6. Bạn có một vài lỗi nhỏ ngăn cản việc khởi động để hoạt động chính xác, có lẽ chúng là từ phiên bản cũ hơn của tm?Dù sao, đây là cách để làm cho nó làm việc:

require(RWeka) 
require(tm) 

Các gói phần mềm bắt nguồn không phải là Snowball của bạn nhưng SnowballC:

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) 

Thay đổi của bạn SnowballStemmer-stemDocument trong dòng tiếp theo như sau:

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

Từ bắt nguồn, như mong đợi:

<<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 

Bây giờ làm ma trận tài liệu hạn:

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

Thay đổi của bạn stemDocument-stemming:

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

Và chúng ta có được một TDM của bắt nguồn từ, như mong đợi:

 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 

Vì vậy, có bạn đi. Có lẽ đọc kỹ hơn tài liệu tm có thể đã tiết kiệm một chút thời gian của bạn với điều này;)

0

Một giải pháp khác là viết mã cứng. Nó chỉ chia tách các văn bản và thân cây sau đó reconcentrates:

library(SnowballC) 
i=1 
#Snowball stemming 
while(i<=nrow(veri)){ 
    metin=veri[i,2] 
    stemmed_metin=""; 
    parcali=unlist(strsplit(metin,split=" ")) #split the text 
    for(klm in parcali){ 
    stemmed_klm=wordStem(klm,language = "turkish") #stem word by word 
    stemmed_metin=sprintf("%s %s",stemmed_metin,stemmed_klm) #reconcantrate 
    } 

    veri[i,4]=stemmed_metin #write to new column 

    i=i+1 
}