sashtech commited on
Commit
a69899f
·
verified ·
1 Parent(s): fe589d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
app.py CHANGED
@@ -42,23 +42,19 @@ def fix_punctuation_spacing(text):
42
  # Split the text into words and punctuation
43
  words = text.split(' ')
44
  cleaned_words = []
45
-
46
- # Define punctuation marks to check
47
  punctuation_marks = {',', '.', "'", '!', '?', ':'}
48
 
49
  for word in words:
50
- # If the word ends with a punctuation mark, remove space before it
51
  if cleaned_words and word and word[0] in punctuation_marks:
52
- cleaned_words[-1] += word # Append punctuation to the last word
53
  else:
54
- cleaned_words.append(word) # Add word to the list
55
 
56
  return ' '.join(cleaned_words).replace(' ,', ',').replace(' .', '.').replace(" '", "'") \
57
  .replace(' !', '!').replace(' ?', '?').replace(' :', ':')
58
 
59
  # Function to fix possessives like "Earth's"
60
  def fix_possessives(text):
61
- # Simple rule to catch possessives and correct spacing
62
  text = re.sub(r'(\w)\s\'\s?s', r"\1's", text)
63
  return text
64
 
@@ -70,9 +66,9 @@ def capitalize_sentences_and_nouns(text):
70
  for sent in doc.sents:
71
  sentence = []
72
  for token in sent:
73
- if token.i == sent.start: # First word of the sentence
74
  sentence.append(token.text.capitalize())
75
- elif token.pos_ == "PROPN": # Proper noun
76
  sentence.append(token.text.capitalize())
77
  else:
78
  sentence.append(token.text)
@@ -82,14 +78,12 @@ def capitalize_sentences_and_nouns(text):
82
 
83
  # Function to force capitalization of the first letter of every sentence and ensure full stops
84
  def force_first_letter_capital(text):
85
- sentences = re.split(r'(?<=\w[.!?])\s+', text) # Split based on sentence-ending punctuation
86
  capitalized_sentences = []
87
 
88
  for sentence in sentences:
89
  if sentence:
90
- # Capitalize the first letter if not already capitalized
91
  capitalized_sentence = sentence[0].capitalize() + sentence[1:]
92
- # Ensure there's a full stop at the end if there's no punctuation
93
  if not re.search(r'[.!?]$', capitalized_sentence):
94
  capitalized_sentence += '.'
95
  capitalized_sentences.append(capitalized_sentence)
@@ -131,9 +125,9 @@ def ensure_subject_verb_agreement(text):
131
  corrected_text = []
132
  for token in doc:
133
  if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
134
- if token.tag_ == "NN" and token.head.tag_ != "VBZ": # Singular noun, should use singular verb
135
  corrected_text.append(token.head.lemma_ + "s")
136
- elif token.tag_ == "NNS" and token.head.tag_ == "VBZ": # Plural noun, should not use singular verb
137
  corrected_text.append(token.head.lemma_)
138
  corrected_text.append(token.text)
139
  return ' '.join(corrected_text)
@@ -147,31 +141,41 @@ def correct_spelling(text):
147
  if corrected_word is not None:
148
  corrected_words.append(corrected_word)
149
  else:
150
- corrected_words.append(word) # Keep the original word if correction is None
151
  return ' '.join(corrected_words)
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  # Main function for paraphrasing and grammar correction
154
  def paraphrase_and_correct(text):
155
- # Remove meaningless or redundant words first
156
  cleaned_text = remove_redundant_words(text)
157
-
158
- # Capitalize sentences and nouns
159
  paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
160
-
161
- # Ensure first letter of each sentence is capitalized
162
  paraphrased_text = force_first_letter_capital(paraphrased_text)
163
-
164
- # Apply grammatical corrections
165
  paraphrased_text = correct_article_errors(paraphrased_text)
166
  paraphrased_text = correct_tense_errors(paraphrased_text)
167
  paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
168
-
169
- # Fix punctuation spacing and possessives
170
  paraphrased_text = fix_possessives(paraphrased_text)
171
-
172
- # Correct spelling errors
173
  paraphrased_text = correct_spelling(paraphrased_text)
174
  paraphrased_text = fix_punctuation_spacing(paraphrased_text)
 
175
 
176
  return paraphrased_text
177
 
 
42
  # Split the text into words and punctuation
43
  words = text.split(' ')
44
  cleaned_words = []
 
 
45
  punctuation_marks = {',', '.', "'", '!', '?', ':'}
46
 
47
  for word in words:
 
48
  if cleaned_words and word and word[0] in punctuation_marks:
49
+ cleaned_words[-1] += word
50
  else:
51
+ cleaned_words.append(word)
52
 
53
  return ' '.join(cleaned_words).replace(' ,', ',').replace(' .', '.').replace(" '", "'") \
54
  .replace(' !', '!').replace(' ?', '?').replace(' :', ':')
55
 
56
  # Function to fix possessives like "Earth's"
57
  def fix_possessives(text):
 
58
  text = re.sub(r'(\w)\s\'\s?s', r"\1's", text)
59
  return text
60
 
 
66
  for sent in doc.sents:
67
  sentence = []
68
  for token in sent:
69
+ if token.i == sent.start:
70
  sentence.append(token.text.capitalize())
71
+ elif token.pos_ == "PROPN":
72
  sentence.append(token.text.capitalize())
73
  else:
74
  sentence.append(token.text)
 
78
 
79
  # Function to force capitalization of the first letter of every sentence and ensure full stops
80
  def force_first_letter_capital(text):
81
+ sentences = re.split(r'(?<=\w[.!?])\s+', text)
82
  capitalized_sentences = []
83
 
84
  for sentence in sentences:
85
  if sentence:
 
86
  capitalized_sentence = sentence[0].capitalize() + sentence[1:]
 
87
  if not re.search(r'[.!?]$', capitalized_sentence):
88
  capitalized_sentence += '.'
89
  capitalized_sentences.append(capitalized_sentence)
 
125
  corrected_text = []
126
  for token in doc:
127
  if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
128
+ if token.tag_ == "NN" and token.head.tag_ != "VBZ":
129
  corrected_text.append(token.head.lemma_ + "s")
130
+ elif token.tag_ == "NNS" and token.head.tag_ == "VBZ":
131
  corrected_text.append(token.head.lemma_)
132
  corrected_text.append(token.text)
133
  return ' '.join(corrected_text)
 
141
  if corrected_word is not None:
142
  corrected_words.append(corrected_word)
143
  else:
144
+ corrected_words.append(word)
145
  return ' '.join(corrected_words)
146
 
147
+ # Function to replace a word with its synonym
148
+ def replace_with_synonyms(text):
149
+ words = text.split()
150
+ replaced_words = []
151
+
152
+ for word in words:
153
+ synonyms = wordnet.synsets(word)
154
+ if synonyms:
155
+ # Take the first synonym if available
156
+ synonym = synonyms[0].lemmas()[0].name()
157
+ # Replace the word with its synonym if it's different
158
+ if synonym.lower() != word.lower():
159
+ replaced_words.append(synonym.replace('_', ' '))
160
+ else:
161
+ replaced_words.append(word)
162
+ else:
163
+ replaced_words.append(word)
164
+
165
+ return ' '.join(replaced_words)
166
+
167
  # Main function for paraphrasing and grammar correction
168
  def paraphrase_and_correct(text):
 
169
  cleaned_text = remove_redundant_words(text)
 
 
170
  paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
 
 
171
  paraphrased_text = force_first_letter_capital(paraphrased_text)
 
 
172
  paraphrased_text = correct_article_errors(paraphrased_text)
173
  paraphrased_text = correct_tense_errors(paraphrased_text)
174
  paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
 
 
175
  paraphrased_text = fix_possessives(paraphrased_text)
 
 
176
  paraphrased_text = correct_spelling(paraphrased_text)
177
  paraphrased_text = fix_punctuation_spacing(paraphrased_text)
178
+ paraphrased_text = replace_with_synonyms(paraphrased_text) # Add synonym replacement here
179
 
180
  return paraphrased_text
181