Skip to main content
 首页 » 编程设计

python之从名词 Wordnet python 获取动词

2025年02月15日7java哥

我正在尝试使用 Python 中的 Wordnet 从名词中获取动词。 这是代码:

nouns = ["slip", "frustration"] 
def nominalization(noun_word): 
   set_of_related_verbs = set() 
 
   for lemma in wn.lemmas(wn.morphy(noun_word, wn.NOUN), pos="n"): 
      for related_form in lemma.derivationally_related_forms(): 
        set_of_related_verbs.add(related_form) 
   return set_of_related_verbs 
 
for word in nouns: 
    set_of_words = nominalization(word) 
    if len(set_of_words) >= 1: 
        print word + "+++" 
        print set_of_words 

我只需要动词,但我也得到了其他词,例如: 滑+++ 设置([Lemma('slippery.a.01.slippy'), Lemma('err.v.01.slip')])

我怎样才能改变这个?

如果我使用这条线:

for synset in wn.synsets(related_form.name(), pos=wn.VERB): 

我只得到动词,但有些动词可能与我的名词无关,例如: 获取+++ set([Synset('get.v.01'), Synset('assume.v.03'), Synset('grow .v.08'), Synset('acquire.v.04'), Synset('acquire.v.05'), Synset('develop.v.03'), Synset(' 学习.v.01')])

请您参考如下方法:

您可以推导出词条和相关词形式,您可以从中提取动词:

import nltk as nltk 
from nltk.corpus import wordnet as wn 
 
lem = wn.lemmas('frustration') 
print lem 

结果:

[Lemma('frustration.n.01.frustration'), Lemma('frustration.n.02.frustration'), Lemma('frustration.n.03.frustration')] 

从中你可以得到这样的动词:

related_forms = [lem[i].derivationally_related_forms() for i in range(len(lem))] 
print related_forms 

结果:

[[Lemma('thwart.v.01.frustrate')], [Lemma('thwart.v.01.frustrate')], [Lemma('thwart.v.01.frustrate'), Lemma('torment.v.02.frustrate')]]