Bug fixes and features #1
|
@ -26,8 +26,8 @@ def count_words(line):
|
||||||
def main():
|
def main():
|
||||||
arguments = sys.argv[1:]
|
arguments = sys.argv[1:]
|
||||||
filename = arguments[0]
|
filename = arguments[0]
|
||||||
chapter_number = None
|
chapter_heading = None
|
||||||
act_number = None
|
act_heading = None
|
||||||
total_word_count = 0
|
total_word_count = 0
|
||||||
word_count_by_chapter = collections.defaultdict(int)
|
word_count_by_chapter = collections.defaultdict(int)
|
||||||
word_count_by_status = collections.defaultdict(int)
|
word_count_by_status = collections.defaultdict(int)
|
||||||
|
@ -36,44 +36,44 @@ def main():
|
||||||
|
|
||||||
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_number] += word_count_by_chapter[chapter_number]
|
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||||
total_word_count += word_count_by_chapter[chapter_number]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
if chapter_number in status_by_chapter:
|
if chapter_heading in status_by_chapter:
|
||||||
word_count_by_status[status_by_chapter[chapter_number]] += 1
|
word_count_by_status[status_by_chapter[chapter_heading]] += 1
|
||||||
|
|
||||||
chapter_number = int(line[len(CHAPTER_MARKER):])
|
chapter_heading = line[len(CHAPTER_MARKER):].strip('()\n')
|
||||||
|
|
||||||
word_count_by_chapter[chapter_number] = 1 # Start at one, because the chapter number itself counts as a word.
|
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.
|
||||||
elif line.startswith(STATUS_MARKER):
|
elif line.startswith(STATUS_MARKER):
|
||||||
status_by_chapter[chapter_number] = line[len(STATUS_MARKER):].strip('()\n')
|
status_by_chapter[chapter_heading] = line[len(STATUS_MARKER):].strip('()\n')
|
||||||
elif line.startswith(ACT_MARKER):
|
elif line.startswith(ACT_MARKER):
|
||||||
act_number = int(line[len(ACT_MARKER):].strip('()\n'))
|
act_heading = line[len(ACT_MARKER):].strip('()\n')
|
||||||
word_count_by_act[act_number] = 1
|
word_count_by_act[act_heading] = count_words(act_heading)
|
||||||
elif line.startswith(COMMENT_MARKER):
|
elif line.startswith(COMMENT_MARKER):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
line_word_count = count_words(line)
|
line_word_count = count_words(line)
|
||||||
word_count_by_chapter[chapter_number] += line_word_count
|
word_count_by_chapter[chapter_heading] += line_word_count
|
||||||
|
|
||||||
if chapter_number in status_by_chapter:
|
if chapter_heading in status_by_chapter:
|
||||||
word_count_by_status[status_by_chapter[chapter_number]] += line_word_count
|
word_count_by_status[status_by_chapter[chapter_heading]] += line_word_count
|
||||||
|
|
||||||
# Do some final accounting after the last chapter.
|
# Do some final accounting after the last chapter.
|
||||||
word_count_by_act[act_number] += word_count_by_chapter[chapter_number]
|
word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading]
|
||||||
total_word_count += word_count_by_chapter[chapter_number]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
if chapter_number in status_by_chapter:
|
if chapter_heading in status_by_chapter:
|
||||||
word_count_by_status[status_by_chapter[chapter_number]] += 1
|
word_count_by_status[status_by_chapter[chapter_heading]] += 1
|
||||||
|
|
||||||
# Print out word counts.
|
# Print out word counts.
|
||||||
witten marked this conversation as resolved
|
|||||||
for chapter_number, chapter_word_count in word_count_by_chapter.items():
|
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
||||||
if chapter_number is None:
|
if chapter_heading is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
chapter_status = status_by_chapter.get(chapter_number)
|
chapter_status = status_by_chapter.get(chapter_heading)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
'chapter {}: {:,} words{}'.format(
|
'chapter {}: {:,} words{}'.format(
|
||||||
chapter_number,
|
chapter_heading,
|
||||||
chapter_word_count,
|
chapter_word_count,
|
||||||
' ({})'.format(chapter_status) if chapter_status else '',
|
' ({})'.format(chapter_status) if chapter_status else '',
|
||||||
)
|
)
|
||||||
|
@ -81,11 +81,11 @@ def main():
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
for act_number, act_word_count in word_count_by_act.items():
|
for act_heading, act_word_count in word_count_by_act.items():
|
||||||
if act_number is None:
|
if act_heading is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print('act {}: {:,} words (~{}%)'.format(act_number, act_word_count, act_word_count * 100 // total_word_count))
|
print('act {}: {:,} words (~{}%)'.format(act_heading, act_word_count, act_word_count * 100 // total_word_count))
|
||||||
|
|
||||||
for status, status_word_count in word_count_by_status.items():
|
for status, status_word_count in word_count_by_status.items():
|
||||||
print('{}: {:,} words (~{}%)'.format(status, status_word_count, status_word_count * 100 // total_word_count))
|
print('{}: {:,} words (~{}%)'.format(status, status_word_count, status_word_count * 100 // total_word_count))
|
||||||
|
|
Loading…
Reference in New Issue
What does the
+= 0
do? Maybe should just be= 0
?The
+= 0
is intentional.If this status hasn't been used in this chapter yet it's no different from
= 0
, mainly here to initialize.If this status has been used, it leaves it unchanged.
Didn't want to do a split logic.
Okay, I see now! Is it necessary to initialize though? Like won't the previous
status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading)
still work even if it hasn't been initialized? Or is the idea that sometimes you want to report statuses even if there are zero words for them?Yep, you're right.
I think the only time that line would do anything is if you put a status change at the end of the file (the way it is in my version would report a 0 for that status), and now that I think about it, deleting that line would be an improvement, in my opinion.
Also same thing if you switch status twice in a row.
Makes sense. I'll remove it after merging!