簡體   English   中英

在使用nltk時,如何操作nltk.corpus.reader.wordnet.Synset?

[英]While working on nltk, how to manipulate the nltk.corpus.reader.wordnet.Synset?

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') 當我嘗試使用sendi wordnet時

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

這將輸出“ kill”的正負分數。

我的問題是-如何在代碼片段2中利用意義(來自代碼片段1)? 當我嘗試直接使用它時,它引發了此錯誤引理,pos,synset_index_str = name.lower()。rsplit('。',, 2)AttributeError:'Synset'對象沒有屬性'lower'

可以使用Synset對象中的get函數返回Synset屬性,例如

>> 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的索引,請使用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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM