Commit 119e1e7d authored by Litwin Bartosz's avatar Litwin Bartosz
Browse files

Zadanie 1

parent 832f442d
No related merge requests found
Showing with 48 additions and 0 deletions
+48 -0
import argparse
import time
import rich.traceback
from rich.progress import track
#from collections import defaultdict
from ascii_graph import Pyasciigraph
from ascii_graph import colors
import collections
from _collections_abc import Iterable
collections.Iterable = Iterable
rich.traceback.install()
parser = argparse.ArgumentParser(description='txt word counter.')
parser.add_argument('filename', help='Name of the file to be read')
parser.add_argument('-w','--words', type=int, default=10, help='Number of words for histogram')
parser.add_argument('-l','--length', type=int, default=0, help='Minimal lenght of a word') ###
args = parser.parse_args()
# Counts = defaultdict(list)
Counts = {}
try:
with open(args.filename,"r",encoding='utf-8') as f:
text = f.read()
wordlist = text.split()
wordlist = wordlist[0:args.words]
for word in track(wordlist):
if len(word) < args.length: continue
nword = word.strip('.,?!').lower()
Counts[nword] = Counts.get(word,0) + 1
#print(nword, Counts[nword])
time.sleep(0.1)
data = list(Counts.items())
graph = Pyasciigraph()
for line in graph.graph('Word Counts', data):
print(line)
except FileNotFoundError:
print(f"Sorry, {args.filename} not found.")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment