gpt4 book ai didi

python - 在动词/名词/形容词形式之间转换单词

转载 作者:IT老高 更新时间:2023-10-28 22:22:34 27 4
gpt4 key购买 nike

我想要一个 python 库函数,它可以跨不同的词性进行翻译/转换。有时它应该输出多个单词(例如“coder”和“code”都是动词“to code”的名词,一个是主语,另一个是宾语)

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

我主要关心动词<=>名词,我想写一个笔记程序。即我可以写“咖啡因拮抗 A1”或“咖啡因是 A1 拮抗剂”,并且通过一些 NLP 它可以找出它们的意思相同。 (我知道这并不容易,它需要 NLP 来解析而不只是标记,但我想破解一个原型(prototype))。

类似的问题... Converting adjectives and adverbs to their noun forms(这个答案只能归结为根 POS。我想在 POS 之间切换。)

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

最佳答案

这更像是一种启发式方法。我刚刚对它进行了编码,因此很适合这种风格。它使用来自 wordnet 的 derivationally_related_forms()。我已经实现了名词化。我猜verify的工作方式类似。从我测试的结果来看,效果很好:

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 - 在动词/名词/形容词形式之间转换单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14489309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com