Python 我想用随机选择同义词从列表中打印一个句子
我正在用nltk做一个释义程序,我一直在做这件事。 需要注意的函数是synonymifExist()和reporthrase()。这段代码是打印列表列表的列表。输出如下所示。我想用同义词打印一个简单的最后句子Python 我想用随机选择同义词从列表中打印一个句子,python,python-3.x,random,nlp,nltk,Python,Python 3.x,Random,Nlp,Nltk,我正在用nltk做一个释义程序,我一直在做这件事。 需要注意的函数是synonymifExist()和reporthrase()。这段代码是打印列表列表的列表。输出如下所示。我想用同义词打印一个简单的最后句子 from nltk.tokenize import word_tokenize from nltk.tag import pos_tag from nltk.corpus import wordnet as wn import random def tag(sentence):
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import wordnet as wn
import random
def tag(sentence):
words = word_tokenize(sentence)
words = pos_tag(words)
return words
def paraphraseable(tag):
return tag.startswith('NN') or tag == 'VB' or tag.startswith('JJ')
def pos(tag):
if tag.startswith('NN'):
return wn.NOUN
elif tag.startswith('V'):
return wn.VERB
def synonyms(word, tag):
lemma_lists = [ss.lemmas() for ss in wn.synsets(word, pos(tag))]
lemmas = [lemma.name() for lemma in sum(lemma_lists, [])]
return set(lemmas)
**def synonymIfExists(sentence):
for (word, t) in tag(sentence):
if paraphraseable(t):
syns = synonyms(word, t)
if syns:
if len(syns) > 1:
yield[word, list(syns)]
continue
yield [word, []]
def paraphrase(sentence):
result = [x for x in synonymIfExists(sentence)]
return result
**
print(paraphrase("The quick brown fox jumps over the lazy dog"))
结果:
straightaway
brown
fox
slothful
click
编辑: 因为一些同义词列表是空的,所以您也可以使用
else
来放置?
或放置原始单词(或者您可以尝试确定是否确实需要同义词-例如,对于或类似词)
结果
The -> The [oryginal]
quick -> nimble
brown -> Brown_University
fox -> George_Fox
jumps -> jumps [oryginal]
over -> over [oryginal]
the -> the [oryginal]
lazy -> faineant
dog -> Canis_familiaris
The quickly brownness Fox jumps over the otiose Canis_familiaris
现在你们可以把生词放在单子上而不用打印了
import random
output = [
['The', []],
['quick', ['flying', 'speedy', 'nimble', 'spry', 'promptly', 'straightaway', 'quick', 'agile', 'ready', 'warm', 'fast', 'immediate', 'quickly', 'prompt']], ['brown', ['brownness', 'Brown_University', 'John_Brown', 'Robert_Brown', 'brown', 'Brown']], ['fox', ['dodger', 'George_Fox', 'slyboots', 'fox', 'Charles_James_Fox', 'Fox']],
['jumps', []],
['over', []],
['the', []],
['lazy', ['otiose', 'slothful', 'faineant', 'work-shy', 'lazy', 'indolent']],
['dog', ['weenie', 'Canis_familiaris', 'hound', 'dog-iron', 'domestic_dog', 'wienerwurst', 'firedog', 'andiron', 'frankfurter', 'bounder', 'click', 'pawl', 'wiener', 'frump', 'heel', 'dog', 'hotdog', 'blackguard', 'detent', 'frank', 'cad', 'hot_dog']]
]
sentence = []
for original, synonyms in output:
if synonyms:
new = random.choice(synonyms)
#print(original, '->', new)
sentence.append(new)
else:
#print(original, '->', original, '[original]')
sentence.append(original)
print( " ".join(sentence) )
结果
The -> The [oryginal]
quick -> nimble
brown -> Brown_University
fox -> George_Fox
jumps -> jumps [oryginal]
over -> over [oryginal]
the -> the [oryginal]
lazy -> faineant
dog -> Canis_familiaris
The quickly brownness Fox jumps over the otiose Canis_familiaris
对于输出中的项:如果项[1]:随机。选择(项[1])
?我不确定,但可以使用结果=[x代表同义词exists(句子)]
而不是结果=列表(同义词exists(句子))
The quickly brownness Fox jumps over the otiose Canis_familiaris