#/usr/bin/python3
# ====================================================================
# count the number and size of the words in a text file
# plot the data
# ====================================================================
import numpy as np
import matplotlib.pyplot as plt
filename = 'constitution.txt'
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
max_word_length_count = 20
word_length_list = [0]* max_word_length_count
line_count = 0
word_count = 0
with open(filename,'r') as f:
try:
for line in f:
line = line.strip()
line_count += 1
words = line.split()
for w in words:
word_count += 1
word_length_list[len(w)] += 1
except Exception as e:
print(e)
print()
print(f'line count = {line_count}')
print(f'word count = {word_count}')
for i,v in enumerate(word_length_list):
print(f'[{i}] {v}')
plt.title(f'{filename} Word Length Distribution')
plt.xlabel('Word Length')
plt.ylabel('Word Count')
plt.grid()
plt.bar(x,word_length_list,color='b',linewidth=2.0)
plt.show()