在动词/名词/形容词形式之间转换单词 [英] Convert words between verb/noun/adjective forms

查看:95
本文介绍了在动词/名词/形容词形式之间转换单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个python库函数,用于跨语音的不同部分进行翻译/转换.有时它应该输出多个单词(例如"coder"和"code"都是动词"to code"中的名词,一个是主语,另一个是宾语)

i would like a python library function that translates/converts across different parts of speech. sometimes it should output multiple words (e.g. "coder" and "code" are both nouns from the verb "to code", one's the subject the other's the object)

# :: String => List of String
print verbify('writer') # => ['write']
print nounize('written') # => ['writer']
print adjectivate('write') # => ['written']

我主要关心动词< =>名词,对于我想编写的记笔记程序.也就是说,我可以写成咖啡因拮抗A1"或咖啡因是A1拮抗剂",并且通过一些NLP可以弄清楚它们的含义相同. (我知道这并不容易,而且将需要NLP进行解析,而不仅仅是标记,但我想破解一个原型).

i mostly care about verbs <=> nouns, for a note taking program i want to write. i.e. i can write "caffeine antagonizes A1" or "caffeine is an A1 antagonist" and with some NLP it can figure out they mean the same thing. (i know that's not easy, and that it will take NLP that parses and doesn't just tag, but i want to hack up a prototype).

类似的问题... 将形容词和副词转换为名词形式 (此答案仅源于根POS.我想在POS之间进行转换.)

similar questions ... Converting adjectives and adverbs to their noun forms (this answer only stems down to the root POS. i want to go between POS.)

ps在语言学中称为转换 http://en.wikipedia.org/wiki/Conversion_ %28linguistics%29

ps called Conversion in linguistics http://en.wikipedia.org/wiki/Conversion_%28linguistics%29

推荐答案

这是一种启发式方法.我刚刚编写了代码,因此样式很有趣.它使用来自wordnet的derivationally_related_forms().我已经实现名词化.我猜verbify工作类似.根据我的测试,效果很好:

This is more a heuristic approach. I have just coded it so appologies for the style. It uses the derivationally_related_forms() from wordnet. I have implemented nounify. I guess verbify works analogous. From what I've tested works pretty well:

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

这篇关于在动词/名词/形容词形式之间转换单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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