如何在python中使用NLTK列出单词的所有形式 [英] How to list all the forms of a word using NLTK in python

查看:549
本文介绍了如何在python中使用NLTK列出单词的所有形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python中的NLTK库列出单词的所有形式(动词,名词,比较词,最高级词,形容词和副词).例如,如果我有"write"一词,则结果应为:writing writer writer writer等.冷然后更冷,最冷.快速:快速等.有没有办法做到这一点?

I need to list all the forms (verb , noun, comparative, superlative, adjective, and adverb) of a word using NLTK library in python . For example if I have the word "write" the result should be: wrote writing writer written etc..., also if the word can be written in comparative and superlative form e.g; cold then colder, coldest. And quick : quickly etc. Is there a way to do that?

推荐答案

这是我的最新答案.希望这对您有所帮助.我只是对其进行了一些改进,并进行了一些小的调试以适应新的nltk版本.原始代码可在George-Bogdan Ivanov的答案中找到,在动词/名词/形容词形式

Hi this is my late answer. Hope this still help. I just improve it a little and some small debugging to fit new nltk version. The original code can be found in George-Bogdan Ivanov's answer here Convert words between verb/noun/adjective forms

from nltk.corpus import wordnet as wn

def morphify(word,org_pos,target_pos):
    """ morph a word """
    synsets = wn.synsets(word, pos=org_pos)

    # Word not found
    if not synsets:
        return []

    # Get all  lemmas of the word
    lemmas = [l for s in synsets \
                   for l in s.lemmas() if s.name().split('.')[1] == org_pos]

    # Get related forms
    derivationally_related_forms = [(l, l.derivationally_related_forms()) \
                                    for l in    lemmas]

    # filter only the targeted pos
    related_lemmas = [l for drf in derivationally_related_forms \
                           for l in drf[1] if l.synset().name().split('.')[1] == target_pos]

    # Extract the words from the lemmas
    words = [l.name() for l in related_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

print morphify('sadness','n','v')

这篇关于如何在python中使用NLTK列出单词的所有形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆