diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index f46a005..d63f779 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -1,19 +1,15 @@ #!/usr/bin/python +import collections import os import string import sys CHAPTER_MARKER = '## ' -FIRST_CHAPTER_TO_ACT = { - 1: 1, - 11: 2, - 28: 3, -} - -DEV_EDITED_CHAPTERS = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31} +STATUS_MARKER = '[status]: # ' +ACT_MARKER = '[act]: # ' def count_words(line): @@ -33,37 +29,56 @@ def main(): filename = arguments[0] chapter_number = None chapter_word_count = 0 + chapter_status = None act_number = None act_word_count = 0 total_word_count = 0 - dev_edited_word_count = 0 + word_count_by_status = collections.defaultdict(int) for line in open(filename).readlines(): if line.startswith(CHAPTER_MARKER): - dev_edited = bool(chapter_number in DEV_EDITED_CHAPTERS) if chapter_number: - print('chapter {}: {} words{}'.format(chapter_number, chapter_word_count, ' (dev edited)' if dev_edited else '')) + print( + 'chapter {}: {} words{}'.format( + chapter_number, + chapter_word_count, + ' ({})'.format(chapter_status) if chapter_status else '' + ) + ) chapter_number = int(line[len(CHAPTER_MARKER):]) act_word_count += chapter_word_count total_word_count += chapter_word_count - if dev_edited: - dev_edited_word_count += chapter_word_count - chapter_word_count = 1 # Start at one, because the chapter number itself counts as a word. + if chapter_status: + word_count_by_status[chapter_status] += 1 - if chapter_number in FIRST_CHAPTER_TO_ACT: - if act_number: - print('act {}: {} words'.format(act_number, act_word_count)) - act_number = FIRST_CHAPTER_TO_ACT[chapter_number] - act_word_count = 1 + chapter_status = None # Clear out the status from the previous chapter. + elif line.startswith(STATUS_MARKER): + chapter_status = line[len(STATUS_MARKER):].strip('()\n') + elif line.startswith(ACT_MARKER): + if act_number: + print('act {}: {} words'.format(act_number, act_word_count)) + act_number = int(line[len(ACT_MARKER):].strip('()\n')) + act_word_count = 1 else: - chapter_word_count += count_words(line) + line_word_count = count_words(line) + chapter_word_count += line_word_count + + if chapter_status: + word_count_by_status[chapter_status] += line_word_count + + if chapter_status: + word_count_by_status[chapter_status] += 1 print('chapter {}: {} words'.format(chapter_number, chapter_word_count)) print('act {}: {} words'.format(act_number, act_word_count)) + print() total_word_count += chapter_word_count - print('dev edited: {} words'.format(dev_edited_word_count)) + + for status, status_word_count in word_count_by_status.items(): + print('{}: {} words'.format(status, status_word_count)) + print('total: {} words'.format(total_word_count))