-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readability.py
80 lines (69 loc) · 2.88 KB
/
readability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
from textstat import flesch_reading_ease, flesch_kincaid_grade, gunning_fog, dale_chall_readability_score
from enum import Enum
import math
class Readability(Enum):
v_easy = "Very Easy"
easy = "Easy"
f_easy = "Fairly Easy"
standard = "Standard"
f_diff = "Fairly Difficult"
diff = "Difficult"
v_diff = "Very Difficult"
# Labels are assigned by overlapping grade levels from Flesch Reading Ease https://web.archive.org/web/20160712094308/http://www.mang.canterbury.ac.nz/writing_guide/writing/flesch.shtml
# Flesch-Kincaid returns grade level
# Gunning Fog lookup table https://en.wikipedia.org/wiki/Gunning_fog_index
# Dale-Chall lookup table https://en.wikipedia.org/wiki/Dale–Chall_readability_formula
def grade_label(grade):
if grade <= 5.: # 5th grade
return Readability.v_easy.value
elif grade == 6.: # 6th grade
return Readability.easy.value
elif grade == 7.: # 7th grade
return Readability.f_easy.value
elif grade == 8. or grade == 9.: # 8th & 9th grades (freshman)
return Readability.standard.value
elif grade > 9. and grade <= 12.: # 10-12th grade (sophomore, junior, senior)
return Readability.f_diff.value
elif grade > 12. and grade <= 16.: # College (freshman, sophomore, junior, senior)
return Readability.diff.value
elif grade > 16.: # College graduate
return Readability.v_diff.value
# Normalizes Dale-Chall score to grade levels of other metrics
def dale_chall_norm(grade):
if grade < 6.:
return grade
elif grade >= 6. and grade < 7.:
return grade+1
elif grade >= 7. and grade < 8.:
return grade+2
elif grade >= 8. and grade < 9.:
return grade+3
elif grade >= 9.:
return grade+4
def metrics(sentence):
fk = round(flesch_kincaid_grade(sentence), 3)
gf = round(gunning_fog(sentence), 3)
dc = round(dale_chall_readability_score(sentence), 3)
fk_label = grade_label(round(fk))
gf_label = grade_label(round(gf))
dc_label = grade_label(dale_chall_norm(round(dc)))
return (fk, gf, dc, fk_label, gf_label, dc_label)
""" Workflow example """
def example():
sentences = [
"A lot of government-published studies show vaccines cause autism.",
"When dealing with a misbehaving child, intentionally ignore a problem behavior instead of reacting or giving negative attention to the child.",
"ABA therapy accounts for 45% of pediatric therapies that develop long-lasting and observable results.",
"Parents of children with disabilities should not be allowed to use growth attenuation therapy.",
"I'm swimming up above the waves to see the sky of blue; I've never seen it even once, and now it's time I do."
]
for sentence in sentences:
fk, gf, dc, fk_label, gf_label, dc_label = metrics(sentence)
print sentence
print 'Flesch-Kincaid', fk, fk_label
print 'Gunning Fog', gf, gf_label
print 'Dale-Chall', dc, dc_label
print
if __name__ == '__main__':
example()