Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
#
|
183 |
-
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
def count_syllables(word):
|
187 |
return len(re.findall(r'[aeiouyAEIOUY]', word))
|
188 |
|
189 |
def flesch_kincaid_grade_level(text):
|
190 |
-
|
191 |
-
words =
|
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 =
|
200 |
-
words =
|
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 =
|
209 |
-
words =
|
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:
|