[英]How to get synsets using sentiwordnet and calculate their sentiment score
import nltk
from nltk.corpus import sentiwordnet as swn,SentiSynset
swn.senti_synsets('slow')
for this code in python 3.4.3 i am getting output as: 对于python 3.4.3中的这段代码,我得到的输出为:
<filter object at 0x0806DE70>
But it should be like: 但这应该像:
[SentiSynset('decelerate.v.01'), SentiSynset('slow.v.02'), \
SentiSynset('slow.v.03'), SentiSynset('slow.a.01'),SentiSynset('slow.a.02'), \
SentiSynset('slow.a.04'), SentiSynset('slowly.r.01'),SentiSynset('behind.r.03')]
I am really sorry if my question is vague or silly but i am new to python and nltk and not getting this one.And how can i get the sentiment scores of these synsets using sentiwordnet. 如果我的问题含糊或愚蠢,我真的很抱歉,但是我是python和nltk的新手,却没有得到这个。我如何使用sentiwordnet获得这些同义词集的情感分数。
You are using python3 . 您正在使用python3。 In python3 filter function returns a filter object instead of list.
在python3中, 过滤器函数返回过滤器对象而不是列表。
senti_synsets method is defined in nltk like this. 像这样在nltk中定义了senti_synsets方法。
def senti_synsets(self, string, pos=None):
from nltk.corpus import wordnet as wn
sentis = []
synset_list = wn.synsets(string, pos)
for synset in synset_list:
sentis.append(self.senti_synset(synset.name()))
sentis = filter(lambda x : x, sentis)
return sentis
and since you are using python3, senti_synsets method returns a python filter object. 并且由于您使用的是python3,所以senti_synsets方法将返回一个python过滤器对象。
You can convert that filter object into list. 您可以将该过滤器对象转换为列表。
synsets=list(swn.senti_synsets('slow'))
synsets
output 输出
[SentiSynset('decelerate.v.01'),
SentiSynset('slow.v.02'),
SentiSynset('slow.v.03'),
SentiSynset('slow.a.01'),
SentiSynset('slow.a.02'),
SentiSynset('dense.s.04'),
SentiSynset('slow.a.04'),
SentiSynset('boring.s.01'),
SentiSynset('dull.s.08'),
SentiSynset('slowly.r.01'),
SentiSynset('behind.r.03')]
from nltk.corpus import sentiwordnet as swn
good = swn.senti_synsets('good', 'n')
posscore=0
negscore=0
for synst in good:
posscore=posscore+synst.pos_score()
negscore=negscore+synst.neg_score()
print(posscore)
print(negscore)
better to get an average. 最好得到一个平均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.