使用nltk进行名义化

时间:2014-04-04 04:48:43

标签: python nltk

我的任务是将任何动词转换为适当的名词形式。

例如:改善 - 改善      认可 - 认可

等等......

我尝试了这个但是我收到了错误

我的代码:

import nltk.app.wordnet_app as nwapp
from nltk.corpus import wordnet as wn

word = "recognize"
print("Nominalizing " + word)
verb_synsets = wn.synsets(word, pos=wn.VERB)
print nwapp.get_relations_data(word,verb_synsets)

我的错误:

Nominalizing recognize
Traceback (most recent call last):
  File "nominalizeme.py", line 8, in <module>
    print nwapp.get_relations_data(word,
verb_synsets)
  File "/usr/lib/python2.7/dist-packages/nltk/app/wordnet_app.py", line 412, in get_relations_data
    if synset.pos == wn.NOUN:
AttributeError: 'list' object has no attribute 'pos'

1 个答案:

答案 0 :(得分:1)

get_relations_data()一次只能获取一个synset。您的verb_synsets是一个同义词列表,请参阅http://www.nltk.org/_modules/nltk/app/wordnet_app.html

>>> import nltk.app.wordnet_app as wnapp
>>> from nltk.corpus import wordnet as wn
>>> word = "recognize"
>>> verb_synsets = wn.synsets(word, pos=wn.VERB)
>>> verb_synsets
[Synset('acknowledge.v.06'), Synset('recognize.v.02'), Synset('spot.v.02'), Synset('recognize.v.04'), Synset('accredit.v.01'), Synset('greet.v.01'), Synset('acknowledge.v.04'), Synset('recognize.v.08'), Synset('recognize.v.09')]
>>> print wnapp.get_relations_data(word, verb_synsets[0])
((18, 'Antonym', []), (0, 'Hyponym', []), (1, 'Direct hypernyms', [Synset('accept.v.01')]), (26, 'Indirect hypernyms', [(Synset('accept.v.01'), [(Synset('evaluate.v.02'), [(Synset('think.v.03'), [])])])]), (17, 'Entailments', []), (14, 'Causes', []), (15, 'Also see', []), (10, 'Verb Groups', []), (25, 'Derivationally related form', []))