首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“词典”中Vader情感分析结果的排序

“词典”中Vader情感分析结果的排序
EN

Stack Overflow用户
提问于 2020-06-05 15:05:34
回答 1查看 186关注 0票数 0

是否有一种常用的方法来对字典中的多个Vader情感分析结果进行排序

我正在尝试按“复合”Vader情感分析结果在“评论词典”中进行排序。

我刚开始学习nlp和情感分析,到目前为止,我的第一个项目95%都是学习书籍、教程和这里。

我希望排序,最近的分数,5个整体情感分数,,在一大组多字典的结果中特定的一个。

我尝试的可能也是因为打印它是一个字符串,我想我需要把它转换成字符串或对象,只是不确定接下来的步骤,我认为我可能需要遍历字典或转换字典。任何指示都将不胜感激。

代码语言:javascript
复制
newlist = sorted(review, key=lambda k: k['compound']) 

我也试过

代码语言:javascript
复制
from operator import itemgetter
newlist = sorted(review, key=itemgetter('compound'))

Vader结果是从字典.中打印出来的

代码语言:javascript
复制
s = pprint.pformat(review)
print(s)

--这是结果的格式,我认为这是标准输出

代码语言:javascript
复制
 'america-reviews': "Overall sentiment dictionary is : {'neg': 0.051, 'neu': "
                    "0.632, 'pos': 0.316, 'compound': 1.0}, sentence was rated "
                    'as 5.1% Negative, sentence was rated as 63.2% Neutral, '
                    'sentence was rated as 31.6% Positive, Sentence Overall '
                    'Rated As Positive',
 'amygrant-reviews': "Overall sentiment dictionary is : {'neg': 0.022, 'neu': "
                     "0.734, 'pos': 0.244, 'compound': 0.9998}, sentence was "
                     'rated as 2.1999999999999997% Negative, sentence was '
                     'rated as 73.4% Neutral, sentence was rated as 24.4% '
                     'Positive, Sentence Overall Rated As Positive',
 'andygarcia-reviews': "Overall sentiment dictionary is : {'neg': 0.0, 'neu': "
                       "0.955, 'pos': 0.045, 'compound': 0.8419}, sentence was "
                       'rated as 0.0% Negative, sentence was rated as 95.5% '
                       'Neutral, sentence was rated as 4.5% Positive, Sentence '
                       'Overall Rated As Positive',
 'annemurray-reviews': "Overall sentiment dictionary is : {'neg': 0.02, 'neu': "
                       "0.769, 'pos': 0.211, 'compound': 0.9986}, sentence was "
                       'rated as 2.0% Negative, sentence was rated as 76.9% '
                       'Neutral, sentence was rated as 21.099999999999998% '
                       'Positive, Sentence Overall Rated As Positive',
 'annielennox-reviews': "Overall sentiment dictionary is : {'neg': 0.03, "
                        "'neu': 0.717, 'pos': 0.254, 'compound': 0.9999}, "
                        'sentence was rated as 3.0% Negative, sentence was '
                        'rated as 71.7% Neutral, sentence was rated as 25.4% '
                        'Positive, Sentence Overall Rated As Positive',
 'artgarfunkel-reviews': "Overall sentiment dictionary is : {'neg': 0.056, "
                         "'neu': 0.642, 'pos': 0.302, 'compound': 1.0}, "
                         'sentence was rated as 5.6000000000000005% Negative, '
                         'sentence was rated as 64.2% Neutral, sentence was '
                         'rated as 30.2% Positive, Sentence Overall Rated As '
                         'Positive',
 'bangles-reviews': "Overall sentiment dictionary is : {'neg': 0.054, 'neu': "
                    "0.733, 'pos': 0.213, 'compound': 0.9998}, sentence was "
                    'rated as 5.4% Negative, sentence was rated as 73.3% '
                    'Neutral, sentence was rated as 21.3% Positive, Sentence '
                    'Overall Rated As Positive',
 'barbrastriesand-reviews': "Overall sentiment dictionary is : {'neg': 0.014, "
                            "'neu': 0.815, 'pos': 0.171, 'compound': 0.9982}, "
                            'sentence was rated as 1.4000000000000001% '
                            'Negative, sentence was rated as 81.5% Neutral, '
                            'sentence was rated as 17.1% Positive, Sentence '
                            'Overall Rated As Positive',
 'barrymanilow-reviews': "Overall sentiment dictionary is : {'neg': 0.041, "
                         "'neu': 0.647, 'pos': 0.313, 'compound': 1.0}, "
                         'sentence was rated as 4.1000000000000005% Negative, '
                         'sentence was rated as 64.7% Neutral, sentence was '
                         'rated as 31.3% Positive, Sentence Overall Rated As '
                         'Positive',
 'beachboys-reviews': "Overall sentiment dictionary is : {'neg': 0.016, 'neu': "
                      "0.906, 'pos': 0.078, 'compound': 0.945}, sentence was "
                      'rated as 1.6% Negative, sentence was rated as '
                      '90.60000000000001% Neutral, sentence was rated as 7.8% '
                      'Positive, Sentence Overall Rated As Positive',
 'belindacarlisle-reviews': "Overall sentiment dictionary is : {'neg': 0.046, "
                            "'neu': 0.756, 'pos': 0.197, 'compound': 0.9987}, "
                            'sentence was rated as 4.6% Negative, sentence was '
                            'rated as 75.6% Neutral, sentence was rated as '
                            '19.7% Positive, Sentence Overall Rated As '
                            'Positive',
 'bernadettepeters-reviews': "Overall sentiment dictionary is : {'neg': 0.02, "
                             "'neu': 0.753, 'pos': 0.227, 'compound': 0.9992}, "
                             'sentence was rated as 2.0% Negative, sentence '
                             'was rated as 75.3% Neutral, sentence was rated '
                             'as 22.7% Positive, Sentence Overall Rated As '
                             'Positive',
 'bethhart-reviews': "Overall sentiment dictionary is : {'neg': 0.041, 'neu': "
                     "0.592, 'pos': 0.366, 'compound': 1.0}, sentence was "
                     'rated as 4.1000000000000005% Negative, sentence was '
                     'rated as 59.199999999999996% Neutral, sentence was rated '
                     'as 36.6% Positive, Sentence Overall Rated As Positive',
 'bettemidler-reviews': "Overall sentiment dictionary is : {'neg': 0.043, "
                        "'neu': 0.635, 'pos': 0.322, 'compound': 0.9999}, "
                        'sentence was rated as 4.3% Negative, sentence was '
                        'rated as 63.5% Neutral, sentence was rated as 32.2% '
                        'Positive, Sentence Overall Rated As Positive',
 'bjork-reviews': "Overall sentiment dictionary is : {'neg': 0.042, 'neu': "
                  "0.696, 'pos': 0.262, 'compound': 1.0}, sentence was rated "
                  'as 4.2% Negative, sentence was rated as 69.6% Neutral, '
                  'sentence was rated as 26.200000000000003% Positive, '
                  'Sentence Overall Rated As Positive',
 'bluemangroup-reviews': "Overall sentiment dictionary is : {'neg': 0.047, "
                         "'neu': 0.726, 'pos': 0.227, 'compound': 0.9999}, "
                         'sentence was rated as 4.7% Negative, sentence was '
                         'rated as 72.6% Neutral, sentence was rated as 22.7% '
                         'Positive, Sentence Overall Rated As Positive',
 'bluetravelers-reviews': "Overall sentiment dictionary is : {'neg': 0.0, "
                          "'neu': 0.914, 'pos': 0.086, 'compound': 0.9455}, "
                          'sentence was rated as 0.0% Negative, sentence was '
                          'rated as 91.4% Neutral, sentence was rated as 8.6% '
                          'Positive, Sentence Overall Rated As Positive',
 'bobbyvinton-reviews': "Overall sentiment dictionary is : {'neg': 0.0, 'neu': "
                        "0.928, 'pos': 0.072, 'compound': 0.9501}, sentence "
                        'was rated as 0.0% Negative, sentence was rated as '
                        '92.80000000000001% Neutral, sentence was rated as '
                        '7.199999999999999% Positive, Sentence Overall Rated '
                        'As Positive',
 'bonnieRatt-reviews': "Overall sentiment dictionary is : {'neg': 0.034, "
                       "'neu': 0.612, 'pos': 0.354, 'compound': 0.9999}, "
                       'sentence was rated as 3.4000000000000004% Negative, '
                       'sentence was rated as 61.199999999999996% Neutral, '
                       'sentence was rated as 35.4% Positive, Sentence Overall '
                       'Rated As Positive',
 'boygeorge-reviews': "Overall sentiment dictionary is : {'neg': 0.039, 'neu': "
                      "0.884, 'pos': 0.076, 'compound': 0.9217}, sentence was "
                      'rated as 3.9% Negative, sentence was rated as 88.4% '
                      'Neutral, sentence was rated as 7.6% Positive, Sentence '
                      'Overall Rated As Positive',
 'brianlittrell-reviews': "Overall sentiment dictionary is : {'neg': 0.052, "
                          "'neu': 0.873, 'pos': 0.074, 'compound': 0.8203}, "
                          'sentence was rated as 5.2% Negative, sentence was '
                          'rated as 87.3% Neutral, sentence was rated as '
                          '7.3999999999999995% Positive, Sentence Overall '
                          'Rated As Positive',
 'briansetzerorchestra-reviews': "Overall sentiment dictionary is : {'neg': "
                                 "0.046, 'neu': 0.646, 'pos': 0.308, "
                                 "'compound': 1.0}, sentence was rated as 4.6% "
                                 'Negative, sentence was rated as '
                                 '64.60000000000001% Neutral, sentence was '
                                 'rated as 30.8% Positive, Sentence Overall '
                                 'Rated As Positive',
 'brianwilson-reviews': "Overall sentiment dictionary is : {'neg': 0.042, "
                        "'neu': 0.647, 'pos': 0.311, 'compound': 1.0}, "
                        'sentence was rated as 4.2% Negative, sentence was '
                        'rated as 64.7% Neutral, sentence was rated as 31.1% '
                        'Positive, Sentence Overall Rated As Positive',
 'brucehornsby-reviews': "Overall sentiment dictionary is : {'neg': 0.0, "
                         "'neu': 0.928, 'pos': 0.072, 'compound': 0.9196}, "
                         'sentence was rated as 0.0% Negative, sentence was '
                         'rated as 92.80000000000001% Neutral, sentence was '
                         'rated as 7.199999999999999% Positive, Sentence '
                         'Overall Rated As Positive',
 'bryanadams-reviews': "Overall sentiment dictionary is : {'neg': 0.008, "
                       "'neu': 0.933, 'pos': 0.059, 'compound': 0.9028}, "
                       'sentence was rated as 0.8% Negative, sentence was '
                       'rated as 93.30000000000001% Neutral, sentence was '
                       'rated as 5.8999999999999995% Positive, Sentence '
                       'Overall Rated As Positive',
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-05 18:09:28

直接排序不起作用,因为字典值是字符串,而不是字典或列表。要按化合物排序,首先需要提取它的值。下面是一个简单的示例,说明如何使用regex和lambda进行操作:

代码语言:javascript
复制
import re


def extract_compound(item):
    # define regex to extract compound value
    rgxp = r'(?<=\'compound\':\s)\d\.\d+'
    compound = re.search(rgxp, item)
    compound = compound.group(0)

    return float(compound)


review_sorted = {key: value for key, value in sorted(
    review.items(), key=lambda item: extract_compound(item[1]), reverse=True)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62218478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档