Python 在字典中对维德情绪分析结果进行排序
字典中是否有一种常用的方法对多个维德情绪分析结果进行排序 我试图根据评论词典中的“复合”维德情绪分析结果进行排序。Python 在字典中对维德情绪分析结果进行排序,python,pandas,dictionary,nlp,nltk,Python,Pandas,Dictionary,Nlp,Nltk,字典中是否有一种常用的方法对多个维德情绪分析结果进行排序 我试图根据评论词典中的“复合”维德情绪分析结果进行排序。 s = pprint.pformat(review) print(s) 我刚开始学习nlp和情绪分析,到目前为止,我的第一个项目有95%是在这里学习书籍、教程和文章 我希望在一大组多词典结果中,将最接近的分数5个总体情绪分数排序到一个特定的分数 这就是我尝试的 也可能是因为打印它是一个字符串,我想我需要将其转换为字符串或对象,只是不确定下一步。我想我可能需要循环浏览字典或转换字典
s = pprint.pformat(review)
print(s)
我刚开始学习nlp和情绪分析,到目前为止,我的第一个项目有95%是在这里学习书籍、教程和文章
我希望在一大组多词典结果中,将最接近的分数5个总体情绪分数排序到一个特定的分数
这就是我尝试的
也可能是因为打印它是一个字符串,我想我需要将其转换为字符串或对象,只是不确定下一步。我想我可能需要循环浏览字典或转换字典。任何指点都将不胜感激
newlist = sorted(review, key=lambda k: k['compound'])
我也试过了
from operator import itemgetter
newlist = sorted(review, key=itemgetter('compound'))
维德结果是从字典中打印出来的。
s = pprint.pformat(review)
print(s)
这是我认为是标准输出的结果格式
'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',
直接排序不起作用,因为您的字典值是字符串,而不是字典或列表。要按化合物排序,首先需要提取它的值。下面是一个简单的示例,说明如何使用regex和lambda执行此操作:
重新导入
def萃取剂(项目):
#定义正则表达式以提取复合值
rgxp=r'(?谢谢,我遇到了一个错误,但我假设我只需要找出正则表达式模式就可以了。review.items(),key=lambda item:extract_component(item[1]),reverse=True)^SyntaxError:parsingRegex应该可以正常工作时出现意外的EOF,我用你发布的数据作为字典进行了测试。但是,您的完整数据中可能存在不同的情况和模式,因此在这种情况下,您需要相应地更改正则表达式或添加额外的检查来验证数据,例如,如果没有值并进行处理。再次感谢,哦,是的,没有值很可能我还没有处理它,我将首先填充nan值,然后重试