#!/usr/bin/python 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} def count_words(line): count = 0 for word in line.strip().split(' '): if not word.strip() or word == '*' or word.startswith('#'): continue #print('{} '.format(word), end='') count += 1 #print('-> {}'.format(count)) return count def main(): arguments = sys.argv[1:] filename = arguments[0] chapter_number = None chapter_word_count = 0 act_number = None act_word_count = 0 total_word_count = 0 dev_edited_word_count = 0 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 '')) 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_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 else: chapter_word_count += count_words(line) print('chapter {}: {} words'.format(chapter_number, chapter_word_count)) print('act {}: {} words'.format(act_number, act_word_count)) total_word_count += chapter_word_count print('dev edited: {} words'.format(dev_edited_word_count)) print('total: {} words'.format(total_word_count)) if __name__ == '__main__': main()