sashtech commited on
Commit
5bee7e5
·
verified ·
1 Parent(s): dc68f99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -162
app.py CHANGED
@@ -1,162 +1,25 @@
1
- import os
2
- import gradio as gr
3
- import spacy
4
- import subprocess
5
- import nltk
6
- from nltk.corpus import wordnet
7
- from spellchecker import SpellChecker
8
- from ginger import get_ginger_result # Importing the grammar correction function
9
-
10
- # Initialize the English text classification pipeline for AI detection
11
- pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
12
-
13
- # Initialize the spell checker
14
- spell = SpellChecker()
15
-
16
- # Ensure necessary NLTK data is downloaded
17
- nltk.download('wordnet')
18
- nltk.download('omw-1.4')
19
-
20
- # Ensure the SpaCy model is installed
21
- try:
22
- nlp = spacy.load("en_core_web_sm")
23
- except OSError:
24
- subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
25
- nlp = spacy.load("en_core_web_sm")
26
-
27
- # Function to predict the label and score for English text (AI Detection)
28
- def predict_en(text):
29
- res = pipeline_en(text)[0]
30
- return res['label'], res['score']
31
-
32
- # Function to get synonyms using NLTK WordNet
33
- def get_synonyms_nltk(word, pos):
34
- synsets = wordnet.synsets(word, pos=pos)
35
- if synsets:
36
- lemmas = synsets[0].lemmas()
37
- return [lemma.name() for lemma in lemmas]
38
- return []
39
-
40
- # Function to remove redundant and meaningless words
41
- def remove_redundant_words(text):
42
- doc = nlp(text)
43
- meaningless_words = {"actually", "basically", "literally", "really", "very", "just"}
44
- filtered_text = [token.text for token in doc if token.text.lower() not in meaningless_words]
45
- return ' '.join(filtered_text)
46
-
47
- # Function to capitalize the first letter of sentences and proper nouns
48
- def capitalize_sentences_and_nouns(text):
49
- doc = nlp(text)
50
- corrected_text = []
51
-
52
- for sent in doc.sents:
53
- sentence = []
54
- for token in sent:
55
- if token.i == sent.start: # First word of the sentence
56
- sentence.append(token.text.capitalize())
57
- elif token.pos_ == "PROPN": # Proper noun
58
- sentence.append(token.text.capitalize())
59
- else:
60
- sentence.append(token.text)
61
- corrected_text.append(' '.join(sentence))
62
-
63
- return ' '.join(corrected_text)
64
-
65
- # Function to force capitalization of the first letter of every sentence
66
- def force_first_letter_capital(text):
67
- sentences = text.split(". ") # Split by period to get each sentence
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)
74
- corrected_text = []
75
- for token in doc:
76
- if token.pos_ == "VERB" and token.dep_ in {"aux", "auxpass"}:
77
- lemma = wordnet.morphy(token.text, wordnet.VERB) or token.text
78
- corrected_text.append(lemma)
79
- else:
80
- corrected_text.append(token.text)
81
- return ' '.join(corrected_text)
82
-
83
- # Function to correct singular/plural errors
84
- def correct_singular_plural_errors(text):
85
- doc = nlp(text)
86
- corrected_text = []
87
-
88
- for token in doc:
89
- if token.pos_ == "NOUN":
90
- if token.tag_ == "NN": # Singular noun
91
- if any(child.text.lower() in ['many', 'several', 'few'] for child in token.head.children):
92
- corrected_text.append(token.lemma_ + 's')
93
- else:
94
- corrected_text.append(token.text)
95
- elif token.tag_ == "NNS": # Plural noun
96
- if any(child.text.lower() in ['a', 'one'] for child in token.head.children):
97
- corrected_text.append(token.lemma_)
98
- else:
99
- corrected_text.append(token.text)
100
- else:
101
- corrected_text.append(token.text)
102
-
103
- return ' '.join(corrected_text)
104
-
105
- # Function to check and correct article errors
106
- def correct_article_errors(text):
107
- doc = nlp(text)
108
- corrected_text = []
109
- for token in doc:
110
- if token.text in ['a', 'an']:
111
- next_token = token.nbor(1)
112
- if token.text == "a" and next_token.text[0].lower() in "aeiou":
113
- corrected_text.append("an")
114
- elif token.text == "an" and next_token.text[0].lower() not in "aeiou":
115
- corrected_text.append("a")
116
- else:
117
- corrected_text.append(token.text)
118
- else:
119
- corrected_text.append(token.text)
120
- return ' '.join(corrected_text)
121
-
122
- # Function to get the correct synonym while maintaining verb form
123
- def replace_with_synonym(token):
124
- pos = None
125
- if token.pos_ == "VERB":
126
- pos = wordnet.VERB
127
- elif token.pos_ == "NOUN":
128
- pos = wordnet.NOUN
129
- elif token.pos_ == "ADJ":
130
- pos = wordnet.ADJ
131
- elif token.pos_ == "ADV":
132
- pos = wordnet.ADV
133
-
134
- synonyms = get_synonyms_nltk(token.text, pos)
135
- if synonyms:
136
- return synonyms[0]
137
- return token.text
138
-
139
- # Function to use Ginger API for grammar correction (NEW)
140
- def correct_grammar_with_ginger(text):
141
- result = get_ginger_result(text)
142
- corrected_text = text
143
- for suggestion in result["LightGingerTheTextResult"]:
144
- if suggestion["Suggestions"]:
145
- from_index = suggestion["From"]
146
- to_index = suggestion["To"] + 1
147
- suggested_text = suggestion["Suggestions"][0]["Text"]
148
- corrected_text = corrected_text[:from_index] + suggested_text + corrected_text[to_index:]
149
- return corrected_text
150
-
151
- # Gradio interface
152
- def process_text(text):
153
- text = correct_article_errors(text)
154
- text = correct_singular_plural_errors(text)
155
- text = correct_tense_errors(text)
156
- text = capitalize_sentences_and_nouns(text)
157
- text = remove_redundant_words(text)
158
- text = correct_grammar_with_ginger(text) # Add grammar correction using Ginger here
159
- return text
160
-
161
- iface = gr.Interface(fn=process_text, inputs="text", outputs="text")
162
- iface.launch()
 
1
+ from ginger import correct_sentence
2
+
3
+ def main():
4
+ """
5
+ Main application function.
6
+ Prompts the user for input and corrects grammar using the Ginger API.
7
+ """
8
+ while True:
9
+ # Get input from the user
10
+ text = input("Enter a sentence (or 'q' to quit): ")
11
+
12
+ # Exit the loop if the user wants to quit
13
+ if text.lower() == 'q':
14
+ print("Exiting the application.")
15
+ break
16
+
17
+ # Correct the sentence using the Ginger API
18
+ corrected_text = correct_sentence(text)
19
+
20
+ # Display the original and corrected sentences
21
+ print(f"Original Sentence: {text}")
22
+ print(f"Corrected Sentence: {corrected_text}\n")
23
+
24
+ if __name__ == "__main__":
25
+ main()