rodrigomasini commited on
Commit
d6b851c
·
verified ·
1 Parent(s): eac7093

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -173,22 +173,31 @@ def on_select(instruction1, instruction2, instruction3, evt: gr.SelectData):
173
  # Grammar metrics
174
  import re
175
 
176
- def preprocess_text(text):
177
- # Remove extra spaces and special characters except punctuation
178
- text = re.sub(r'\s+', ' ', text).strip()
179
- return text
180
-
181
- def count_sentences(text):
182
- # Split sentences using punctuation followed by a space or end of string
183
- sentences = re.split(r'(?<=[.!?]) +', text)
184
- return len(sentences), sentences
 
 
 
 
 
 
 
 
 
185
 
186
  def count_syllables(word):
187
  return len(re.findall(r'[aeiouyAEIOUY]', word))
188
 
189
  def flesch_kincaid_grade_level(text):
190
- sentences_count, sentences = count_sentences(text)
191
- words = len(text.split())
192
  syllables = sum([count_syllables(word) for word in text.split()])
193
 
194
  if sentences_count == 0 or words == 0:
@@ -196,8 +205,8 @@ def flesch_kincaid_grade_level(text):
196
  return 0.39 * (words / sentences_count) + 11.8 * (syllables / words) - 15.59
197
 
198
  def flesch_reading_ease(text):
199
- sentences_count, sentences = count_sentences(text)
200
- words = len(text.split())
201
  syllables = sum([count_syllables(word) for word in text.split()])
202
 
203
  if sentences_count == 0 or words == 0:
@@ -205,8 +214,8 @@ def flesch_reading_ease(text):
205
  return 206.835 - 1.015 * (words / sentences_count) - 84.6 * (syllables / words)
206
 
207
  def gunning_fog_index(text):
208
- sentences_count, sentences = count_sentences(text)
209
- words = len(text.split())
210
  complex_words = len([word for word in text.split() if count_syllables(word) >= 3])
211
 
212
  if sentences_count == 0 or words == 0:
 
173
  # Grammar metrics
174
  import re
175
 
176
+ def pre_process_text(text):
177
+ sentences_list = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
178
+ # Split the elements of the list by newline characters
179
+ split_sentences = []
180
+ for sentence in sentences_list:
181
+ split_sentences.extend(re.split(r'\n+', sentence))
182
+ # Remove empty elements
183
+ cleaned_sentences = [sentence for sentence in split_sentences if sentence.strip()]
184
+ sentences_number = len(cleaned_sentences)
185
+ return sentences, sentences_number
186
+
187
+ # Function to clean the sentences list and return words only
188
+ def extract_words(sentences):
189
+ words = []
190
+ for sentence in sentences:
191
+ # Extract words using regex, ignoring special characters
192
+ words.extend(re.findall(r'\b\w+\b', sentence))
193
+ return words
194
 
195
  def count_syllables(word):
196
  return len(re.findall(r'[aeiouyAEIOUY]', word))
197
 
198
  def flesch_kincaid_grade_level(text):
199
+ sentences, sentences_count = pre_process_text(text)
200
+ words = extract_words(sentences)
201
  syllables = sum([count_syllables(word) for word in text.split()])
202
 
203
  if sentences_count == 0 or words == 0:
 
205
  return 0.39 * (words / sentences_count) + 11.8 * (syllables / words) - 15.59
206
 
207
  def flesch_reading_ease(text):
208
+ sentences_count, sentences = pre_process_text(text)
209
+ words = extract_words(sentences)
210
  syllables = sum([count_syllables(word) for word in text.split()])
211
 
212
  if sentences_count == 0 or words == 0:
 
214
  return 206.835 - 1.015 * (words / sentences_count) - 84.6 * (syllables / words)
215
 
216
  def gunning_fog_index(text):
217
+ sentences_count, sentences = pre_process_text(text)
218
+ words = extract_words(sentences)
219
  complex_words = len([word for word in text.split() if count_syllables(word) >= 3])
220
 
221
  if sentences_count == 0 or words == 0: