Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -78,6 +78,78 @@ def correct_grammar_tense_plural(text):
|
|
78 |
text = check_pluralization_error(text)
|
79 |
return text
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# Gradio app setup with three tabs
|
82 |
with gr.Blocks() as demo:
|
83 |
with gr.Tab("AI Detection"):
|
|
|
78 |
text = check_pluralization_error(text)
|
79 |
return text
|
80 |
|
81 |
+
# Function to get synonyms using NLTK WordNet (Humanifier)
|
82 |
+
def get_synonyms_nltk(word, pos):
|
83 |
+
synsets = wordnet.synsets(word, pos=pos)
|
84 |
+
if synsets:
|
85 |
+
lemmas = synsets[0].lemmas()
|
86 |
+
return [lemma.name() for lemma in lemmas]
|
87 |
+
return []
|
88 |
+
|
89 |
+
# Function to capitalize the first letter of sentences and proper nouns (Humanifier)
|
90 |
+
def capitalize_sentences_and_nouns(text):
|
91 |
+
doc = nlp(text)
|
92 |
+
corrected_text = []
|
93 |
+
|
94 |
+
for sent in doc.sents:
|
95 |
+
sentence = []
|
96 |
+
for token in sent:
|
97 |
+
if token.i == sent.start: # First word of the sentence
|
98 |
+
sentence.append(token.text.capitalize())
|
99 |
+
elif token.pos_ == "PROPN": # Proper noun
|
100 |
+
sentence.append(token.text.capitalize())
|
101 |
+
else:
|
102 |
+
sentence.append(token.text)
|
103 |
+
corrected_text.append(' '.join(sentence))
|
104 |
+
|
105 |
+
return ' '.join(corrected_text)
|
106 |
+
|
107 |
+
# Paraphrasing function using SpaCy and NLTK (Humanifier)
|
108 |
+
def paraphrase_with_spacy_nltk(text):
|
109 |
+
doc = nlp(text)
|
110 |
+
paraphrased_words = []
|
111 |
+
|
112 |
+
for token in doc:
|
113 |
+
# Map SpaCy POS tags to WordNet POS tags
|
114 |
+
pos = None
|
115 |
+
if token.pos_ in {"NOUN"}:
|
116 |
+
pos = wordnet.NOUN
|
117 |
+
elif token.pos_ in {"VERB"}:
|
118 |
+
pos = wordnet.VERB
|
119 |
+
elif token.pos_ in {"ADJ"}:
|
120 |
+
pos = wordnet.ADJ
|
121 |
+
elif token.pos_ in {"ADV"}:
|
122 |
+
pos = wordnet.ADV
|
123 |
+
|
124 |
+
synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
|
125 |
+
|
126 |
+
# Replace with a synonym only if it makes sense
|
127 |
+
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
|
128 |
+
paraphrased_words.append(synonyms[0])
|
129 |
+
else:
|
130 |
+
paraphrased_words.append(token.text)
|
131 |
+
|
132 |
+
# Join the words back into a sentence
|
133 |
+
paraphrased_sentence = ' '.join(paraphrased_words)
|
134 |
+
|
135 |
+
# Capitalize sentences and proper nouns
|
136 |
+
corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
|
137 |
+
|
138 |
+
return corrected_text
|
139 |
+
|
140 |
+
# Combined function: Paraphrase -> Capitalization -> Grammar Correction
|
141 |
+
def paraphrase_and_correct(text):
|
142 |
+
# Step 1: Paraphrase the text
|
143 |
+
paraphrased_text = paraphrase_with_spacy_nltk(text)
|
144 |
+
|
145 |
+
# Step 2: Capitalize sentences and proper nouns
|
146 |
+
capitalized_text = capitalize_sentences_and_nouns(paraphrased_text)
|
147 |
+
|
148 |
+
# Step 3: Correct grammar, tense, and pluralization
|
149 |
+
final_text = correct_grammar_tense_plural(capitalized_text)
|
150 |
+
|
151 |
+
return final_text
|
152 |
+
|
153 |
# Gradio app setup with three tabs
|
154 |
with gr.Blocks() as demo:
|
155 |
with gr.Tab("AI Detection"):
|