【问题标题】:error message while stemming for sentiment analysis进行情绪分析时出现错误消息
【发布时间】:2014-04-16 22:59:34
【问题描述】:

我对我的数据集进行词干分析以进行情绪分析,但收到此错误消息

"结构错误(if (length(n)) n else NA, names = x) : 'names' 属性 [2] 必须与向量 [1] 的长度相同"

请帮忙!

myCorpus<-Corpus(VectorSource(Datasetlow_cost_airline$text))
# Convert to lower case
myCorpus<-tm_map(myCorpus,tolower)
# Remove puntuation
myCorpus<-tm_map(myCorpus,removePunctuation)
# Remove numbers
myCorpus<-tm_map(myCorpus,removeNumbers)
# Remove URLs ?regex = regular expression ?gsub = pattern matching
removeURL<-function(x)gsub("http[[:alnum:]]*","",x)
myCorpus<-tm_map(myCorpus,removeURL)
stopwords("english")
# Add two extra stop words: 'available' and 'via'
myStopwords<-c(stopwords("english"),"available","via","can")
# Remove stopwords from corpus
myCorpus<-tm_map(myCorpus,removeWords,myStopwords)
# Keep a copy of corpus to use later as a dictionary for stem completion
myCorpusCopy<-myCorpus
# Stem word (change all the words to its root word)
myCorpus<-tm_map(myCorpus,stemDocument)
# Inspect documents (tweets) numbered 11 to 15
for(i in 11:15){
cat(paste("[[",i,"]]",sep=""))
writeLines(strwrap(myCorpus[[i]],width=73))
}
# Stem completion
myCorpus<-tm_map(myCorpus,stemCompletion,dictionary=myCorpusCopy)

【问题讨论】:

  • 嗯,在我的 Datasethigh_cost_airline$text 向量上工作。你能用Datasethigh_cost_airline$text试试看它是否有效吗?有关该数据集的更多信息:stackoverflow.com/help/mcve

标签: r sentiment-analysis stemming


【解决方案1】:

tm 0.6 版中的stemCompletion 函数似乎有些奇怪。有一个很好的解决方法here 用于this answer。简而言之,更换你的

# Stem completion
myCorpus <- tm_map(myCorpus, stemCompletion, dictionary = myCorpusCopy) # use spaces!

# Stem completion
stemCompletion_mod <- function(x,dict) {
  PlainTextDocument(stripWhitespace(paste(stemCompletion(unlist(strsplit(as.character(x)," ")), dictionary = dict, type = "shortest"), sep = "", collapse = " ")))
}
# apply workaround function 
myCorpus <- lapply(corpus, stemCompletion_mod, myCorpusCopy)

如果这没有帮助,那么您需要提供更多详细信息和实际数据样本。

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2015-09-23
    • 2012-05-01
    • 1970-01-01
    相关资源
    最近更新 更多