如何使用sentiwordnet获取同义词并计算他们的情绪分数

时间:2015-06-14 13:04:37

标签: python-3.x

# Get the content from a file
Get-Content 'C:\path_to_your_file.txt' | 
# Get replace each line if it matches the pattern
ForEach-Object {$_ -replace "(public interface (\S+) extends InterfaceName)", ' $1<$2>' } |
# Save the changes back to the file
Set-Content 'C:\path_to_your_file.txt'

对于python 3.4.3中的这段代码我输出为:

import nltk
from nltk.corpus import sentiwordnet as swn,SentiSynset
swn.senti_synsets('slow')

但它应该是这样的:

<filter object at 0x0806DE70>

我真的很抱歉,如果我的问题模糊或愚蠢,但我是python和nltk的新手并没有得到这个。我怎样才能使用sentiwordnet获得这些同义词的情感分数。

2 个答案:

答案 0 :(得分:2)

您使用的是python3。在python3 过滤器函数中,返回过滤器对象而不是列表。

senti_synsets方法在nltk中定义如下。

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

由于你使用的是python3,senti_synsets方法会返回一个python过滤器对象。

您可以将该过滤器对象转换为列表。

 synsets=list(swn.senti_synsets('slow'))
 synsets

输出

[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')]

答案 1 :(得分:0)

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)

更好地获得平均值。