-
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.
Wrote code using test driven development for new starter mantid pytho…
…n course - the code contains functions listed below 1. get_file_name_from_command_line 2. read_ascii_file 3. split_hyphenated_word 4. remove_punctuations_from_text 5. split_file_content_into_words 6. count_word_occurence the tes functions are added to the test_wordcount.py file
- Loading branch information
1 parent
19e0593
commit cc17398
Showing
5 changed files
with
94 additions
and
26 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
Empty file.
Empty file.
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,17 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import re | ||
from collections import Counter | ||
|
||
|
||
def get_file_name_from_command_line(): | ||
try: | ||
filename = sys.argv[1] | ||
return filename | ||
except IndexError: | ||
raise ValueError(f"no commandline argument for filename was provided") | ||
|
||
|
||
def read_ascii_file(filename): | ||
try: | ||
f = open(filename, 'r') | ||
return f.read() | ||
except FileNotFoundError as e: | ||
raise FileNotFoundError(f"The file {filename} was not found.") | ||
try: | ||
f = open(filename, 'r') | ||
content = f.read() | ||
f.close() | ||
return content | ||
except FileNotFoundError: | ||
raise FileNotFoundError(f"The file {filename} was not found.") | ||
|
||
def main(): | ||
read_ascii_file("file.txt") | ||
|
||
def split_hyphenated_word(word): | ||
return word.split("-") | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
def remove_punctuations_from_text(word): | ||
clean_word = re.sub(r'[.,?\'"!():]', '', word) | ||
return clean_word | ||
|
||
|
||
def split_file_content_into_words(file_content): | ||
word_list = [] | ||
for word in file_content.split(): | ||
word = word.lower() | ||
word = remove_punctuations_from_text(word) | ||
hyphenated_word_list = split_hyphenated_word(word) | ||
word_list.extend(hyphenated_word_list) | ||
|
||
return word_list | ||
|
||
|
||
def count_word_occurence(words): | ||
word_counts = Counter(words) | ||
for word, count in word_counts.items(): | ||
print(f'{word:<15} {count}') | ||
return word_counts | ||
|
||
|
||
def main(): | ||
try: | ||
filename = get_file_name_from_command_line() | ||
file_content = read_ascii_file(filename) | ||
words = split_file_content_into_words(file_content) | ||
count_word_occurence(words) | ||
except FileNotFoundError: | ||
print(f"Error: The file '{filename}' was not found.") | ||
except ValueError as e: | ||
print(e) | ||
|
||
|
||
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