|
|
|
|
|
import nltk |
|
from nltk.corpus import wordnet |
|
import streamlit as st |
|
|
|
|
|
nltk.download('punkt') |
|
nltk.download('wordnet') |
|
nltk.download('averaged_perceptron_tagger') |
|
|
|
def rewrite_content(text): |
|
|
|
tokens = nltk.word_tokenize(text) |
|
|
|
|
|
tagged_tokens = nltk.pos_tag(tokens) |
|
|
|
|
|
rewritten_text = [] |
|
for token, tag in tagged_tokens: |
|
|
|
synonyms = wordnet.synsets(token) |
|
if synonyms: |
|
|
|
synonym = synonyms[0].lemmas()[0].name() |
|
|
|
if synonym != token: |
|
rewritten_text.append(synonym) |
|
else: |
|
rewritten_text.append(token) |
|
else: |
|
rewritten_text.append(token) |
|
|
|
|
|
rewritten_content = ' '.join(rewritten_text) |
|
|
|
|
|
paragraphs = text.split('\n') |
|
rewritten_paragraphs = [] |
|
token_index = 0 |
|
|
|
for paragraph in paragraphs: |
|
paragraph_tokens = nltk.word_tokenize(paragraph) |
|
rewritten_paragraph = ' '.join(rewritten_text[token_index:token_index + len(paragraph_tokens)]) |
|
rewritten_paragraphs.append(rewritten_paragraph) |
|
token_index += len(paragraph_tokens) |
|
|
|
return '\n'.join(rewritten_paragraphs) |
|
|
|
|
|
st.title("Content Rewriter using NLTK") |
|
|
|
|
|
input_text = st.text_area("Enter text to rewrite", height=200) |
|
|
|
|
|
if st.button("Rewrite"): |
|
if input_text: |
|
|
|
rewritten_text = rewrite_content(input_text) |
|
st.text_area("Rewritten text", rewritten_text, height=200) |
|
else: |
|
st.warning("Please enter some text to rewrite.") |
|
|