sashtech commited on
Commit
9ea0d50
·
verified ·
1 Parent(s): aa379a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -5,31 +5,31 @@ import spacy
5
  import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
- from spellchecker import SpellChecker # Import SpellChecker for spelling correction
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
  # Function to predict the label and score for English text (AI Detection)
14
  def predict_en(text):
15
  res = pipeline_en(text)[0]
16
  return res['label'], res['score']
17
 
18
- # Ensure necessary NLTK data is downloaded for Humanifier
19
  nltk.download('wordnet')
20
  nltk.download('omw-1.4')
21
 
22
- # Ensure the SpaCy model is installed for Humanifier
23
  try:
24
  nlp = spacy.load("en_core_web_sm")
25
  except OSError:
26
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
27
  nlp = spacy.load("en_core_web_sm")
28
 
29
- # Initialize SpellChecker
30
- spell = SpellChecker()
31
-
32
- # Function to get synonyms using NLTK WordNet (Humanifier)
33
  def get_synonyms_nltk(word, pos):
34
  synsets = wordnet.synsets(word, pos=pos)
35
  if synsets:
@@ -37,7 +37,7 @@ def get_synonyms_nltk(word, pos):
37
  return [lemma.name() for lemma in lemmas]
38
  return []
39
 
40
- # Function to capitalize the first letter of sentences and proper nouns (Humanifier)
41
  def capitalize_sentences_and_nouns(text):
42
  doc = nlp(text)
43
  corrected_text = []
@@ -55,7 +55,20 @@ def capitalize_sentences_and_nouns(text):
55
 
56
  return ' '.join(corrected_text)
57
 
58
- # Function to correct singular/plural errors (Singular/Plural Correction)
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  def correct_singular_plural_errors(text):
60
  doc = nlp(text)
61
  corrected_text = []
@@ -77,18 +90,6 @@ def correct_singular_plural_errors(text):
77
 
78
  return ' '.join(corrected_text)
79
 
80
- # Function to correct tense errors in a sentence (Tense Correction)
81
- def correct_tense_errors(text):
82
- doc = nlp(text)
83
- corrected_text = []
84
- for token in doc:
85
- if token.pos_ == "VERB" and token.dep_ in {"aux", "auxpass"}:
86
- lemma = wordnet.morphy(token.text, wordnet.VERB) or token.text
87
- corrected_text.append(lemma)
88
- else:
89
- corrected_text.append(token.text)
90
- return ' '.join(corrected_text)
91
-
92
  # Function to check and correct article errors
93
  def correct_article_errors(text):
94
  doc = nlp(text)
@@ -122,11 +123,11 @@ def replace_with_synonym(token):
122
 
123
  if synonyms:
124
  synonym = synonyms[0]
125
- if token.tag_ == "VBG":
126
  synonym = synonym + 'ing'
127
- elif token.tag_ == "VBD" or token.tag_ == "VBN":
128
  synonym = synonym + 'ed'
129
- elif token.tag_ == "VBZ":
130
  synonym = synonym + 's'
131
  return synonym
132
  return token.text
@@ -148,9 +149,9 @@ def ensure_subject_verb_agreement(text):
148
  corrected_text = []
149
  for token in doc:
150
  if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
151
- if token.tag_ == "NN" and token.head.tag_ != "VBZ":
152
  corrected_text.append(token.head.lemma_ + "s")
153
- elif token.tag_ == "NNS" and token.head.tag_ == "VBZ":
154
  corrected_text.append(token.head.lemma_)
155
  corrected_text.append(token.text)
156
  return ' '.join(corrected_text)
@@ -158,10 +159,17 @@ def ensure_subject_verb_agreement(text):
158
  # Function to correct spelling errors
159
  def correct_spelling(text):
160
  words = text.split()
161
- corrected_words = [spell.candidates(word) or word for word in words]
 
 
 
 
 
 
 
162
  return ' '.join(corrected_words)
163
 
164
- # Function to paraphrase, correct grammar, and fix spelling errors
165
  def paraphrase_and_correct(text):
166
  # Capitalize first to ensure proper noun capitalization
167
  paraphrased_text = capitalize_sentences_and_nouns(text)
 
5
  import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
+ from spellchecker import SpellChecker
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
  # Function to predict the label and score for English text (AI Detection)
17
  def predict_en(text):
18
  res = pipeline_en(text)[0]
19
  return res['label'], res['score']
20
 
21
+ # Ensure necessary NLTK data is downloaded
22
  nltk.download('wordnet')
23
  nltk.download('omw-1.4')
24
 
25
+ # Ensure the SpaCy model is installed
26
  try:
27
  nlp = spacy.load("en_core_web_sm")
28
  except OSError:
29
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
30
  nlp = spacy.load("en_core_web_sm")
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:
 
37
  return [lemma.name() for lemma in lemmas]
38
  return []
39
 
40
+ # Function to capitalize the first letter of sentences and proper nouns
41
  def capitalize_sentences_and_nouns(text):
42
  doc = nlp(text)
43
  corrected_text = []
 
55
 
56
  return ' '.join(corrected_text)
57
 
58
+ # Function to correct tense errors in a sentence
59
+ def correct_tense_errors(text):
60
+ doc = nlp(text)
61
+ corrected_text = []
62
+ for token in doc:
63
+ if token.pos_ == "VERB" and token.dep_ in {"aux", "auxpass"}:
64
+ # Replace with appropriate verb form
65
+ lemma = wordnet.morphy(token.text, wordnet.VERB) or token.text
66
+ corrected_text.append(lemma)
67
+ else:
68
+ corrected_text.append(token.text)
69
+ return ' '.join(corrected_text)
70
+
71
+ # Function to correct singular/plural errors
72
  def correct_singular_plural_errors(text):
73
  doc = nlp(text)
74
  corrected_text = []
 
90
 
91
  return ' '.join(corrected_text)
92
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # Function to check and correct article errors
94
  def correct_article_errors(text):
95
  doc = nlp(text)
 
123
 
124
  if synonyms:
125
  synonym = synonyms[0]
126
+ if token.tag_ == "VBG": # Present participle (e.g., running)
127
  synonym = synonym + 'ing'
128
+ elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
129
  synonym = synonym + 'ed'
130
+ elif token.tag_ == "VBZ": # Third-person singular present
131
  synonym = synonym + 's'
132
  return synonym
133
  return token.text
 
149
  corrected_text = []
150
  for token in doc:
151
  if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
152
+ if token.tag_ == "NN" and token.head.tag_ != "VBZ": # Singular noun, should use singular verb
153
  corrected_text.append(token.head.lemma_ + "s")
154
+ elif token.tag_ == "NNS" and token.head.tag_ == "VBZ": # Plural noun, should not use singular verb
155
  corrected_text.append(token.head.lemma_)
156
  corrected_text.append(token.text)
157
  return ' '.join(corrected_text)
 
159
  # Function to correct spelling errors
160
  def correct_spelling(text):
161
  words = text.split()
162
+ corrected_words = []
163
+ for word in words:
164
+ candidates = spell.candidates(word)
165
+ if candidates:
166
+ corrected_word = spell.candidates(word).pop()
167
+ else:
168
+ corrected_word = word
169
+ corrected_words.append(corrected_word)
170
  return ' '.join(corrected_words)
171
 
172
+ # Function to paraphrase and correct grammar
173
  def paraphrase_and_correct(text):
174
  # Capitalize first to ensure proper noun capitalization
175
  paraphrased_text = capitalize_sentences_and_nouns(text)