我对分数有一个(可能)愚蠢的问题。情绪 我尝试将此函数与 3 个默认短语一起使用,问题是该函数返回分数 0.0.0,但它应该返回 2.-5.4 我不明白这个问题,因为 RGui 不会给我错误,而且我正在遵循教程!
我已经下载了否定词和肯定词的列表
hu.liu.pos = scan('https://www.dropbox.com/sh/3xctszdxx4n00xq/AAA_Go_Y3kJxQACFaVBem__ea/positive-words.txt?dl=0', what='character', comment.char=';');
hu.liu.neg = scan('https://www.dropbox.com/sh/3xctszdxx4n00xq/AABTGWHitlRZcddq1pPXOSqca/negative-words.txt?dl=0', what='character', comment.char=';');
我有我的评分函数
score.sentiment = function(sentences, 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);
}
这是我的示例代码
sample=c("You're awesome and I love you","I hate and hate and hate. So angry. Die!","Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.")
result=score.sentiment(sample,pos.words,neg.words)
class(result)
result$score
result
如果有用的话,我已经安装了库和软件包。
install.packages('twitteR', dependencies=T);
install.packages('ggplot2', dependencies=T);
install.packages('XML', dependencies=T);
install.packages('plyr', dependencies=T);
install.packages('doBy', dependencies=T);
install.packages('tm', dependencies=T);
install.packages('RJSONIO', dependencies=T)
install.packages('RWeka')
install.packages('base64enc')
library(twitteR);
library(ggplot2);
library(XML);
library(plyr);
library(doBy);
library(RJSONIO)
library(tm)
library(RWeka)
谢谢您的建议
最佳答案
适合我
hu.liu.pos = readLines('https://www.dropbox.com/sh/3xctszdxx4n00xq/AAA_Go_Y3kJxQACFaVBem__ea/positive-words.txt?dl=1');
hu.liu.neg = readLines('https://www.dropbox.com/sh/3xctszdxx4n00xq/AABTGWHitlRZcddq1pPXOSqca/negative-words.txt?dl=1');
score.sentiment = function(sentences, 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);
}
sample=c("You're awesome and I love you","I hate and hate and hate. So angry. Die!","Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.")
result=score.sentiment(sample,hu.liu.pos,hu.liu.neg)
result
# score text
# 1 2 You're awesome and I love you
# 2 -5 I hate and hate and hate. So angry. Die!
# 3 4 Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.
关于r - R中的Score Sentiment函数,始终返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35222946/