2016-02-05 44 views
1

我有一个(可能)愚蠢的问题与score.sentiment 我想用3个默认短语使用此函数,问题是函数返回得分0.0.0,但它应该返回2.-5.4 我不明白这个问题,因为RGUI不会给我错误,我正在学习一个教程!R得分情绪函数,总是返回0

我dowloaded阴性和阳性词列表与

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) 

感谢您的建议

回答

2

对我的作品

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. 
+0

谢谢!有用。差异似乎只在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'); 我在哪里使用扫描和你的Readlines和缺少什么='字符',comment.char =';' 有一个原因? – JEricaM

+0

另一个区别是URL中的“dl = 1”而不是“dl = 0”。除此之外,'readLines'和'scan'应该产生相同的结果。 – lukeA

+0

哦对!我错过了这个细节! – JEricaM