-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathari.py
95 lines (86 loc) · 2.51 KB
/
ari.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import math
from readability.exceptions import ReadabilityException
class Result:
def __init__(self, score, grade_levels, ages):
self.score = score
self.grade_levels = grade_levels
self.ages = ages
def __str__(self):
return "score: {}, grade_levels: {}, ages: {}". \
format(self.score, self.grade_levels, self.ages)
class ARI:
def __init__(self, stats, min_words=100):
self._stats = stats
if stats.num_words < min_words:
raise ReadabilityException('{} words required.'.format(min_words))
def score(self):
score = self._score()
return Result(
score=score,
grade_levels=self._grade_levels(score),
ages=self._ages(score))
def _score(self):
s = self._stats
letters_per_word = s.num_letters / s.num_words
words_per_sent = s.num_words / s.num_sentences
return 4.71 * letters_per_word + 0.5 * words_per_sent - 21.43
def _grade_levels(self, score):
score = math.ceil(score)
if score <= 1:
return ['K']
elif score <= 2:
return ['1', '2']
elif score <= 3:
return ['3']
elif score <= 4:
return ['4']
elif score <= 5:
return ['5']
elif score <= 6:
return ['6']
elif score <= 7:
return ['7']
elif score <= 8:
return ['8']
elif score <= 9:
return ['9']
elif score <= 10:
return ['10']
elif score <= 11:
return ['11']
elif score <= 12:
return ['12']
elif score <= 13:
return ['college']
else:
return ['college_graduate']
def _ages(self, score):
score = math.ceil(score)
if score <= 1:
return [5, 6]
elif score <= 2:
return [6, 7]
elif score <= 3:
return [7, 9]
elif score <= 4:
return [9, 10]
elif score <= 5:
return [10, 11]
elif score <= 6:
return [11, 12]
elif score <= 7:
return [12, 13]
elif score <= 8:
return [13, 14]
elif score <= 9:
return [14, 15]
elif score <= 10:
return [15, 16]
elif score <= 11:
return [16, 17]
elif score <= 12:
return [17, 18]
elif score <= 13:
return [18, 24]
else:
return [24, 100]