Added multi-status chapter support
This commit is contained in:
parent
70e9dd1b98
commit
dcfea89ec7
|
@ -33,19 +33,26 @@ def main():
|
||||||
word_count_by_status = collections.defaultdict(int)
|
word_count_by_status = collections.defaultdict(int)
|
||||||
word_count_by_act = collections.defaultdict(int)
|
word_count_by_act = collections.defaultdict(int)
|
||||||
status_by_chapter = {}
|
status_by_chapter = {}
|
||||||
|
current_status = None
|
||||||
|
|
||||||
for line in open(filename).readlines():
|
for line in open(filename).readlines():
|
||||||
if line.startswith(CHAPTER_MARKER):
|
if line.startswith(CHAPTER_MARKER):
|
||||||
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||||
total_word_count += word_count_by_chapter[chapter_heading]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
if chapter_heading in status_by_chapter:
|
|
||||||
word_count_by_status[status_by_chapter[chapter_heading]] += 1
|
|
||||||
|
|
||||||
chapter_heading = line[len(CHAPTER_MARKER):].strip('()\n')
|
chapter_heading = line[len(CHAPTER_MARKER):].strip('()\n')
|
||||||
|
|
||||||
word_count_by_chapter[chapter_heading] = count_words(chapter_heading) # 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) # Count the words in chapter heading, because the chapter number and title count as words.
|
||||||
|
|
||||||
|
status_by_chapter[chapter_heading] = collections.defaultdict(int)
|
||||||
|
current_status = None
|
||||||
elif line.startswith(STATUS_MARKER):
|
elif line.startswith(STATUS_MARKER):
|
||||||
status_by_chapter[chapter_heading] = line[len(STATUS_MARKER):].strip('()\n')
|
if current_status == None:
|
||||||
|
current_status = line[len(STATUS_MARKER):].strip('()\n')
|
||||||
|
status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading)
|
||||||
|
else:
|
||||||
|
current_status = line[len(STATUS_MARKER):].strip('()\n')
|
||||||
|
status_by_chapter[chapter_heading][current_status] += 0
|
||||||
elif line.startswith(ACT_MARKER):
|
elif line.startswith(ACT_MARKER):
|
||||||
act_heading = line[len(ACT_MARKER):].strip('()\n')
|
act_heading = line[len(ACT_MARKER):].strip('()\n')
|
||||||
word_count_by_act[act_heading] = count_words(act_heading)
|
word_count_by_act[act_heading] = count_words(act_heading)
|
||||||
|
@ -55,29 +62,32 @@ def main():
|
||||||
line_word_count = count_words(line)
|
line_word_count = count_words(line)
|
||||||
word_count_by_chapter[chapter_heading] += line_word_count
|
word_count_by_chapter[chapter_heading] += line_word_count
|
||||||
|
|
||||||
if chapter_heading in status_by_chapter:
|
if current_status:
|
||||||
word_count_by_status[status_by_chapter[chapter_heading]] += line_word_count
|
word_count_by_status[current_status] += line_word_count
|
||||||
|
status_by_chapter[chapter_heading][current_status] += line_word_count
|
||||||
|
|
||||||
# Do some final accounting after the last chapter.
|
# Do some final accounting after the last chapter.
|
||||||
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||||
total_word_count += word_count_by_chapter[chapter_heading]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
if chapter_heading in status_by_chapter:
|
|
||||||
word_count_by_status[status_by_chapter[chapter_heading]] += 1
|
|
||||||
|
|
||||||
# Print out word counts.
|
# Print out word counts.
|
||||||
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
||||||
if chapter_heading is None:
|
if chapter_heading is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
chapter_status = status_by_chapter.get(chapter_heading)
|
|
||||||
|
|
||||||
print(
|
if len(status_by_chapter[chapter_heading]) > 1:
|
||||||
'chapter {}: {:,} words{}'.format(
|
print(f'chapter {chapter_heading}:')
|
||||||
chapter_heading,
|
|
||||||
chapter_word_count,
|
for chapter_status, status_count in status_by_chapter[chapter_heading].items():
|
||||||
' ({})'.format(chapter_status) if chapter_status else '',
|
print(f'\t {status_count:,} ({chapter_status})')
|
||||||
)
|
print(f'\t {chapter_word_count:,} words (total)')
|
||||||
)
|
elif len(status_by_chapter[chapter_heading]) == 1:
|
||||||
|
chapter_status = list(status_by_chapter[chapter_heading].keys())[0]
|
||||||
|
print(f'chapter {chapter_heading}: {chapter_word_count:,} ({chapter_status})')
|
||||||
|
else:
|
||||||
|
print(f'chapter {chapter_heading}: {chapter_word_count:,}')
|
||||||
|
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user