Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -74,7 +74,7 @@ def correct_singular_plural_errors(text):
|
|
74 |
if token.pos_ == "NOUN":
|
75 |
# Check if the noun is singular or plural
|
76 |
if token.tag_ == "NN": # Singular noun
|
77 |
-
# Look for determiners like "many" to correct to plural
|
78 |
if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
|
79 |
corrected_text.append(token.lemma_ + 's')
|
80 |
else:
|
@@ -85,8 +85,6 @@ def correct_singular_plural_errors(text):
|
|
85 |
corrected_text.append(token.lemma_)
|
86 |
else:
|
87 |
corrected_text.append(token.text)
|
88 |
-
else:
|
89 |
-
corrected_text.append(token.text)
|
90 |
else:
|
91 |
corrected_text.append(token.text)
|
92 |
|
@@ -109,52 +107,16 @@ def correct_article_errors(text):
|
|
109 |
corrected_text.append(token.text)
|
110 |
return ' '.join(corrected_text)
|
111 |
|
112 |
-
#
|
113 |
-
def paraphrase_with_spacy_nltk(text):
|
114 |
-
doc = nlp(text)
|
115 |
-
paraphrased_words = []
|
116 |
-
|
117 |
-
for token in doc:
|
118 |
-
# Map SpaCy POS tags to WordNet POS tags
|
119 |
-
pos = None
|
120 |
-
if token.pos_ in {"NOUN"}:
|
121 |
-
pos = wordnet.NOUN
|
122 |
-
elif token.pos_ in {"VERB"}:
|
123 |
-
pos = wordnet.VERB
|
124 |
-
elif token.pos_ in {"ADJ"}:
|
125 |
-
pos = wordnet.ADJ
|
126 |
-
elif token.pos_ in {"ADV"}:
|
127 |
-
pos = wordnet.ADV
|
128 |
-
|
129 |
-
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
130 |
-
|
131 |
-
# Replace with a synonym only if it makes sense
|
132 |
-
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
133 |
-
paraphrased_words.append(synonyms[0])
|
134 |
-
else:
|
135 |
-
paraphrased_words.append(token.text)
|
136 |
-
|
137 |
-
# Join the words back into a sentence
|
138 |
-
paraphrased_sentence = ' '.join(paraphrased_words)
|
139 |
-
|
140 |
-
return paraphrased_sentence
|
141 |
-
|
142 |
-
# Combined function: Paraphrase -> Grammar Correction -> Capitalization (Humanifier)
|
143 |
def paraphrase_and_correct(text):
|
144 |
-
#
|
145 |
-
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
146 |
-
|
147 |
-
# Step 2: Apply grammatical corrections on the paraphrased text
|
148 |
-
corrected_text = correct_article_errors(paraphrased_text)
|
149 |
-
|
150 |
-
corrected_text = capitalize_sentences_and_nouns(corrected_text)
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
return
|
158 |
|
159 |
# Gradio app setup with two tabs
|
160 |
with gr.Blocks() as demo:
|
@@ -176,4 +138,4 @@ with gr.Blocks() as demo:
|
|
176 |
paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
|
177 |
|
178 |
# Launch the app with the remaining functionalities
|
179 |
-
demo.launch()
|
|
|
74 |
if token.pos_ == "NOUN":
|
75 |
# Check if the noun is singular or plural
|
76 |
if token.tag_ == "NN": # Singular noun
|
77 |
+
# Look for determiners like "many", "several", "few" to correct to plural
|
78 |
if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
|
79 |
corrected_text.append(token.lemma_ + 's')
|
80 |
else:
|
|
|
85 |
corrected_text.append(token.lemma_)
|
86 |
else:
|
87 |
corrected_text.append(token.text)
|
|
|
|
|
88 |
else:
|
89 |
corrected_text.append(token.text)
|
90 |
|
|
|
107 |
corrected_text.append(token.text)
|
108 |
return ' '.join(corrected_text)
|
109 |
|
110 |
+
# Function to paraphrase and correct grammar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
def paraphrase_and_correct(text):
|
112 |
+
paraphrased_text = capitalize_sentences_and_nouns(text) # Capitalize first to ensure proper noun capitalization
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
# Apply grammatical corrections
|
115 |
+
paraphrased_text = correct_article_errors(paraphrased_text)
|
116 |
+
paraphrased_text = correct_singular_plural_errors(paraphrased_text)
|
117 |
+
paraphrased_text = correct_tense_errors(paraphrased_text)
|
118 |
+
|
119 |
+
return paraphrased_text
|
120 |
|
121 |
# Gradio app setup with two tabs
|
122 |
with gr.Blocks() as demo:
|
|
|
138 |
paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
|
139 |
|
140 |
# Launch the app with the remaining functionalities
|
141 |
+
demo.launch()
|