Skip to content

Commit 2f53310

Browse files
authored
Merge pull request cdimascio#33 from EpsIotaPi/master
2 parents 3ffb97f + 4678480 commit 2f53310

File tree

10 files changed

+53
-41
lines changed

10 files changed

+53
-41
lines changed

readability/readability.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
from .text import Analyzer
22
from .scorers import ARI, ColemanLiau, DaleChall, Flesch, \
33
FleschKincaid, GunningFog, LinsearWrite, Smog, Spache
4-
4+
import warnings
55

66
class Readability:
7-
def __init__(self, text):
7+
def __init__(self, text, min_words=100):
88
self._analyzer = Analyzer()
99
self._statistics = self._analyzer.analyze(text)
10+
self._min_words = min_words
11+
if self._min_words < 100:
12+
warnings.warn(
13+
"Documents with fewer than 100 words may affect the accuracy of readability tests"
14+
)
1015

1116
def ari(self):
1217
"""Calculate Automated Readability Index (ARI)."""
13-
return ARI(self._statistics).score()
18+
return ARI(self._statistics, self._min_words).score()
1419

1520
def coleman_liau(self):
1621
"""Calculate Coleman Liau Index."""
17-
return ColemanLiau(self._statistics).score()
22+
return ColemanLiau(self._statistics, self._min_words).score()
1823

1924
def dale_chall(self):
2025
"""Calculate Dale Chall."""
21-
return DaleChall(self._statistics).score()
26+
return DaleChall(self._statistics, self._min_words).score()
2227

2328
def flesch(self):
2429
"""Calculate Flesch Reading Ease score."""
25-
return Flesch(self._statistics).score()
30+
return Flesch(self._statistics, self._min_words).score()
2631

2732
def flesch_kincaid(self):
2833
"""Calculate Flesch-Kincaid Grade Level."""
29-
return FleschKincaid(self._statistics).score()
34+
return FleschKincaid(self._statistics, self._min_words).score()
3035

3136
def gunning_fog(self):
3237
"""Calculate Gunning Fog score."""
33-
return GunningFog(self._statistics).score()
38+
return GunningFog(self._statistics, self._min_words).score()
3439

3540
def linsear_write(self):
3641
"""Calculate Linsear Write."""
37-
return LinsearWrite(self._statistics).score()
42+
return LinsearWrite(self._statistics, self._min_words).score()
3843

39-
def smog(self,all_sentences=False):
44+
def smog(self,all_sentences=False, ignore_length=False):
4045
"""SMOG Index.
4146
`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()
47+
return Smog(self._statistics, self._analyzer.sentences,
48+
all_sentences=all_sentences, ignore_length=ignore_length).score()
4349

4450
def spache(self):
4551
"""Spache Index."""
46-
return Spache(self._statistics).score()
52+
return Spache(self._statistics, self._min_words).score()
4753

4854
def statistics(self):
4955
return {

readability/scorers/ari.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def __str__(self):
1414

1515

1616
class ARI:
17-
def __init__(self, stats):
17+
def __init__(self, stats, min_words=100):
1818
self._stats = stats
19-
if stats.num_words < 100:
20-
raise ReadabilityException('100 words required.')
19+
if stats.num_words < min_words:
20+
raise ReadabilityException('{} words required.'.format(min_words))
2121

2222
def score(self):
2323
score = self._score()

readability/scorers/coleman_liau.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class ColemanLiau:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

readability/scorers/dale_chall.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class DaleChall:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

readability/scorers/flesch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def __str__(self):
1313

1414

1515
class Flesch:
16-
def __init__(self, stats):
16+
def __init__(self, stats, min_words=100):
1717
self._stats = stats
18-
if stats.num_words < 100:
19-
raise ReadabilityException('100 words required.')
18+
if stats.num_words < min_words:
19+
raise ReadabilityException('{} words required.'.format(min_words))
2020

2121
def score(self):
2222
score = self._score()

readability/scorers/flesch_kincaid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class FleschKincaid:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

readability/scorers/gunning_fog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class GunningFog:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

readability/scorers/linsear_write.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class LinsearWrite:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

readability/scorers/smog.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import math
22
from readability.text.analyzer import Analyzer
33
from readability.exceptions import ReadabilityException
4-
4+
import warnings
55

66
class Result:
77
def __init__(self, score, grade_level):
@@ -14,16 +14,22 @@ def __str__(self):
1414

1515

1616
class Smog:
17-
def __init__(self, stats, sentences, all_sentences=False):
17+
def __init__(self, stats, sentences, all_sentences=False, ignore_length=False):
1818
"""
1919
Computes the SMOG readability score (Harry McLaughlin, 1969 https://ogg.osu.edu/media/documents/health_lit/WRRSMOG_Readability_Formula_G._Harry_McLaughlin__1969_.pdf)
2020
If all_sentences is false, computes the score as described in McLaughlin, 1969, using exactly 30 sentences
2121
If all_sentences is true, adjusts the score to use all sentences in the text
2222
"""
2323
if stats.num_sentences < 30:
24-
raise ReadabilityException(
25-
'SMOG requires 30 sentences. {} found'
26-
.format(stats.num_sentences))
24+
if not ignore_length:
25+
raise ReadabilityException(
26+
'SMOG requires 30 sentences. {} found'
27+
.format(stats.num_sentences))
28+
else:
29+
warnings.warn(
30+
'SMOG requires 30 sentences. {} found'
31+
.format(stats.num_sentences))
32+
2733

2834
self._stats = stats
2935
self.all_sentences = all_sentences

readability/scorers/spache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def __str__(self):
1212

1313

1414
class Spache:
15-
def __init__(self, stats):
15+
def __init__(self, stats, min_words=100):
1616
self._stats = stats
17-
if stats.num_words < 100:
18-
raise ReadabilityException('100 words required.')
17+
if stats.num_words < min_words:
18+
raise ReadabilityException('{} words required.'.format(min_words))
1919

2020
def score(self):
2121
score = self._score()

0 commit comments

Comments
 (0)