|
1 | 1 | from .text import Analyzer |
2 | 2 | from .scorers import ARI, ColemanLiau, DaleChall, Flesch, \ |
3 | 3 | FleschKincaid, GunningFog, LinsearWrite, Smog, Spache |
4 | | - |
| 4 | +import warnings |
5 | 5 |
|
6 | 6 | class Readability: |
7 | | - def __init__(self, text): |
| 7 | + def __init__(self, text, length_exception=True): |
8 | 8 | self._analyzer = Analyzer() |
9 | 9 | self._statistics = self._analyzer.analyze(text) |
| 10 | + self._length_exception = length_exception |
| 11 | + if not self._length_exception: |
| 12 | + if self._statistics.num_words < 100: |
| 13 | + warnings.warn( |
| 14 | + "Documents with fewer than 100 words may affect the accuracy of readability tests. {} words found" |
| 15 | + .format(self._statistics.num_words)) |
10 | 16 |
|
11 | 17 | def ari(self): |
12 | 18 | """Calculate Automated Readability Index (ARI).""" |
13 | | - return ARI(self._statistics).score() |
| 19 | + return ARI(self._statistics, self._length_exception).score() |
14 | 20 |
|
15 | 21 | def coleman_liau(self): |
16 | 22 | """Calculate Coleman Liau Index.""" |
17 | | - return ColemanLiau(self._statistics).score() |
| 23 | + return ColemanLiau(self._statistics, self._length_exception).score() |
18 | 24 |
|
19 | 25 | def dale_chall(self): |
20 | 26 | """Calculate Dale Chall.""" |
21 | | - return DaleChall(self._statistics).score() |
| 27 | + return DaleChall(self._statistics, self._length_exception).score() |
22 | 28 |
|
23 | 29 | def flesch(self): |
24 | 30 | """Calculate Flesch Reading Ease score.""" |
25 | | - return Flesch(self._statistics).score() |
| 31 | + return Flesch(self._statistics, self._length_exception).score() |
26 | 32 |
|
27 | 33 | def flesch_kincaid(self): |
28 | 34 | """Calculate Flesch-Kincaid Grade Level.""" |
29 | | - return FleschKincaid(self._statistics).score() |
| 35 | + return FleschKincaid(self._statistics, self._length_exception).score() |
30 | 36 |
|
31 | 37 | def gunning_fog(self): |
32 | 38 | """Calculate Gunning Fog score.""" |
33 | | - return GunningFog(self._statistics).score() |
| 39 | + return GunningFog(self._statistics, self._length_exception).score() |
34 | 40 |
|
35 | 41 | def linsear_write(self): |
36 | 42 | """Calculate Linsear Write.""" |
37 | | - return LinsearWrite(self._statistics).score() |
| 43 | + return LinsearWrite(self._statistics, self._length_exception).score() |
38 | 44 |
|
39 | 45 | def smog(self,all_sentences=False): |
40 | 46 | """SMOG Index. |
41 | 47 | `all_sentences` indicates whether SMOG should use a sample of 30 sentences, as described in the original paper, or if it should use all sentences in the text""" |
42 | | - return Smog(self._statistics, self._analyzer.sentences,all_sentences=all_sentences).score() |
| 48 | + return Smog(self._statistics, self._analyzer.sentences, |
| 49 | + all_sentences=all_sentences,length_exception=self._length_exception).score() |
43 | 50 |
|
44 | 51 | def spache(self): |
45 | 52 | """Spache Index.""" |
46 | | - return Spache(self._statistics).score() |
| 53 | + return Spache(self._statistics, self._length_exception).score() |
47 | 54 |
|
48 | 55 | def statistics(self): |
49 | 56 | return { |
|
0 commit comments