Readability metrics for MoonBit — text counts plus the eight textstat formulas, with all data embedded (no runtime I/O).
import {
"vicoproplus/moon_textstat",
}///|
fn main {
let stats = try! @moon_textstat.analyze("The cat sat on the mat.")
println(stats.flesch_reading_ease) // 116.14500000000001
println(stats.flesch_kincaid_grade) // -1.4499999999999993
println(stats.gunning_fog) // 2.4000000000000004
println(stats.smog_index) // 3.1291
println(stats.automated_readability_index) // -4.300000000000001
println(stats.coleman_liau_index) // -4.299999999999997
println(stats.linsear_write_formula) // 2
println(stats.dale_chall_readability_score) // 0.2976
}moon run cmd/gate --target nativepub suberror TextstatError {
UnsupportedLanguage(String)
MetricFailure(String)
}pub(all) struct TextStats {
char_count : Int
letter_count : Int
lexicon_count : Int
sentence_count : Int
syllable_count : Int
polysyllabcount : Int
monosyllabcount : Int
difficult_words : Int
difficult_words_list : Array[String]
long_word_count : Int
reading_time : Double
flesch_reading_ease : Double
flesch_kincaid_grade : Double
gunning_fog : Double
smog_index : Double
automated_readability_index : Double
coleman_liau_index : Double
linsear_write_formula : Double
dale_chall_readability_score : Double
}fn automated_readability_index(text : String) -> Doublea = chars_per_word(text)
b = words_per_sentence(text)
if a == 0 or b == 0: return 0.0
return (4.71 * a) + (0.5 * b) - 21.43fn char_count(text : String, ignore_spaces? : Bool) -> Intfn coleman_liau_index(text : String) -> Doubleletters = letters_per_word(text) * 100
sentences = sentences_per_word(text) * 100
if letters == 0 or sentences == 0: return 0.0
return (0.058 * letters) - (0.296 * sentences) - 15.8word_count = count_words(text)
hard_count = count_difficult_words(text, lang, syllable_threshold=0)
if word_count == 0: return 0.0
per_difficult_words = 100 * hard_count / word_count
score = (0.1579 * per_difficult_words) + (0.0496 * words_per_sentence(text))
if per_difficult_words > 5: score += 3.6365
return scorefn difficult_words(text : String, lang? : String, syllable_threshold? : Int, unique? : Bool) -> Int raise TextstatErrorfn difficult_words_list(text : String, lang? : String, syllable_threshold? : Int, unique? : Bool) -> Array[String] raise TextstatError0.39 * sl + 11.8 * spw - 15.59fre_base - fre_sentence_length * sl - fre_syll_per_word * spwsyllable_threshold = int(get_lang_cfg(lang, "syllable_threshold"))
diff_words = count_difficult_words(text, lang, syllable_threshold)
tot_words = count_words(text)
if tot_words == 0: return 0.0
per_diff_words = 100 * diff_words / tot_words
return 0.4 * (words_per_sentence(text) + per_diff_words)fn letter_count(text : String) -> Intfn lexicon_count(text : String, removepunct? : Bool, split_contractions? : Bool, split_hyphens? : Bool) -> Intfn linsear_write_formula(text : String, lang? : String, strict_lower? : Bool, strict_upper? : Bool) -> Double raise TextstatErrortext_list = list_words(text, rm_punctuation=False)
if strict_upper and len(text_list) > 100:
words_list = []; i_text = 0
while i_text < len(text_list) and len(words_list) < 100:
word = remove_punctuation(text_list[i_text], rm_apostrophe=False)
i_text += 1
if len(word) > 0: words_list.append(word)
else:
words_list = list_words(text, rm_punctuation=True)
i_text = len(text_list)
if strict_lower and len(words_list) < 100: return 0.0
easy = 0; difficult = 0
for word in words_list:
n = count_syllables(word, lang)
if n >= 3: difficult += 1
elif n > 0: easy += 1
text_trunc = " ".join(text_list[:i_text])
if count_sentences(text_trunc) == 0: return 0.0
number = float((easy * 1 + difficult * 3) / count_sentences(text_trunc))
if number <= 20: number -= 2
return number / 2fn long_word_count(text : String, threshold? : Int) -> Intfn reading_time(text : String, ms_per_char? : Double) -> Doublefn sentence_count(text : String) -> Intsentences = count_sentences(text)
poly_syllab = count_polysyllable_words(text, lang)
return (1.043 * (30 * (poly_syllab / sentences)) ** 0.5) + 3.1291Install
Download zipReadability metrics for MoonBit — text counts plus the eight textstat formulas, with all data embedded (no runtime I/O).