Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -133,14 +133,43 @@ def replace_with_synonym(token):
|
|
| 133 |
return synonym
|
| 134 |
return token.text
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# Function to paraphrase and correct grammar
|
| 137 |
def paraphrase_and_correct(text):
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
# Apply grammatical corrections
|
| 141 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 142 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
| 143 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# Replace words with synonyms while maintaining verb form
|
| 146 |
doc = nlp(paraphrased_text)
|
|
|
|
| 133 |
return synonym
|
| 134 |
return token.text
|
| 135 |
|
| 136 |
+
# Function to check for and avoid double negatives
|
| 137 |
+
def correct_double_negatives(text):
|
| 138 |
+
doc = nlp(text)
|
| 139 |
+
corrected_text = []
|
| 140 |
+
for token in doc:
|
| 141 |
+
if token.text.lower() == "not" and any(child.text.lower() == "never" for child in token.head.children):
|
| 142 |
+
# Replace the double negative with a positive statement
|
| 143 |
+
corrected_text.append("always")
|
| 144 |
+
else:
|
| 145 |
+
corrected_text.append(token.text)
|
| 146 |
+
return ' '.join(corrected_text)
|
| 147 |
+
|
| 148 |
+
# Function to ensure subject-verb agreement
|
| 149 |
+
def ensure_subject_verb_agreement(text):
|
| 150 |
+
doc = nlp(text)
|
| 151 |
+
corrected_text = []
|
| 152 |
+
for token in doc:
|
| 153 |
+
if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
|
| 154 |
+
# Check if the verb agrees with the subject in number
|
| 155 |
+
if token.tag_ == "NN" and token.head.tag_ != "VBZ": # Singular noun, should use singular verb
|
| 156 |
+
corrected_text.append(token.head.lemma_ + "s")
|
| 157 |
+
elif token.tag_ == "NNS" and token.head.tag_ == "VBZ": # Plural noun, should not use singular verb
|
| 158 |
+
corrected_text.append(token.head.lemma_)
|
| 159 |
+
corrected_text.append(token.text)
|
| 160 |
+
return ' '.join(corrected_text)
|
| 161 |
+
|
| 162 |
# Function to paraphrase and correct grammar
|
| 163 |
def paraphrase_and_correct(text):
|
| 164 |
+
# Capitalize first to ensure proper noun capitalization
|
| 165 |
+
paraphrased_text = capitalize_sentences_and_nouns(text)
|
| 166 |
|
| 167 |
# Apply grammatical corrections
|
| 168 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
| 169 |
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
| 170 |
paraphrased_text = correct_tense_errors(paraphrased_text)
|
| 171 |
+
paraphrased_text = correct_double_negatives(paraphrased_text)
|
| 172 |
+
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 173 |
|
| 174 |
# Replace words with synonyms while maintaining verb form
|
| 175 |
doc = nlp(paraphrased_text)
|