gpt4 book ai didi

r - TermDocumentMatrix 有时会抛出错误

转载 作者:行者123 更新时间:2023-12-02 17:39:17 34 4
gpt4 key购买 nike

我正在根据各个不同运动队的推文创建词云。此代码成功执行的概率约为十分之一:

handle <- 'arsenal'
txt <- searchTwitter(handle,n=1000,lang='en')
t <- sapply(txt,function(x) x$getText())
t <- gsub('http.*\\s*|RT|Retweet','',t)
t <- gsub(handle,'',t)
t_c <- Corpus(VectorSource(t))
tdm = TermDocumentMatrix(t_c,control = list(removePunctuation = TRUE,stopwords = stopwords("english"),removeNumbers = TRUE, content_transformer(tolower)))
m = as.matrix(tdm)
word_freqs = sort(rowSums(m), decreasing=TRUE)
dm = data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"),rot.per=0.5)

另外 10 次中有 9 次,它会抛出以下错误:

Error in simple_triplet_matrix(i = i, j = j, v = as.numeric(v), nrow = length(allTerms),  : 
'i, j, v' different lengths
In addition: Warning messages:
1: In mclapply(unname(content(x)), termFreq, control) :
all scheduled cores encountered errors in user code
2: In simple_triplet_matrix(i = i, j = j, v = as.numeric(v), nrow = length(allTerms), :
NAs introduced by coercion

大家有什么想法吗?我用谷歌搜索过,但到目前为止还没有找到!请记住,我绝对是 R 新手!

最佳答案

经过一番尝试后,以下代码行完全解决了我的问题:

t <- iconv(t,to="utf-8-mac")

关于r - TermDocumentMatrix 有时会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699340/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com