2017-06-05 31 views
1

我對青澀的分析,使用R包 「情」錯誤情感分析,使用R包 「情」

neg_words =掃描(「C工作。 txt「,what ='character',comment.char =';')

pos_words = scan(」C:/ Users/kothasan/Desktop/Sentiment Analysis/pos.words.txt「,what ='character' ,comment.char = ';')

score.sentiment =函數(句子,pos.words,neg.words,.progress = '無')

{

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 

1.我很舒服的服務

  • 問題沒有得到解決
  • 使用

    功能:

    分析= score.sentiment (sample,pos_words,neg_words)

    當我運行上面的函數時,我得到了下面的警告與輸出得分爲0,0這是錯誤的:

    警告消息: 在data.frame(得分=分數,文本=句子):從短變發現 行的名稱和已被丟棄

    輸出:

    比分文字

    1. 0我很舒服你的服務

    2. 0問題沒有得到解決

    我得到正確的分數時,我只用一排作爲輸入。

    有人能幫我解決嗎?

    感謝,

    桑迪普

    回答

    1

    你能請檢查是否要執行senti分析是一種矢量文本列。 示例:如果數據集有2列:編號和文本。你想對文字進行情感分析。因此,在運行代碼之前,使其像句子=數據集$文本一樣。現在在代碼中使用這些句子。