我是靠谱客的博主 温柔小蚂蚁,这篇文章主要介绍python语言单词_python – 在动词/名词/形容词之间转换单词,现在分享给大家,希望可以做个参考。

这更是一种启发式的方法。我已经编码了它的风格。它使用来自wordnet的derivationally_related_forms()。我已经实施了名词我猜动词的作用类似。从我所测试的工作相当不错:

from nltk.corpus import wordnet as wn

def nounify(verb_word):

""" Transform a verb to the closest noun: die -> death """

verb_synsets = wn.synsets(verb_word, pos="v")

# Word not found

if not verb_synsets:

return []

# Get all verb lemmas of the word

verb_lemmas = [l for s in verb_synsets

for l in s.lemmas if s.name.split('.')[1] == 'v']

# Get related forms

derivationally_related_forms = [(l, l.derivationally_related_forms())

for l in verb_lemmas]

# filter only the nouns

related_noun_lemmas = [l for drf in derivationally_related_forms

for l in drf[1] if l.synset.name.split('.')[1] == 'n']

# Extract the words from the lemmas

words = [l.name for l in related_noun_lemmas]

len_words = len(words)

# Build the result in the form of a list containing tuples (word, probability)

result = [(w, float(words.count(w))/len_words) for w in set(words)]

result.sort(key=lambda w: -w[1])

# return all the possibilities sorted by probability

return result

最后

以上就是温柔小蚂蚁最近收集整理的关于python语言单词_python – 在动词/名词/形容词之间转换单词的全部内容,更多相关python语言单词_python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部