IFRAME SYNC
from nltk import word_tokenize, pos_tag
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
import random
def get_wordnet_pos(treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return ''
def replace_synonyms(word, pos):
synsets = wordnet.synsets(word, pos=pos)
if synsets:
synonyms = [lemma.name() for synset in synsets for lemma in synset.lemmas()]
if synonyms:
return random.choice(synonyms)
return word
def paraphrase(sentence):
tokens = word_tokenize(sentence)
pos_tags = pos_tag(tokens)
lemmatizer = WordNetLemmatizer()
paraphrased_tokens = []
for token, pos_tag in pos_tags:
pos = get_wordnet_pos(pos_tag)
if pos:
lemma = lemmatizer.lemmatize(token, pos=pos)
paraphrased_word = replace_synonyms(lemma, pos)
paraphrased_tokens.append(paraphrased_word)
else:
paraphrased_tokens.append(token)
paraphrased_sentence = ' '.join(paraphrased_tokens)
return paraphrased_sentence
if __name__ == "__main__":
input_sentence = "PARAPHRASING TOOL write code about this software"
paraphrased_sentence = paraphrase(input_sentence)
print("Original sentence: ", input_sentence)
print("Paraphrased sentence: ", paraphrased_sentence)
IFRAME SYNC
Comments
Post a Comment