编辑维德_词典.txt在nltk for python中添加与我的域相关的单词

2024-10-02 00:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用vader中的nltk来查找文件中每一行的情感。我有两个问题:

  1. 我需要在vader_lexicon.txt中添加单词,但是其语法如下:

assaults -2.5 0.92195 [-1, -3, -3, -3, -4, -3, -1, -2, -2, -3]

-2.5和{}代表什么?在

我该如何为新单词编码?假设我必须添加类似'100%''A1'之类的内容。在

  1. 我还可以在nltk_data\corpora\opinion_lexicon文件夹中看到正反两个单词txt。这些是如何利用的?我可以在这些txt文件中添加我的文字吗?在

Tags: txt内容编码dataa1语法代表单词
1条回答
网友
1楼 · 发布于 2024-10-02 00:31:18

我相信维德在对文本进行分类时只使用单词和第一个值。如果要添加新词,只需创建一个单词词典及其情感值,可以使用update函数添加:

from nltk.sentiment.vader import SentimentIntensityAnalyzer

Analyzer = SentimentIntensityAnalyser()
Analyzer.lexicon.update(your_dictionary)

您可以根据感知到的情绪强度手动为单词指定情绪值,或者如果这不可行,则可以为两个类别指定一个广泛的值(例如-1.5和1.5)。在

您可以使用此脚本(不是我的脚本)来检查是否包含了您的更新:

^{pr2}$

在更新维德之前:

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}

相关问题 更多 >

    热门问题