From c8651856a094979d20afbab2b8a2ef0818abefed Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 10 Jan 2022 09:59:18 -0800 Subject: [PATCH] Account for a few more edge cases. --- novel_stats/novel_stats.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index 5181d06..8452ffe 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -72,7 +72,7 @@ def main(): word_count_by_chapter = collections.defaultdict(int) word_count_by_status = collections.defaultdict(int) word_count_by_act = collections.defaultdict(int) - status_by_chapter = {} + status_by_chapter = collections.defaultdict(lambda: collections.defaultdict(int)) current_status = None for line in mdfile.readlines(): @@ -83,15 +83,15 @@ def main(): chapter_heading = line[len(CHAPTER_MARKER) :].strip('()\n') # Count the words in chapter heading, because the chapter number and title count as words. - word_count_by_chapter[chapter_heading] = count_words(chapter_heading) - - status_by_chapter[chapter_heading] = collections.defaultdict(int) - current_status = None + if chapter_heading: + word_count_by_chapter[chapter_heading] = count_words(chapter_heading) + current_status = None # Modified to allow multiple statuses in a single chapter, can swap back and forth. elif line.startswith(STATUS_MARKER): if current_status is None: current_status = line[len(STATUS_MARKER) :].strip('()\n') - status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading) + if chapter_heading: + status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading) else: current_status = line[len(STATUS_MARKER) :].strip('()\n') elif line.startswith(ACT_MARKER):