Skip to content

Commit 679e29a

Browse files
committed
add argument 'length_exception'
1 parent 3ffb97f commit 679e29a

File tree

10 files changed

+45
-32
lines changed

10 files changed

+45
-32
lines changed

readability/readability.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
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, length_exception=True):
88
self._analyzer = Analyzer()
99
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))
1016

1117
def ari(self):
1218
"""Calculate Automated Readability Index (ARI)."""
13-
return ARI(self._statistics).score()
19+
return ARI(self._statistics, self._length_exception).score()
1420

1521
def coleman_liau(self):
1622
"""Calculate Coleman Liau Index."""
17-
return ColemanLiau(self._statistics).score()
23+
return ColemanLiau(self._statistics, self._length_exception).score()
1824

1925
def dale_chall(self):
2026
"""Calculate Dale Chall."""
21-
return DaleChall(self._statistics).score()
27+
return DaleChall(self._statistics, self._length_exception).score()
2228

2329
def flesch(self):
2430
"""Calculate Flesch Reading Ease score."""
25-
return Flesch(self._statistics).score()
31+
return Flesch(self._statistics, self._length_exception).score()
2632

2733
def flesch_kincaid(self):
2834
"""Calculate Flesch-Kincaid Grade Level."""
29-
return FleschKincaid(self._statistics).score()
35+
return FleschKincaid(self._statistics, self._length_exception).score()
3036

3137
def gunning_fog(self):
3238
"""Calculate Gunning Fog score."""
33-
return GunningFog(self._statistics).score()
39+
return GunningFog(self._statistics, self._length_exception).score()
3440

3541
def linsear_write(self):
3642
"""Calculate Linsear Write."""
37-
return LinsearWrite(self._statistics).score()
43+
return LinsearWrite(self._statistics, self._length_exception).score()
3844

3945
def smog(self,all_sentences=False):
4046
"""SMOG Index.
4147
`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()
4350

4451
def spache(self):
4552
"""Spache Index."""
46-
return Spache(self._statistics).score()
53+
return Spache(self._statistics, self._length_exception).score()
4754

4855
def statistics(self):
4956
return {

readability/scorers/ari.py

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

1515

1616
class ARI:
17-
def __init__(self, stats):
17+
def __init__(self, stats, length_exception=True):
1818
self._stats = stats
19-
if stats.num_words < 100:
19+
if length_exception and stats.num_words < 100:
2020
raise ReadabilityException('100 words required.')
2121

2222
def score(self):

readability/scorers/coleman_liau.py

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

1313

1414
class ColemanLiau:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

readability/scorers/dale_chall.py

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

1313

1414
class DaleChall:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

readability/scorers/flesch.py

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

1414

1515
class Flesch:
16-
def __init__(self, stats):
16+
def __init__(self, stats, length_exception=True):
1717
self._stats = stats
18-
if stats.num_words < 100:
18+
if length_exception and stats.num_words < 100:
1919
raise ReadabilityException('100 words required.')
2020

2121
def score(self):

readability/scorers/flesch_kincaid.py

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

1313

1414
class FleschKincaid:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

readability/scorers/gunning_fog.py

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

1313

1414
class GunningFog:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

readability/scorers/linsear_write.py

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

1313

1414
class LinsearWrite:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

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, length_exception=True):
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 length_exception:
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def __str__(self):
1212

1313

1414
class Spache:
15-
def __init__(self, stats):
15+
def __init__(self, stats, length_exception=True):
1616
self._stats = stats
17-
if stats.num_words < 100:
17+
if length_exception and stats.num_words < 100:
1818
raise ReadabilityException('100 words required.')
1919

2020
def score(self):

0 commit comments

Comments
 (0)