Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
# Define the TTR, MTLD, and Flesch Reading Ease functions as previously described
|
| 6 |
+
# ... (functions go here)
|
| 7 |
+
|
| 8 |
+
# TTR
|
| 9 |
+
|
| 10 |
+
def calculate_ttr(text):
|
| 11 |
+
words = re.findall(r'\b[a-zA-Z]+\b', text.lower())
|
| 12 |
+
unique_words = len(set(words))
|
| 13 |
+
total_words = len(words)
|
| 14 |
+
ttr = unique_words / total_words if total_words != 0 else 0
|
| 15 |
+
return math.ceil(ttr * 100) / 100 # Rounded up to 2 decimal places
|
| 16 |
+
|
| 17 |
+
# MTLD
|
| 18 |
+
|
| 19 |
+
def calculate_mtld(text, ttr_threshold=0.72):
|
| 20 |
+
def mtld_calculation(word_list, threshold):
|
| 21 |
+
token_count = 0
|
| 22 |
+
factor_count = 0
|
| 23 |
+
for i in range(len(word_list)):
|
| 24 |
+
token_count += 1
|
| 25 |
+
unique_words = len(set(word_list[:i + 1]))
|
| 26 |
+
current_ttr = unique_words / token_count
|
| 27 |
+
if current_ttr < threshold:
|
| 28 |
+
factor_count += 1
|
| 29 |
+
token_count = 0
|
| 30 |
+
if token_count > 0:
|
| 31 |
+
factor_count += token_count / len(word_list)
|
| 32 |
+
return len(word_list) / factor_count if factor_count != 0 else 0
|
| 33 |
+
|
| 34 |
+
words = re.findall(r'\b[a-zA-Z]+\b', text.lower())
|
| 35 |
+
mtld_forward = mtld_calculation(words, ttr_threshold)
|
| 36 |
+
mtld_backward = mtld_calculation(words[::-1], ttr_threshold)
|
| 37 |
+
mtld = (mtld_forward + mtld_backward) / 2
|
| 38 |
+
return math.ceil(mtld * 100) / 100 # Rounded up to 2 decimal places
|
| 39 |
+
|
| 40 |
+
# Flesch Reading Ease
|
| 41 |
+
|
| 42 |
+
def count_syllables(word):
|
| 43 |
+
syllable_count = 0
|
| 44 |
+
vowels = "aeiouy"
|
| 45 |
+
if word[0] in vowels:
|
| 46 |
+
syllable_count += 1
|
| 47 |
+
for index in range(1, len(word)):
|
| 48 |
+
if word[index] in vowels and word[index - 1] not in vowels:
|
| 49 |
+
syllable_count += 1
|
| 50 |
+
if word.endswith("e"):
|
| 51 |
+
syllable_count -= 1
|
| 52 |
+
if syllable_count == 0:
|
| 53 |
+
syllable_count += 1
|
| 54 |
+
return syllable_count
|
| 55 |
+
|
| 56 |
+
def calculate_flesch_reading_ease(text):
|
| 57 |
+
sentences = re.split(r'[.!?]+', text)
|
| 58 |
+
words = re.findall(r'\b[a-zA-Z]+\b', text)
|
| 59 |
+
total_sentences = len(sentences)
|
| 60 |
+
total_words = len(words)
|
| 61 |
+
total_syllables = sum(count_syllables(word) for word in words)
|
| 62 |
+
flesch_score = 206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words)
|
| 63 |
+
return math.ceil(flesch_score * 100) / 100 # Rounded up to 2 decimal places
|
| 64 |
+
|