Last active
October 11, 2017 20:49
-
-
Save max-kov/e599e5c9d2a12cef4c97f2e7cbd285d8 to your computer and use it in GitHub Desktop.
Letter parser for text files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from string import ascii_lowercase | |
from collections import Counter | |
import matplotlib.pyplot as plt | |
import operator | |
import numpy as np | |
import glob | |
my_counter = Counter() | |
# for filename in glob.iglob(/home/max/*.txt): | |
for filename in glob.iglob(---PATH---): | |
with open(filename) as f: | |
my_counter+=Counter(letter for line in f | |
for letter in line.lower() | |
if letter in ascii_lowercase) | |
D=dict(my_counter) | |
D = sorted(D.items(), key=operator.itemgetter(1), reverse=True) | |
D.sort(key=lambda x: x[1], reverse=True) | |
people, score = zip(*D) | |
x_pos = np.arange(len(people)) | |
slope, intercept = np.polyfit(x_pos, score, 1) | |
trendline = intercept + (slope * x_pos) | |
plt.plot(x_pos, trendline, color='red', linestyle='--') | |
plt.bar(x_pos, score,align='center') | |
plt.xticks(x_pos, people) | |
plt.ylabel('Time occured') | |
plt.xlabel('Letter') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment