python - 将形容词转换为副词

标签 python nlp nltk wordnet spacy

有谁知道如何将英语形容词转换成相应的副词? Python 是理想的选择,但实际上任何编程方法都会很棒。

我试过了 pattern.en , nltk wordnet , 和 spacy无济于事。

将副词转换为其词根形容词形式没有问题。我正在使用 SO 解决方案 here .

我想要的是走另一条路。从形容词到副词。

Here is nltk wordnet code that kind of converts words between different word forms,但形容词<-->副词转换失败。

具体来说,我想要一个像这样的函数 getAdverb:

getAdverb('quick')
>>> quickly
getAdverb('noteable')
>>> notably
getAdverb('happy')
>>> happily

如有任何代码、资源或建议,我们将不胜感激!

最佳答案

想法

让我们获取预训练的词嵌入并使用 word vector arithmetic properties获取与我们的目标词语义相似的词集,然后选择最有希望的词:

word2vec

但我们将尝试利用形容词-副词关系。

代码

首先,您需要下载词嵌入。我通常取GloVe来自斯坦福。然后你需要将 GloVe 文本格式转换为 Gensim:

$ python -m gensim.scripts.glove2word2vec -i glove.6B.100d.txt -o glove-word2vec.6B.100d.txt
2018-01-13 09:54:04,133 : MainThread : INFO : running /usr/lib/python2.7/site-packages/gensim/scripts/glove2word2vec.py -i glove.6B.100d.txt -o glove-word2vec.6B.100d.txt
2018-01-13 09:54:04,248 : MainThread : INFO : converting 400000 vectors from glove.6B.100d.txt to glove-word2vec.6B.100d.txt
2018-01-13 09:54:04,622 : MainThread : INFO : Converted model with 400000 vectors and 100 dimensions

之后加载就相当简单了:

from gensim.models.keyedvectors import KeyedVectors
glove_filename = '../../_data/nlp/glove/glove-word2vec.6B.100d.txt'
model = KeyedVectors.load_word2vec_format(glove_filename, binary=False)
print(model.most_similar(positive=['woman', 'king'], negative=['man']))

这个测试应该输出语义相似的单词给 womankingman:

(u'queen', 0.7698541283607483)
(u'monarch', 0.6843380928039551)
(u'throne', 0.6755735874176025) 
(u'daughter', 0.6594556570053101)
(u'princess', 0.6520534753799438)

最后,这是我们如何导航到最近的副词:

from difflib import SequenceMatcher

def close_adv(input, num=5, model_topn=50):
  positive = [input, 'happily']
  negative = [       'happy']
  all_similar = model.most_similar(positive, negative, topn=model_topn)

  def score(candidate):
    ratio = SequenceMatcher(None, candidate, input).ratio()
    looks_like_adv = 1.0 if candidate.endswith('ly') else 0.0
    return ratio + looks_like_adv

  close = sorted([(word, score(word)) for word, _ in all_similar], key=lambda x: -x[1])
  return close[:num]

print(close_adv('strong'))
print(close_adv('notable'))
print(close_adv('high'))
print(close_adv('quick'))
print(close_adv('terrible'))
print(close_adv('quiet'))

结果并不理想,但看起来很有希望:

[(u'strongly', 1.8571428571428572), (u'slowly', 1.3333333333333333), (u'increasingly', 1.3333333333333333), (u'sharply', 1.3076923076923077), (u'largely', 1.3076923076923077)]
[(u'notably', 1.8571428571428572), (u'principally', 1.3333333333333333), (u'primarily', 1.25), (u'prominently', 1.2222222222222223), (u'chiefly', 1.1428571428571428)]
[(u'rapidly', 1.1818181818181819), (u'briefly', 1.1818181818181819), (u'steadily', 1.1666666666666667), (u'dangerously', 1.1333333333333333), (u'continuously', 1.125)]
[(u'quickly', 1.8333333333333335), (u'quietly', 1.5), (u'briskly', 1.3333333333333333), (u'furiously', 1.2857142857142856), (u'furtively', 1.2857142857142856)]
[(u'horribly', 1.625), (u'heroically', 1.4444444444444444), (u'silently', 1.375), (u'uncontrollably', 1.3636363636363638), (u'stoically', 1.3529411764705883)]
[(u'quietly', 1.8333333333333335), (u'silently', 1.4615384615384617), (u'patiently', 1.4285714285714286), (u'discreetly', 1.4), (u'fitfully', 1.3076923076923077)]

当然,您可以继续使用更好的方法来检查副词,使用 nltk.edit_distance 来衡量单词相似度等等。所以这只是一个想法,它是一种概率,但我觉得它很有趣。

关于python - 将形容词转换为副词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233179/

相关文章:

python - Python 中带有词干的 UnicodeDecodeError

python - 访问 pandas 数据框中的列时出现问题

python - 卡住 Python 应用程序时创建更少的文件

python - BeautifulSoup,你要把我的 HTML 放在哪里?

python - tensorflow : ValueError: Shape must be rank 2 but is rank 3

python - 情感分析 Python 标记化

python - 任何可以在 python/nltk 中将阿拉伯语翻译成英语的模块?

python - 使用python快速抓取动态内容

python - NLTK 词干提取不通过简单的案例

python - 如何将用户输入的字符串转换为正确的对象类型