小编典典

从nltk或其他NLP库中的副词获取形容词

python

有没有一种方法可以获取与NLTK或其他python库中给定副词相对应的形容词。例如,对于副词“ 非常可怕 ”,我需要变得“ 可怕 ”。谢谢。


阅读 210

收藏
2021-01-16

共1个答案

小编典典

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())
2021-01-16