2015-04-19 30 views
0
import nltk 
from nltk.corpus import wordnet as wn 

synsets = wn.synsets('killed','v') 
sense=synsets[0] 

这里的感觉类型是nltk.corpus.reader.wordnet.Synset。它输出Synset('kill.v.01')。 当我尝试使用senti共发现在使用nltk时,如何操作nltk.corpus.reader.wordnet.Synset?

k = sentiwordnet.senti_synset('kill.v.01') 
print(k) 

这种输出的“杀”的正面和负面的分数。

我的问题是 - 如何在代码段2中使用sense(来自代码片段1)? 当我试图直接使用它扔这个错误 引理,POS,synset_index_str = name.lower()rsplit(2 '') AttributeError的: '同义词集' 对象没有属性 '下'

回答

5

同义词集属性可以用Synset对象中的get函数返回,例如

>> from nltk.corpus import wordnet as wn 
>>> wn.synsets('dog') 
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')] 
>>> dog = wn.synsets('dog')[0] 
>>> dog.definition() 
u'a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds' 
>>> dog.lemma_names() 
[u'dog', u'domestic_dog', u'Canis_familiaris'] 
>>> dog.pos() 
u'n' 
>>> dog.offset() 
2084071 
>>> dog.name() 
u'dog.n.01' 

如果你想保持同义词集的名字,POS和同义词集ID的索引,使用TNE synset.name()它返回一个unicode字符串:

>>> type(dog.name()) 
<type 'unicode'> 
>>> name, pos, sid = dog.name().split('.') 
>>> name 
u'dog' 
>>> pos 
u'n' 
>>> sid 
u'01' 

这些模块和变量的Synset对象可以访问:

>>> dir(dog) 
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_all_hypernyms', '_definition', '_examples', '_frame_ids', '_hypernyms', '_instance_hypernyms', '_iter_hypernym_lists', '_lemma_names', '_lemma_pointers', '_lemmas', '_lexname', '_max_depth', '_min_depth', '_name', '_needs_root', '_offset', '_pointers', '_pos', '_related', '_shortest_hypernym_paths', '_wordnet_corpus_reader', 'also_sees', 'attributes', 'causes', 'closure', 'common_hypernyms', 'definition', 'entailments', 'examples', 'frame_ids', 'hypernym_distances', 'hypernym_paths', 'hypernyms', 'hyponyms', 'instance_hypernyms', 'instance_hyponyms', 'jcn_similarity', 'lch_similarity', 'lemma_names', 'lemmas', 'lexname', 'lin_similarity', 'lowest_common_hypernyms', 'max_depth', 'member_holonyms', 'member_meronyms', 'min_depth', 'name', 'offset', 'part_holonyms', 'part_meronyms', 'path_similarity', 'pos', 'region_domains', 'res_similarity', 'root_hypernyms', 'shortest_path_distance', 'similar_tos', 'substance_holonyms', 'substance_meronyms', 'topic_domains', 'tree', 'unicode_repr', 'usage_domains', 'verb_groups', 'wup_similarity'] 
+1

救了我的命1 @alvas – modarwish