编程语言
首页 > 编程语言> > python – 将形容词转换为副词

python – 将形容词转换为副词

作者:互联网

有谁知道如何将英文形容词转换为各自的副词? Python是理想的,但实际上任何程序化方法都会很棒.

我试过pattern.en,nltk wordnetspacy无济于事.

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

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

Here is nltk wordnet code那种在不同的单词形式之间转换单词,但是对于形容词< - >副词转换.

具体来说,我喜欢这样的函数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']))

这个测试应该输出语义相似的单词给一个像男人一样的女人:

(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,nlp,nltk,wordnet,spacy
来源: https://codeday.me/bug/20190705/1389458.html