rodrigomasini commited on
Commit
ef34466
·
verified ·
1 Parent(s): 4f8b874

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -174,31 +174,44 @@ def on_select(instruction1, instruction2, instruction3, evt: gr.SelectData):
174
  import re
175
 
176
  def preprocess_text(text):
177
- # Remove special characters and extra spaces
178
- text = re.sub(r'[^\w\s]', '', text)
179
  text = re.sub(r'\s+', ' ', text).strip()
180
  return text
181
 
 
 
 
 
 
 
 
 
182
  def flesch_kincaid_grade_level(text):
183
- sentences = len(re.split(r'[.!?]', text))
184
  words = len(text.split())
185
- syllables = sum([len(re.findall(r'[aeiouyAEIOUY]', word)) for word in text.split()])
186
-
187
- return 0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59
 
 
188
 
189
  def flesch_reading_ease(text):
190
- sentences = len(re.split(r'[.!?]', text))
191
  words = len(text.split())
192
- syllables = sum([len(re.findall(r'[aeiouyAEIOUY]', word)) for word in text.split()])
193
-
194
- return 206.835 - 1.015 * (words / sentences) - 84.6 * (syllables / words)
 
 
195
 
196
  def gunning_fog_index(text):
197
- sentences = len(re.split(r'[.!?]', text))
198
  words = len(text.split())
199
- complex_words = len([word for word in text.split() if len(re.findall(r'[aeiouyAEIOUY]', word)) >= 3])
200
-
201
- return 0.4 * ((words / sentences) + 100 * (complex_words / words))
 
 
202
 
203
  def calculate_readability_metrics(text):
204
  preprocessed_text = preprocess_text(text)
 
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:
195
+ return float('nan') # Return NaN to indicate an error
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:
204
+ return float('nan') # Return NaN to indicate an error
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:
213
+ return float('nan') # Return NaN to indicate an error
214
+ return 0.4 * ((words / sentences_count) + 100 * (complex_words / words))
215
 
216
  def calculate_readability_metrics(text):
217
  preprocessed_text = preprocess_text(text)