NLTK VADER lexicon Structure for sentiment analysis

VADER lexicon基本結構

$:  -1.5    0.80623 [-1, -1, -1, -1, -3, -1, -3, -1, -2, -1]
%)  -0.4    1.0198  [-1, 0, -1, 0, 0, -2, -1, 2, -1, 0]
%-) -1.5    1.43178 [-2, 0, -2, -2, -1, 2, -2, -3, -2, -3]
&-: -0.4    1.42829 [-3, -1, 0, 0, -1, -1, -1, 2, -1, 2]
&:  -0.7    0.64031 [0, -1, -1, -1, 1, -1, -1, -1, -1, -1]
( '}{' )    1.6 0.66332 [1, 2, 2, 1, 1, 2, 2, 1, 3, 1]
worriedly   -2.0    0.44721 [-2, -2, -3, -2, -2, -2, -2, -1, -2, -2]
worrier -1.8    0.6 [-2, -2, -1, -2, -1, -3, -2, -2, -1, -2]
worriers    -1.7    0.45826 [-2, -1, -2, -2, -2, -2, -1, -2, -1, -2]
worries -1.8    0.6 [-2, -2, -1, -2, -1, -2, -2, -3, -1, -2]
worriment   -1.5    0.67082 [-1, -2, -1, -1, -1, -2, -1, -3, -1, -2]
worriments  -1.9    0.7 [-2, -1, -2, -3, -1, -2, -3, -1, -2, -2]
worrisome   -1.7    0.64031 [-1, -1, -1, -2, -1, -2, -3, -2, -2, -2]

vader_lexicon.txt文件是以tab鍵分割的四列字段組成的
第一列:單詞或詞組(token)
第二列:人類情感打分的均值
第三列:它是單詞的標準偏差闷愤,假設它遵循正態(tài)分布
第四列:這是在實驗中10個人對單詞進行評分的列表。
實際代碼或情感計算不使用第3列和第4列虏缸。因此鲫懒,如果你想根據你的需求更新詞典,你可以將最后兩列留空刽辙,或者用一個隨機數(shù)和一個列表填充窥岩。

VADER 源碼如何加載lexicon?

def __init__(self, lexicon_file="sentiment/vader_lexicon.zip/vader_lexicon/vader_lexicon.txt"):
    self.lexicon_file = nltk.data.load(lexicon_file)
    self.lexicon = self.make_lex_dict()

def make_lex_dict(self):
    """
    Convert lexicon file to a dictionary
    """
    lex_dict = {}
    for line in self.lexicon_file.split('\n'):
        (word, measure) = line.strip().split('\t')[0:2]
        lex_dict[word] = float(measure)
    return lex_dict

print(self.lexicon)
{'guiltier': -2.0, 'proud': 2.1, 'freeholds': 1.0, 'madness': -1.9, 'unsecured': -1.6, 'wilco': 0.9, 'doom': -1.7, 'crazy': -1.4, '|o:': -0.9, 'faultlessness': 1.1, 'triumphs': 2.0, 'excruciatingly': -2.9, 'warsaws': -0.2, 'insecurely': -1.4, 'abusing': -2.0, 'confusions': -0.9, 'relieve': 1.5, 'futile': -1.9, 'stinkpots': -0.7, ...}

VADER lexicon更新使用

  • 預判某個詞的正負中極性
import nltk
from nltk.tokenize import word_tokenize, RegexpTokenizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer

Analyzer = SentimentIntensityAnalyzer()

sentence = 'enter your text to test'

tokenized_sentence = nltk.word_tokenize(sentence)
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]

for word in tokenized_sentence:
    if (Analyzer.polarity_scores(word)['compound']) >= 0.1:
        pos_word_list.append(word)
    elif (Analyzer.polarity_scores(word)['compound']) <= -0.1:
        neg_word_list.append(word)
    else:
        neu_word_list.append(word)                

print('Positive:',pos_word_list)
print('Neutral:',neu_word_list)
print('Negative:',neg_word_list) 
score = Analyzer.polarity_scores(sentence)
print('\nScores:', score)

對vader lexicon可以對其進行增添新領域的lexicon

  • 增加更新前
sentence = 'stocks were volatile on Tuesday due to the recent calamities in the Chinese market'

Positive: []
Neutral: ['stocks', 'were', 'volatile', 'on', 'Tuesday', 'due', 'to', 'the', 'recent', 'calamities', 'in', 'the', 'Chinese', 'markets']
Negative: []
Scores: {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
  • 增加更新后
Analyzer.lexicon.update(Financial_Lexicon)
sentence = 'stocks were volatile on Tuesday due to the recent calamities in the Chinese market'

Positive: []
Neutral: ['stocks', 'were', 'on', 'Tuesday', 'due', 'to', 'the', 'recent', 'in', 'the', 'Chinese', 'markets']
Negative: ['volatile', 'calamities']
Scores: {'neg': 0.294, 'neu': 0.706, 'pos': 0.0, 'compound': -0.6124}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市宰缤,隨后出現(xiàn)的幾起案子颂翼,更是在濱河造成了極大的恐慌,老刑警劉巖慨灭,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件朦乏,死亡現(xiàn)場離奇詭異,居然都是意外死亡氧骤,警方通過查閱死者的電腦和手機呻疹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來语淘,“玉大人诲宇,你說我怎么就攤上這事』谭” “怎么了姑蓝?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長吕粗。 經常有香客問我纺荧,道長,這世上最難降的妖魔是什么颅筋? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任宙暇,我火速辦了婚禮,結果婚禮上议泵,老公的妹妹穿的比我還像新娘占贫。我一直安慰自己,他們只是感情好先口,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布型奥。 她就那樣靜靜地躺著,像睡著了一般碉京。 火紅的嫁衣襯著肌膚如雪厢汹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天谐宙,我揣著相機與錄音烫葬,去河邊找鬼。 笑死,一個胖子當著我的面吹牛搭综,可吹牛的內容都是我干的垢箕。 我是一名探鬼主播递惋,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼到逊,長吁一口氣:“原來是場噩夢啊……” “哼抡驼!你這毒婦竟也來了性昭?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蟆盐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奋姿,經...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年素标,在試婚紗的時候發(fā)現(xiàn)自己被綠了称诗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡头遭,死狀恐怖寓免,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情计维,我是刑警寧澤袜香,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站鲫惶,受9級特大地震影響蜈首,放射性物質發(fā)生泄漏。R本人自食惡果不足惜欠母,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一欢策、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赏淌,春花似錦踩寇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缩擂,卻和暖如春鼠冕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背胯盯。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工懈费, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人博脑。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓憎乙,卻偏偏與公主長得像票罐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泞边,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內容