R中的文档术语矩阵

时间:2015-03-17 06:58:44

标签: r matrix text-mining tm corpus

我有以下代码:

rm(list=ls(all=TRUE)) #clear data
setwd("~/UCSB/14 Win 15/Issy/text.fwt") #set working directory
files <- list.files(); head(files) #load & check working directory

fw1 <- scan(what="c", sep="\n",file="fw_chp01.fwt")

library(tm) 
corpus2<-Corpus(VectorSource(c(fw1)))
skipWords<-(function(x) removeWords(x, stopwords("english")))

#remove punc, numbers, stopwords, etc
funcs<-list(content_transformer(tolower), removePunctuation, removeNumbers, stripWhitespace, skipWords)
corpus2.proc<-tm_map(corpus2, FUN = tm_reduce, tmFuns = funcs)

corpus2a.dtm <- DocumentTermMatrix(corpus2.proc, control = list(wordLengths = c(1,110))) #create document term matrix

我正在尝试使用tm参考手册(http://cran.r-project.org/web/packages/tm/tm.pdf)中详述的一些操作,但收效甚微。例如,当我尝试使用findFreqTerms时,我收到以下错误:

Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE

任何人都可以告诉我为什么这不起作用以及我能做些什么来解决它?

为@lawyeR编辑:

head(fw1)产生文本的前六行(James Joyce的Finnegans Wake第1集):

[1] "003.01    riverrun, past Eve and Adam's, from swerve of shore to bend"      
[2] "003.02  of bay, brings us by a commodius vicus of recirculation back to"    
[3] "003.03  Howth Castle and Environs."                                         
[4] "003.04    Sir Tristram, violer d'amores, fr'over the short sea, had passen-"
[5] "003.05  core rearrived from North Armorica on this side the scraggy"        
[6] "003.06  isthmus of Europe Minor to wielderfight his penisolate war: nor"  

inspect(corpus2)以下列格式输出文本的每一行(这是文本的最后一行):

[[960]]
<<PlainTextDocument (metadata: 7)>>
029.36  borough. #this part differs by line of course

inspect(corpus2a.dtm)返回所有类型的表(总共有4163个(在文本中采用以下格式:

Docs  youths yoxen yu yurap yutah zee zephiroth zine zingzang zmorde zoom
  1        0     0  0     0     0   0         0    0        0      0    0
  2        0     0  0     0     0   0         0    0        0      0    0

2 个答案:

答案 0 :(得分:0)

以下是您提供和完成的内容的简化形式,tm完成了它的工作。可能是您的一个或多个清洁步骤导致了问题。

> library(tm) 
> fw1 <- c("riverrun, past Eve and Adam's, from swerve of shore to bend      
+                                  of bay, brings us by a commodius vicus of recirculation back to
+                                  Howth Castle and Environs.      
+                                  Sir Tristram, violer d'amores, fr'over the short sea, had passen-
+                                  core rearrived from North Armorica on this side the scraggy    
+                                  isthmus of Europe Minor to wielderfight his penisolate war: nor")
> 
> corpus<-Corpus(VectorSource(c(fw1)))
> inspect(corpus)
<<VCorpus (documents: 1, metadata (corpus/indexed): 0/0)>>

[[1]]
<<PlainTextDocument (metadata: 7)>>
riverrun, past Eve and Adam's, from swerve of shore to bend      
                                 of bay, brings us by a commodius vicus of recirculation back to
                                 Howth Castle and Environs.      
                                 Sir Tristram, violer d'amores, fr'over the short sea, had passen-
                                 core rearrived from North Armorica on this side the scraggy    
                                 isthmus of Europe Minor to wielderfight his penisolate war: nor

> dtm <- DocumentTermMatrix(corpus)
> findFreqTerms(dtm)
 [1] "adam's,"       "and"           "armorica"      "back"          "bay,"          "bend"         
 [7] "brings"        "castle"        "commodius"     "core"          "d'amores,"     "environs."    
[13] "europe"        "eve"           "fr'over"       "from"          "had"           "his"          
[19] "howth"         "isthmus"       "minor"         "nor"           "north"         "passen-"      
[25] "past"          "penisolate"    "rearrived"     "recirculation" "riverrun,"     "scraggy"      
[31] "sea,"          "shore"         "short"         "side"          "sir"           "swerve"       
[37] "the"           "this"          "tristram,"     "vicus"         "violer"        "war:"         
[43] "wielderfight" 

另一方面,我发现在开始时将一些其他补充包加载到tm是有用的。

library(SnowballC); library(RWeka); library(rJava); library(RWekajars)

与你的有些复杂的清洁步骤相比,它的价值,我通常像这样跋涉(用你的文字向量替换评论$评论):

comments$comment <- tolower(comments$comment)
comments$comment <- removeNumbers(comments$comment)
comments$comment <- stripWhitespace(comments$comment) 
comments$comment <- str_replace_all(comments$comment, "  ", " ") 
# replace all double spaces internally with single space   
# better to remove punctuation with str_ because the tm function doesn't insert a space
library(stringr)
comments$comment <- str_replace_all(comments$comment, pattern = "[[:punct:]]", " ") 
comments$comment <- removeWords(comments$comment, stopwords(kind = "english"))

答案 1 :(得分:0)

从另一张票中,这应该有助于0.6.0有一个错误,可以通过此声明解决。

corpus_clean <- tm_map( corp_stemmed, PlainTextDocument)

希望这有帮助。