I am making paraphrase program with nltk and i stuck at this thing. Function that need attention is synonymifExist() and paraphrase() this code is printing list of list of list. output is shown below. I want to print a simple final sentence with synonym.
Need help urgently!! any help would be appreciated!
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"))
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']]]
Aucun commentaire:
Enregistrer un commentaire