premmukund's picture
Update app.py
872cc9b verified
raw
history blame contribute delete
No virus
2.03 kB
import nltk
from nltk.corpus import wordnet
import streamlit as st
# Download required NLTK resources if you haven't already
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')
def rewrite_content(text):
# Tokenize the text
tokens = nltk.word_tokenize(text)
# Tag the tokens with their part-of-speech
tagged_tokens = nltk.pos_tag(tokens)
# Iterate over the tagged tokens and replace words with synonyms
rewritten_text = []
for token, tag in tagged_tokens:
# Fetch synonyms for the word
synonyms = wordnet.synsets(token)
if synonyms:
# Choose the first synonym
synonym = synonyms[0].lemmas()[0].name()
# Ensure the synonym is not the same as the original word
if synonym != token:
rewritten_text.append(synonym)
else:
rewritten_text.append(token)
else:
rewritten_text.append(token)
# Join the rewritten tokens back into a string
rewritten_content = ' '.join(rewritten_text)
# Maintain paragraph structure
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)
# Streamlit interface
st.title("Content Rewriter using NLTK")
# Text input for the content to be rewritten
input_text = st.text_area("Enter text to rewrite", height=200)
# Button to trigger content rewriting
if st.button("Rewrite"):
if input_text:
# Display rewritten content
rewritten_text = rewrite_content(input_text)
st.text_area("Rewritten text", rewritten_text, height=200)
else:
st.warning("Please enter some text to rewrite.")