从 nltk 或其他 NLP 库中的副词获取形容词 [英] Getting adjective from an adverb in nltk or other NLP library

查看:51
本文介绍了从 nltk 或其他 NLP 库中的副词获取形容词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

wordnet 中有一种关系,可以将 形容词副词 连接起来,反之亦然.

<预><代码>>>>从 itertools 导入链>>>从 nltk.corpus 导入 wordnet as wn>>>从 difflib 导入 get_close_matches 作为 gcm>>>possible_adjectives = [k.name for k in chain(*[j.pertainyms() for j in chain(*[i.lemmas for i in wn.synsets('terribly')])])]['可怕的','残暴的','可怕的','腐烂的']>>>gcm('可怕',possible_adjectives)['糟糕的']

一种更易读的计算方式possible_adjective如下:

possible_adj = []对于 wn.synsets('terribly') 中的 ss:对于 ss.lemmas 中的引理:# 所有可能的引理.对于引理中的引理:对于 lemma.pertainyms() 中的 ps: # 所有可能的 pertainyms.对于 ps 中的 p:for ln in p.name: # 所有可能的引理名称.possible_adj.append(ln)

在较新版本的 NLTK 中:

possible_adj = []对于 wn.synsets('terribly') 中的 ss:对于 ss.lemmas() 中的引理:# 所有可能的引理对于 lemmas.pertainyms() 中的 ps: # 所有可能的 pertainymspossible_adj.append(ps.name())

Is there a way to get an adjective corresponding to a given adverb in NLTK or other python library. For example, for the adverb "terribly", I need to get "terrible". Thanks.

解决方案

There is a relation in wordnet that connects the adjectives to adverbs and vice versa.

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

A more human readable way to computepossible_adjective is as followed:

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)

EDIT: In the newer version of 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())

这篇关于从 nltk 或其他 NLP 库中的副词获取形容词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆