5

I am using the tm and wordcloud packages in R 2.15.1. I am trying to make a word cloud Here is the code:

maruti_tweets = userTimeline("Maruti_suzuki", n=1000,cainfo="cacert.pem")
hyundai_tweets = userTimeline("HyundaiIndia", n=1000,cainfo="cacert.pem")
tata_tweets = userTimeline("TataMotor", n=1000,cainfo="cacert.pem")
toyota_tweets = userTimeline("Toyota_India", n=1000,cainfo="cacert.pem")
# get text
maruti_txt = sapply(maruti_tweets, function(x) x$getText())
hyundai_txt = sapply(hyundai_tweets, function(x) x$getText())
tata_txt = sapply(tata_tweets, function(x) x$getText())
toyota_txt = sapply(toyota_tweets, function(x) x$getText())
clean.text = function(x)

{
   # tolower
   x = tolower(x)
   # remove rt
   x = gsub("rt", "", x)
   # remove at
   x = gsub("@\\w+", "", x)
   # remove punctuation
   x = gsub("[[:punct:]]", "", x)
   # remove numbers
   x = gsub("[[:digit:]]", "", x)
   # remove links http
   x = gsub("http\\w+", "", x)
   # remove tabs
   x = gsub("[ |\t]{2,}", "", x)
   # remove blank spaces at the beginning
   x = gsub("^ ", "", x)
   # remove blank spaces at the end
   x = gsub(" $", "", x)
   return(x)
}
# clean texts
maruti_clean = clean.text(maruti_txt)
hyundai_clean = clean.text(hyundai_txt)
tata_clean = clean.text(tata_txt)
toyota_clean = clean.text(toyota_txt)
maruti = paste(maruti_clean, collapse=" ")
hyundai= paste(hyundai_clean, collapse=" ")
tata= paste(tata_clean, collapse=" ")
toyota= paste(toyota_clean, collapse=" ")
# put ehyundaiything in a single vector
all = c(maruti, hyundai, tata, toyota)
# remove stop-words
all = removeWords(all,
c(stopwords("english"), "maruti", "tata", "hyundai", "toyota"))
# create corpus
corpus = Corpus(VectorSource(all))
# create term-document matrix
tdm = TermDocumentMatrix(corpus)
# convert as matrix
tdm = as.matrix(tdm)
# add column names
colnames(tdm) = c("MARUTI", "HYUNDAI", "TATA", "TOYOTA")
# comparison cloud
comparison.cloud(tdm, random.order=FALSE,colors = c("#00B2FF", "red",     #FF0099","#6600CC"),max.words=500)

but getting following error

Error in strwidth(words[i], cex = size[i], ...) : invalid 'cex' value
please help
4

2 回答 2

1

您在 TataMotors 推特帐户中有错字。它应该拼写为“TataMotors”,而不是“TataMotor”。结果,您的术语矩阵中的一列是空的,当计算 cex 时,它会被分配为 NAN。

修正错字,其余代码工作正常。祝你好运!

在此处输入图像描述

于 2013-12-03T08:24:31.097 回答
1

我在另一个应用程序中发现了空列问题,引发了相同的错误。就我而言,这是因为removeSparseTerms应用于文档术语矩阵的命令。使用str()帮助我识别错误。

输入变量(稍作编辑)有 289 列:

> str(corpus.dtm)
List of 6
$ i       : int [1:443] 3 4 6 8 10 12 15 18 19 21 ...
$ j       : int [1:443] 105 98 210 93 287 249 126 223 129 146 ...
$ v       : num [1:443] 1 1 1 1 1 1 1 1 1 1 ...
$ nrow    : int 1408
$ ncol    : int 289
$ dimnames:List of 2
..$ Docs : chr [1:1408] "character(0)" "character(0)" "character(0)" "character(0)" ...
..$ Terms: chr [1:289] "word1" "word2" "word3" "word4" ...
- attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
- attr(*, "weighting")= chr [1:2] "term frequency" "tf"

命令是:

removeSparseTerms(corpus.dtm,0.90)->corpus.dtm.frequent

结果有 0 列:

> str(corpus.dtm.frequent)
List of 6
$ i       : int(0) 
$ j       : int(0) 
$ v       : num(0) 
$ nrow    : int 1408
$ ncol    : int 0
$ dimnames:List of 2
..$ Docs : chr [1:1408] "character(0)" "character(0)" "character(0)" "character(0)" ...
..$ Terms: NULL
- attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
- attr(*, "weighting")= chr [1:2] "term frequency" "tf"

将稀疏系数从 0.90 提高到 0.95 解决了这个问题。对于更冗长的文档,我提高到 0.999,以便在删除稀疏术语后获得非空结果。

发生此错误时检查空列是一件好事。

于 2016-01-07T08:42:29.460 回答