-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed output of main files to match the python tests.
- Loading branch information
1 parent
cdcb67a
commit 6ceefeb
Showing
2 changed files
with
25 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
#!/usr/bin/env python3 | ||
import numpy as np | ||
|
||
from pathlib import Path | ||
import sys | ||
|
||
|
||
def main(): | ||
file_path = Path(__file__).parent.absolute() / 'holmes.txt' | ||
# file_path = Path(__file__).parent.absolute() / 'holmes.txt' | ||
file_path = Path(sys.argv[1]) | ||
full_text = file_path.read_text() | ||
|
||
for c in '.,?"!():': # Not very elegant | ||
full_text = full_text.replace(f'{c}', '') | ||
|
||
full_text = full_text.replace('-', ' ') # Treat hyphenated words seperatly | ||
full_text = full_text.replace("'", ' ') # Treat hyphenated words seperatly | ||
full_text = full_text.lower() # Pass everything to lowercases | ||
word_list = full_text.split() # Split words seperated with any white space | ||
word_list = [w for w in word_list if len(w)>4] # Consider words longer than 4 ch | ||
|
||
words, counts = np.unique(word_list, return_counts=True) # Count ocurrences | ||
# Count words | ||
word_count = {} | ||
for word in word_list: | ||
try: | ||
word_count[word] += 1 | ||
except KeyError: | ||
word_count[word] = 1 | ||
|
||
# Sort in decreasing order | ||
word_count = dict(sorted(word_count.items(), key=lambda x : -x[1])) | ||
|
||
print(f'{"Word":10} : Usage') | ||
for i, word in enumerate(word_count): | ||
print(f'{word:10} : {word_count[word]}') | ||
if i==30: break | ||
|
||
sorted_indices = np.argsort(-counts) | ||
for i, (w, c) in enumerate(zip(words[sorted_indices], counts[sorted_indices])): | ||
print(f'{w:10} : {c}') | ||
if i==30:break # Print first 30 words | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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