将形容词转换为副词

stu*_*art 5 python nlp nltk wordnet spacy

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

我尝试过pattern.ennltk wordnetspacy无济于事。

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

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

这是nltk词网代码,可以在不同的词形式之间转换词,但是对于形容词<->副词转换失败。

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

getAdverb('quick')
>>> quickly
getAdverb('noteable')
>>> notably
getAdverb('happy')
>>> happily
Run Code Online (Sandbox Code Playgroud)

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

Max*_*xim 1

主意

让我们获取预先训练的词嵌入并使用词向量算术属性来获取与目标词语相似的词集,然后选择最有希望的词:

字2向量

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

代码

首先,您需要下载词嵌入。我通常会带斯坦福大学的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
Run Code Online (Sandbox Code Playgroud)

之后加载就相当容易了:

$ 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
Run Code Online (Sandbox Code Playgroud)

该测试应该输出语义上与 a 类似的单词,woman类似于kinga man

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']))
Run Code Online (Sandbox Code Playgroud)

最后,这是我们导航到最接近的副词的方法:

(u'queen', 0.7698541283607483)
(u'monarch', 0.6843380928039551)
(u'throne', 0.6755735874176025) 
(u'daughter', 0.6594556570053101)
(u'princess', 0.6520534753799438)
Run Code Online (Sandbox Code Playgroud)

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

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'))
Run Code Online (Sandbox Code Playgroud)

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