6

有没有办法在 NLTK 或其他 python 库中获得与给定副词相对应的形容词。例如,对于副词“可怕”,我需要得到“可怕”。谢谢。

4

2 回答 2

12

wordnet 中有一个关系连接adjectivesadverbs,反之亦然。

>>> from itertools import chain
>>> from nltk.corpus import wordnet as wn
>>> from difflib import get_close_matches as gcm
>>> possible_adjectives = [k.name for k in chain(*[j.pertainyms() for j in chain(*[i.lemmas for i in wn.synsets('terribly')])])]
['terrible', 'atrocious', 'awful', 'rotten']
>>> gcm('terribly',possible_adjectives)
['terrible']

一种更具人类可读性的计算possible_adjective方式如下:

possible_adj = []
for ss in wn.synsets('terribly'):
  for lemmas in ss.lemmas: # all possible lemmas.
    for lemma in lemmas: 
      for ps in lemma.pertainyms(): # all possible pertainyms.
        for p in ps:
          for ln in p.name: # all possible lemma names.
            possible_adj.append(ln)

编辑:在新版本的 NLTK 中:

possible_adj = []
for ss in wn.synsets('terribly'):
  for lemmas in ss.lemmas(): # all possible lemmas
      for ps in lemmas.pertainyms(): # all possible pertainyms
          possible_adj.append(ps.name())
于 2013-06-24T15:30:00.557 回答
1

正如 MKoosej 所提到的,nltk 的引理不再是一个属性,而是一个方法。我还做了一点简化以获得最可能的词。希望其他人也可以使用它:

wordtoinv = 'unduly'
s = []
winner = ""
for ss in wn.synsets(wordtoinv):
    for lemmas in ss.lemmas(): # all possible lemmas.
        s.append(lemmas)

for pers in s:
    posword = pers.pertainyms()[0].name()
    if posword[0:3] == wordtoinv[0:3]:
        winner = posword
        break

print winner # undue
于 2016-01-25T00:10:15.347 回答