2015-04-19 28 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