把形容词改成副词

2024-09-30 22:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何把英语形容词转换成相应的副词吗?Python将是理想的,但实际上任何编程方法都是很好的。在

我试过pattern.ennltk wordnet、和{a3}都没有用。在

把副词转换成形容词的词根形式不是问题。我使用的是SO解决方案here。在

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

Here is nltk wordnet code这种方法可以在不同的词形之间转换单词,但对于形容词和副词的转换则失败。在

具体来说,我希望函数getAdverb如下所示:

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

任何代码,资源,或建议将不胜感激!在


Tags: 方法so编程解决方案wordneta3形式en
1条回答
网友
1楼 · 发布于 2024-09-30 22:22:46

想法

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

word2vec

但我们将尝试利用形容词与副词的关系。在

代码

首先,您需要下载单词embeddings。我通常从斯坦福大学毕业。然后您需要将手套文本格式转换为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

之后装载相当容易:

^{pr2}$

此测试应将语义相似的单词输出到woman,将类似的king输出到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来衡量单词的相似性等等。所以这只是一个想法,有点概率性,但我觉得很有趣。在

相关问题 更多 >