Skip to content

Commit

Permalink
refactored wordcount and charcount
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Nov 20, 2024
1 parent 5fa2fbd commit 3daade4
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions cat_win/src/service/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
summary
"""

from collections import Counter
from itertools import groupby

from cat_win.src.const.regex import TOKENIZER
Expand Down Expand Up @@ -175,15 +176,14 @@ def show_wordcount(files: list, file_encoding: str) -> None:
if Summary.unique:
files = Summary._unique_list(files)

word_count = {}
word_count = Counter()
used_files = []

for hfile in files:
try:
f_content = IoHelper.read_file(hfile.path, file_encoding=file_encoding,
errors='replace')
for token in TOKENIZER.findall(f_content):
word_count[token] = word_count.get(token, 0)+1
word_count.update(TOKENIZER.findall(f_content))
used_files.append(hfile.displayname)
except (OSError, UnicodeError):
pass
Expand All @@ -199,13 +199,12 @@ def show_wordcount(files: list, file_encoding: str) -> None:
print('\n\t'.join(map(
lambda f: f"{Summary.color}{f}{Summary.color_reset}", used_files
)))
sorted_word_count = sorted(word_count.items(), key=lambda token: token[1], reverse=True)
format_delimeter = f"{Summary.color_reset}:{Summary.color} "
for _, group in groupby(sorted_word_count, lambda token: token[1]):

for _, group in groupby(word_count.most_common(), lambda token: token[1]):
sorted_group = sorted(group, key=lambda token: token[0])
formatted_word_count = map(
lambda x: f"{Summary.color}{x[0]}"
f"{format_delimeter}{x[1]}{Summary.color_reset}",
lambda x: f"{Summary.color}{x[0]}{Summary.color_reset}: "
f"{Summary.color}{x[1]}{Summary.color_reset}",
sorted_group
)
print('\n' + '\n'.join(formatted_word_count), end='')
Expand All @@ -225,15 +224,14 @@ def show_charcount(files: list, file_encoding: str) -> None:
if Summary.unique:
files = Summary._unique_list(files)

char_count = {}
char_count = Counter()
used_files = []

for hfile in files:
try:
f_content = IoHelper.read_file(hfile.path, file_encoding=file_encoding,
errors='replace')
for char in list(f_content):
char_count[char] = char_count.get(char, 0)+1
char_count.update(f_content)
used_files.append(hfile.displayname)
except (OSError, UnicodeError):
pass
Expand All @@ -249,13 +247,12 @@ def show_charcount(files: list, file_encoding: str) -> None:
print('\n\t'.join(map(
lambda f: f"{Summary.color}{f}{Summary.color_reset}", used_files
)))
sorted_char_count = sorted(char_count.items(), key=lambda token: token[1], reverse=True)
format_delimeter = f"{Summary.color_reset}:{Summary.color} "
for _, group in groupby(sorted_char_count, lambda token: token[1]):

for _, group in groupby(char_count.most_common(), lambda token: token[1]):
sorted_group = sorted(group, key=lambda token: token[0])
formatted_char_count = map(
lambda x: f"{Summary.color}{repr(x[0]) if x[0].isspace() else x[0]}"
f"{format_delimeter}{x[1]}{Summary.color_reset}",
f"{Summary.color_reset}: {Summary.color}{x[1]}{Summary.color_reset}",
sorted_group
)
print('\n' + '\n'.join(formatted_char_count), end='')
Expand Down

0 comments on commit 3daade4

Please sign in to comment.