added static analysis of Frankenstein
This commit is contained in:
parent
9041ad8ba2
commit
6db3cdaa07
2 changed files with 35 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
books/
|
||||
34
main.py
Normal file
34
main.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import string
|
||||
|
||||
def open_book(book_path):
|
||||
with open(book_path) as f:
|
||||
file_contents = f.read()
|
||||
return file_contents
|
||||
|
||||
def count_words(book_text):
|
||||
words = book_text.split()
|
||||
counter = 0
|
||||
for word in words:
|
||||
counter += 1
|
||||
return counter
|
||||
|
||||
def count_char_instance(book_text):
|
||||
char_count = {char: 0 for char in string.ascii_lowercase}
|
||||
text = book_text
|
||||
for char in text.lower():
|
||||
if char in char_count:
|
||||
char_count[char] += 1
|
||||
return char_count
|
||||
|
||||
def print_report(word_count,char_count):
|
||||
print("--- Begin report of books/frankenstein.txt ---")
|
||||
print(f"{word_count} words found in the document")
|
||||
print("")
|
||||
chars = [char for char in string.ascii_lowercase]
|
||||
for char in chars:
|
||||
print(f"The '{char}' character was found {char_count.get(char)} times")
|
||||
print("--- End report ---")
|
||||
|
||||
wc = count_words(open_book("books/frankenstein.txt"))
|
||||
cc = count_char_instance(open_book("books/frankenstein.txt"))
|
||||
print_report(wc,cc)
|
||||
Loading…
Add table
Add a link
Reference in a new issue