Converting Readability formula into python function

前端 未结 1 1094
一生所求
一生所求 2021-01-25 21:09

I was given this formula called FRES (Flesch reading-ease test) that is used to measure the readability of a document:

My task is to write a python function tha

相关标签:
1条回答
  • 2021-01-25 22:07

    BTW, there's the textstat library.

    from textstat.textstat import textstat
    from nltk.corpus import gutenberg
    
    for filename in gutenberg.fileids():
        print(filename, textstat.flesch_reading_ease(filename))
    

    If you're bent on coding up your own, first you've to

    • decide if a punctuation is a word
    • define how to count no. of syllables in the word.

    If punctuation is a word and syllables is counted by the regex in your question, then:

    import re
    from itertools import chain
    from nltk.corpus import gutenberg
    
    def num_syllables_per_word(word):
        return len(re.findall('[aeiou]+[^aeiou]+', word))
    
    for filename in gutenberg.fileids():
        sents = gutenberg.sents(filename)
        words = gutenberg.words(filename) # i.e. list(chain(*sents))
        num_sents = len(sents)
        num_words = len(words)
        num_syllables = sum(num_syllables_per_word(w) for w in words)
        score = 206.835 - 1.015 * (num_words / num_sents) - 84.6 * (num_syllables / num_words)
        print(filename, score)
    
    0 讨论(0)
提交回复
热议问题