【问题标题】:Getting adjective from an adverb in nltk or other NLP library从 nltk 或其他 NLP 库中的副词获取形容词
【发布时间】:2013-06-19 04:02:15
【问题描述】:

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

【问题讨论】:

标签: python nlp nltk


【解决方案1】:

wordnet 中有一个关系将adjectives 连接到adverbs,反之亦然。

>>> 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']

computepossible_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())

【讨论】:

  • 您会从上面的代码中遇到多个答案,但您可以简单地使用list[0] 作为最佳答案。
  • 在较新版本的 nltk 中,引理现在是方法而不是属性
  • 对我来说,它无法将大约 600 个副词中的 350 个进行转换。
【解决方案2】:

正如 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

【讨论】:

  • 请在发布前测试代码。对于 op 询问的单词,这失败了:terribly
  • 应该添加“if len(pers.pertainyms())==0: continue”。有没有办法从形容词变成副词?
猜你喜欢
  • 2015-11-26
  • 1970-01-01
  • 2013-07-19
  • 2018-06-22
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2012-11-11
  • 2018-07-12
相关资源
最近更新 更多