[Tutor] Ideas on making this cleaner?

Leam Hall leamhall at gmail.com
Sat May 4 11:15:13 EDT 2024


This gets a rough USA grade level report for chapters in a book. There's a lot of "count()" duplication, can it be done in a cleaner fashion?  Coding style is Python 3.12 and standard library only; no exceptions. I also run "black -l 79" on everything.   :)

Full code is at:  https://github.com/LeamHall/bookbot


class Report:
     def __init__(self, data, filename):
         self.filename = filename
         self.lines = list()
         for line in data:
             self.lines.append(line.lower())
         self.count_sentences()
         self.count_words()
         self.count_syallables()
         self.grade_report()

     def count_sentences(self):
         """Counts the number of sentence ending marks."""
         self.sentence_count = 0
         for line in self.lines:
             self.sentence_count += line.count(".")
             self.sentence_count += line.count("?")
             self.sentence_count += line.count("!")

     def count_words(self):
         """Counts the number of words, ignoring punctuation."""
         self.word_count = 0
         for line in self.lines:
             self.word_count += len(line.split())

     def count_syallables(self):
         """Simplistic syllable counter. Does not handle unicode."""
         self.syllable_count = 0
         for line in self.lines:
             self.syllable_count += line.count("a")
             self.syllable_count += line.count("e")
             self.syllable_count += line.count("i")
             self.syllable_count += line.count("o")
             self.syllable_count += line.count("u")
             self.syllable_count -= line.count("ee")
             self.syllable_count -= line.count("oi")
             self.syllable_count -= line.count("oo")
             self.syllable_count -= line.count("ou")
             scrubbed_line = line.replace(".", " ")
             scrubbed_line = scrubbed_line.replace("!", " ")
             scrubbed_line = scrubbed_line.replace("?", " ")
             scrubbed_line = scrubbed_line.replace('"', " ")
             words = scrubbed_line.split()
             for word in words:
                 for phrase in ["e", "ey"]:
                     if word.endswith(phrase):
                         self.syllable_count -= 1
                 for phrase in [
                     "y",
                 ]:
                     if word.endswith(phrase):
                         self.syllable_count += 1
         if self.syllable_count < 1:
             self.syllable_count = 1

     def grade_report(self):
         """Calculates grade level per:
         https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests
         """
         self.sentence_average = self.word_count / self.sentence_count
         self.syllables_per_word_average = self.syllable_count / self.word_count
         self.grade_level = (
             (0.39 * self.sentence_average)
             + (11.8 * self.syllables_per_word_average)
             - 15.59
         )
         self.grade_level = float("{:.2f}".format(self.grade_level))

     def report_data(self):
         """Collates and returns report data."""
         data = dict()
         data["filename"] = self.filename
         data["sentence_average"] = self.sentence_average
         data["grade_level"] = self.grade_level
         data["syllables_per_word_average"] = self.syllables_per_word_average
         return data


-- 
Software Engineer          (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)


More information about the Tutor mailing list