sashtech commited on
Commit
251629d
·
verified ·
1 Parent(s): 895841d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -25
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_ # Get the specific tense tag (e.g., VBZ, VBD, VBG, etc.)
61
-
62
- if tense == 'VBZ': # 3rd person singular present
63
- corrected_text.append(token.lemma_) # Replace with base form (example: goes -> go)
64
- elif tense == 'VBD': # Past tense
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) # For other cases, append the word as is
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: # First word of the sentence
84
  sentence.append(token.text.capitalize())
85
- elif token.pos_ == "PROPN": # Proper noun
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