我正在用R包“感情”进行情感分析
neg_words =扫描(“C:/Users/kothasan/Desktop/感情分析/否定.words.txt”,what=‘字符’,注释.what=‘;’)
pos_words =扫描(“C:/Users/kothasan/Desktop/感情分析/pos.words.txt”,什么=‘字符’,注释.what=‘;’)
score.sentiment =函数(句子,pos.words,neg.words,. .progress='none')
{
require(plyr);
require(stringr);
scores = laply(sentences, function(sentence, pos.words, neg.words) {
sentence = gsub('[^A-z ]','', sentence)
sentence = tolower(sentence);
word.list = str_split(sentence, '\\s+');
words = unlist(word.list);
pos.matches = match(words, pos.words);
neg.matches = match(words, neg.words);
pos.matches = !is.na(pos.matches);
neg.matches = !is.na(neg.matches);
score = sum(pos.matches) - sum(neg.matches);
return(score);
}, pos.words, neg.words, .progress=.progress );
scores.df = data.frame(score=scores, text=sentences);
return(scores.df);}
示例数据由两行组成:。"text“是列名。
text所使用的功能:
分析=score.sentiment(样本、pos_words、neg_words)
当我运行上面的函数时,我得到以下警告,输出分数为0,0,这是错误的:
警告消息:在data.frame中(得分=分数,文本=句子):行名从一个短变量中找到,并已被丢弃
输出:
得分文本
当我只使用一行作为输入时,我得到了正确的分数。
有人能帮我解决这个问题吗?
谢谢,
桑迪普
发布于 2017-10-17 10:02:25
您能否检查要执行senti分析的文本列是否是向量。示例:如果数据集有2列:数字和文本。你想对文本进行情感分析。因此,在运行代码之前,请将代码转到像sentences=dataset$text这样的向量。现在使用代码中的句子。
https://stackoverflow.com/questions/44368239
复制相似问题