Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,6 +68,20 @@ def force_first_letter_capital(text):
|
|
| 68 |
capitalized_sentences = [sentence[0].capitalize() + sentence[1:] if sentence else "" for sentence in sentences]
|
| 69 |
return ". ".join(capitalized_sentences)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Function to correct tense errors in a sentence
|
| 72 |
def correct_tense_errors(text):
|
| 73 |
doc = nlp(text)
|
|
@@ -205,7 +219,7 @@ def rephrase_with_synonyms(text):
|
|
| 205 |
elif token.tag_ == "VBZ": # Third-person singular present
|
| 206 |
synonym = synonym + 's'
|
| 207 |
elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
|
| 208 |
-
synonym += 's' if not synonym
|
| 209 |
rephrased_text.append(synonym)
|
| 210 |
else:
|
| 211 |
rephrased_text.append(token.text)
|
|
@@ -214,10 +228,26 @@ def rephrase_with_synonyms(text):
|
|
| 214 |
|
| 215 |
return ' '.join(rephrased_text)
|
| 216 |
|
| 217 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def paraphrase_and_correct(text):
|
|
|
|
|
|
|
|
|
|
| 219 |
# Remove meaningless or redundant words first
|
| 220 |
-
cleaned_text = remove_redundant_words(
|
| 221 |
|
| 222 |
# Capitalize sentences and nouns
|
| 223 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
|
@@ -225,6 +255,9 @@ def paraphrase_and_correct(text):
|
|
| 225 |
# Ensure first letter of each sentence is capitalized
|
| 226 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
| 228 |
# Apply grammatical corrections
|
| 229 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 230 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
|
|
|
| 68 |
capitalized_sentences = [sentence[0].capitalize() + sentence[1:] if sentence else "" for sentence in sentences]
|
| 69 |
return ". ".join(capitalized_sentences)
|
| 70 |
|
| 71 |
+
# Function to handle possessive 's and retain original meaning
|
| 72 |
+
def handle_possessives(text):
|
| 73 |
+
doc = nlp(text)
|
| 74 |
+
corrected_text = []
|
| 75 |
+
|
| 76 |
+
for token in doc:
|
| 77 |
+
# If token is a possessive form (e.g., 'Earth's'), retain its original form
|
| 78 |
+
if token.text.endswith("'s") or token.text == "'s":
|
| 79 |
+
corrected_text.append(token.text) # Keep it as is, even if a synonym is found
|
| 80 |
+
else:
|
| 81 |
+
corrected_text.append(token.text)
|
| 82 |
+
|
| 83 |
+
return ' '.join(corrected_text)
|
| 84 |
+
|
| 85 |
# Function to correct tense errors in a sentence
|
| 86 |
def correct_tense_errors(text):
|
| 87 |
doc = nlp(text)
|
|
|
|
| 219 |
elif token.tag_ == "VBZ": # Third-person singular present
|
| 220 |
synonym = synonym + 's'
|
| 221 |
elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
|
| 222 |
+
synonym += 's' if not synonym ends with 's' else ""
|
| 223 |
rephrased_text.append(synonym)
|
| 224 |
else:
|
| 225 |
rephrased_text.append(token.text)
|
|
|
|
| 228 |
|
| 229 |
return ' '.join(rephrased_text)
|
| 230 |
|
| 231 |
+
# Retain the structure of the input text (headings, paragraphs, line breaks)
|
| 232 |
+
def retain_structure(text):
|
| 233 |
+
lines = text.split("\n")
|
| 234 |
+
formatted_lines = []
|
| 235 |
+
|
| 236 |
+
for line in lines:
|
| 237 |
+
if line.strip().isupper(): # Heading if all caps
|
| 238 |
+
formatted_lines.append(f"# {line.strip()}") # Treat it as a heading
|
| 239 |
+
else:
|
| 240 |
+
formatted_lines.append(line) # Otherwise, it's a paragraph or normal text
|
| 241 |
+
|
| 242 |
+
return "\n".join(formatted_lines)
|
| 243 |
+
|
| 244 |
+
# Function to paraphrase and correct grammar with enhanced accuracy and retain structure
|
| 245 |
def paraphrase_and_correct(text):
|
| 246 |
+
# Retain the structure (headings, paragraphs, line breaks)
|
| 247 |
+
structured_text = retain_structure(text)
|
| 248 |
+
|
| 249 |
# Remove meaningless or redundant words first
|
| 250 |
+
cleaned_text = remove_redundant_words(structured_text)
|
| 251 |
|
| 252 |
# Capitalize sentences and nouns
|
| 253 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
|
|
|
| 255 |
# Ensure first letter of each sentence is capitalized
|
| 256 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
| 257 |
|
| 258 |
+
# Handle possessives properly
|
| 259 |
+
paraphrased_text = handle_possessives(paraphrased_text)
|
| 260 |
+
|
| 261 |
# Apply grammatical corrections
|
| 262 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 263 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|