Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,18 +55,14 @@ def check_tense_and_correct(text):
|
|
| 55 |
corrected_text = []
|
| 56 |
|
| 57 |
for token in doc:
|
| 58 |
-
# Checking for verbs and their tense
|
| 59 |
if token.pos_ == 'VERB':
|
| 60 |
-
tense = token.tag_
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
corrected_text.append(token.text) # Keep past tense as is
|
| 66 |
-
elif tense == 'VBG': # Gerund/Present participle
|
| 67 |
-
corrected_text.append(token.text) # Keep it unchanged for now
|
| 68 |
else:
|
| 69 |
-
corrected_text.append(token.text)
|
| 70 |
else:
|
| 71 |
corrected_text.append(token.text)
|
| 72 |
|
|
@@ -80,9 +76,9 @@ def capitalize_sentences_and_nouns(text):
|
|
| 80 |
for sent in doc.sents:
|
| 81 |
sentence = []
|
| 82 |
for token in sent:
|
| 83 |
-
if token.i == sent.start:
|
| 84 |
sentence.append(token.text.capitalize())
|
| 85 |
-
elif token.pos_ == "PROPN":
|
| 86 |
sentence.append(token.text.capitalize())
|
| 87 |
else:
|
| 88 |
sentence.append(token.text)
|
|
@@ -96,7 +92,6 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 96 |
paraphrased_words = []
|
| 97 |
|
| 98 |
for token in doc:
|
| 99 |
-
# Map spaCy POS tags to WordNet POS tags
|
| 100 |
pos = None
|
| 101 |
if token.pos_ in {"NOUN"}:
|
| 102 |
pos = wordnet.NOUN
|
|
@@ -108,17 +103,12 @@ def paraphrase_with_spacy_nltk(text):
|
|
| 108 |
pos = wordnet.ADV
|
| 109 |
|
| 110 |
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
| 111 |
-
|
| 112 |
-
# Replace with a synonym only if it makes sense
|
| 113 |
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
| 114 |
paraphrased_words.append(synonyms[0])
|
| 115 |
else:
|
| 116 |
paraphrased_words.append(token.text)
|
| 117 |
|
| 118 |
-
# Join the words back into a sentence
|
| 119 |
paraphrased_sentence = ' '.join(paraphrased_words)
|
| 120 |
-
|
| 121 |
-
# Capitalize sentences and proper nouns
|
| 122 |
corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
|
| 123 |
|
| 124 |
return corrected_text
|
|
@@ -130,16 +120,9 @@ def correct_grammar(text):
|
|
| 130 |
|
| 131 |
# Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
|
| 132 |
def paraphrase_and_correct(text):
|
| 133 |
-
# Step 1: Paraphrase the text
|
| 134 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
| 135 |
-
|
| 136 |
-
# Step 2: Check tense and verbs, and attempt correction
|
| 137 |
tense_checked_text = check_tense_and_correct(paraphrased_text)
|
| 138 |
-
|
| 139 |
-
# Step 3: Capitalize sentences and proper nouns
|
| 140 |
capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
|
| 141 |
-
|
| 142 |
-
# Step 4: Correct grammar using Gramformer
|
| 143 |
final_text = correct_grammar(capitalized_text)
|
| 144 |
|
| 145 |
return final_text
|
|
|
|
| 55 |
corrected_text = []
|
| 56 |
|
| 57 |
for token in doc:
|
|
|
|
| 58 |
if token.pos_ == 'VERB':
|
| 59 |
+
tense = token.tag_
|
| 60 |
+
if tense == 'VBZ':
|
| 61 |
+
corrected_text.append(token.lemma_)
|
| 62 |
+
elif tense == 'VBD':
|
| 63 |
+
corrected_text.append(token.text)
|
|
|
|
|
|
|
|
|
|
| 64 |
else:
|
| 65 |
+
corrected_text.append(token.text)
|
| 66 |
else:
|
| 67 |
corrected_text.append(token.text)
|
| 68 |
|
|
|
|
| 76 |
for sent in doc.sents:
|
| 77 |
sentence = []
|
| 78 |
for token in sent:
|
| 79 |
+
if token.i == sent.start:
|
| 80 |
sentence.append(token.text.capitalize())
|
| 81 |
+
elif token.pos_ == "PROPN":
|
| 82 |
sentence.append(token.text.capitalize())
|
| 83 |
else:
|
| 84 |
sentence.append(token.text)
|
|
|
|
| 92 |
paraphrased_words = []
|
| 93 |
|
| 94 |
for token in doc:
|
|
|
|
| 95 |
pos = None
|
| 96 |
if token.pos_ in {"NOUN"}:
|
| 97 |
pos = wordnet.NOUN
|
|
|
|
| 103 |
pos = wordnet.ADV
|
| 104 |
|
| 105 |
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
|
|
|
|
|
|
| 106 |
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
| 107 |
paraphrased_words.append(synonyms[0])
|
| 108 |
else:
|
| 109 |
paraphrased_words.append(token.text)
|
| 110 |
|
|
|
|
| 111 |
paraphrased_sentence = ' '.join(paraphrased_words)
|
|
|
|
|
|
|
| 112 |
corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
|
| 113 |
|
| 114 |
return corrected_text
|
|
|
|
| 120 |
|
| 121 |
# Combined function: Paraphrase -> Tense Check -> Capitalization -> Grammar Correction
|
| 122 |
def paraphrase_and_correct(text):
|
|
|
|
| 123 |
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
|
|
|
|
|
|
| 124 |
tense_checked_text = check_tense_and_correct(paraphrased_text)
|
|
|
|
|
|
|
| 125 |
capitalized_text = capitalize_sentences_and_nouns(tense_checked_text)
|
|
|
|
|
|
|
| 126 |
final_text = correct_grammar(capitalized_text)
|
| 127 |
|
| 128 |
return final_text
|