Skip to content

Commit 8905cee

Browse files
authored
Merge pull request cdimascio#11 from rbamos/master
Added smog with all sentences
2 parents 77427ad + d7428db commit 8905cee

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

readability/readability.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ def linsear_write(self):
3636
"""Calculate Linsear Write."""
3737
return LinsearWrite(self._statistics).score()
3838

39-
def smog(self):
40-
"""SMOG Index."""
41-
return Smog(self._statistics, self._analyzer.sentences).score()
39+
def smog(self,all_sentences=False):
40+
"""SMOG Index.
41+
`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()
4243

4344
def spache(self):
4445
"""Spache Index."""

readability/scorers/smog.py

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

1515

1616
class Smog:
17-
def __init__(self, stats, sentences):
17+
def __init__(self, stats, sentences, all_sentences=False):
18+
"""
19+
Computes the SMOG readability score (Harry McLaughlin, 1969 https://ogg.osu.edu/media/documents/health_lit/WRRSMOG_Readability_Formula_G._Harry_McLaughlin__1969_.pdf)
20+
If all_sentences is false, computes the score as described in McLaughlin, 1969, using exactly 30 sentences
21+
If all_sentences is true, adjusts the score to use all sentences in the text
22+
"""
1823
if stats.num_sentences < 30:
1924
raise ReadabilityException(
2025
'SMOG requires 30 sentences. {} found'
2126
.format(stats.num_sentences))
2227

2328
self._stats = stats
24-
self._smog_stats = self._smog_text_stats(sentences)
29+
self.all_sentences = all_sentences
30+
if not self.all_sentences:
31+
self._smog_stats = self._smog_text_stats(sentences)
32+
2533

2634
def score(self):
2735
score = self._score()
@@ -32,9 +40,14 @@ def score(self):
3240
)
3341

3442
def _score(self):
35-
smog_stats = self._smog_stats
43+
if self.all_sentences:
44+
smog_stats = self._stats
45+
num_sentences = smog_stats.num_sentences
46+
else:
47+
smog_stats = self._smog_stats
48+
num_sentences = 30
49+
3650
num_complex_words = smog_stats.num_poly_syllable_words
37-
num_sentences = 30
3851
return 1.0430 * math.sqrt(30 * num_complex_words / num_sentences) + 3.1291
3952

4053
def _grade_level(self, score):

test/test_readability.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,21 @@ def test_smog(self):
6262
text = ' '.join(text for i in range(0, 5))
6363

6464
readability = Readability(text)
65-
r = readability.smog()
6665

67-
print(r)
68-
self.assertEqual(12.516099999999998, r.score)
69-
self.assertEqual('13', r.grade_level)
66+
#Test SMOG with 30 sentences
67+
r1 = readability.smog()
68+
69+
#Test SMOG with all sentences
70+
r2 = readability.smog(all_sentences=True)
71+
72+
73+
print("all_sentences=False: %s ; all_sentences=True: %s" % (r1,r2))
74+
self.assertEqual(12.516099999999998, r1.score)
75+
self.assertEqual('13', r1.grade_level)
76+
77+
self.assertEqual(12.785403640627713, r2.score)
78+
self.assertEqual('13', r2.grade_level)
79+
7080

7181
def test_spache(self):
7282
r = self.readability.spache()

0 commit comments

Comments
 (0)