Returns the readability of the string as a value between 0.0-1.0: 0.30-0.50 (difficult) => 0.60-0.70 (standard) => 0.90-1.00 (very easy).
(string)
| 300 | # 0.0-0.3 = best understood by university graduates. |
| 301 | |
| 302 | def flesch_reading_ease(string): |
| 303 | """ Returns the readability of the string as a value between 0.0-1.0: |
| 304 | 0.30-0.50 (difficult) => 0.60-0.70 (standard) => 0.90-1.00 (very easy). |
| 305 | """ |
| 306 | def count_syllables(word, vowels="aeiouy"): |
| 307 | n = 0 |
| 308 | p = False # True if the previous character was a vowel. |
| 309 | for ch in word.endswith("e") and word[:-1] or word: |
| 310 | v = ch in vowels |
| 311 | n += int(v and not p) |
| 312 | p = v |
| 313 | return n |
| 314 | if len(string) < 3: |
| 315 | return 1.0 |
| 316 | string = string.strip() |
| 317 | string = string.strip("\"'().") |
| 318 | string = string.lower() |
| 319 | string = string.replace("!", ".") |
| 320 | string = string.replace("?", ".") |
| 321 | string = string.replace(",", " ") |
| 322 | y = [count_syllables(w) for w in string.split() if w != ""] |
| 323 | w = len([w for w in string.split(" ") if w != ""]) |
| 324 | s = len([s for s in string.split(".") if len(s) > 2]) |
| 325 | #R = 206.835 - 1.015 * w/s - 84.6 * sum(y)/w |
| 326 | # Use the Farr, Jenkins & Patterson algorithm, |
| 327 | # which uses simpler syllable counting (count_syllables() is the weak point here). |
| 328 | R = 1.599 * sum(1 for v in y if v == 1) * 100 / w - 1.015*w/s - 31.517 |
| 329 | R = max(0.0, min(R*0.01, 1.0)) |
| 330 | return R |
| 331 | |
| 332 | readability = flesch_reading_ease |
| 333 |
nothing calls this directly
no test coverage detected
searching dependent graphs…