Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions byu_word_count/task_b_code_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#TODO add required Django imports
from datetime import date

class Person(Model):
name = CharField(max_length=100) #Django Charfield null-true still defaults to empty string, thus redundant
birthdate = DateField()
phone_number = CharField(max_length=25)
email_address = EmailField()
CONTACT_TYPE_CHOICES = (
("EMAIL", "Email"),
("PHONE", "Phone"),
)
contact_type = CharField(max_length=10,choices=CONTACT_TYPE_CHOICES,default="EMAIL")

@property
def age(self):
today = date.today()
max_age = today.year - self.birthdate.year
#Tuple comparison yields bool. Comparision starts at 0 position and moves on if needed
today_is_before_birthday = (today.month, today.day) < (self.birthdate.month, self.birthdate.day)

if today_is_before_birthday:
true_age = max_age - 1
else:
true_age = max_age
return true_age
@age.setter
def age(self, value):
raise AttributeError("Change birthdate attribute to modify age attribute")

@property
def first_name(self):
return self.name.split(' ')[0]
@first_name.setter
def first_name(self, value):
raise AttributeError("Change the name attribute to modify the first_name")
53 changes: 53 additions & 0 deletions byu_word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import string
from collections import Counter

def remove_punctuation(target_string, retain_hyphens=True):
"""Accepts a string, removes punctuation (keeps hyphens by default), returns cleansed string"""
punctuation_to_remove_list = [
string.punctuation,
"\N{Left Double Quotation Mark}",
"\N{Right Double Quotation Mark}",
"\N{Left Single Quotation Mark}",
"\N{Right Single Quotation Mark}",
]

punctuation_to_remove = ''.join(punctuation_to_remove_list)
if retain_hyphens:
#Preserve the hyphen char by removing it from the punctuation_to_remove
punctuation_to_remove = punctuation_to_remove.replace("-", '')
#maketrans below maps empty string to empty string as placholder, and then maps third argument to None, removing all punctuation
cleansed_string = target_string.translate(str.maketrans('','',punctuation_to_remove))
return cleansed_string

def parse_file_to_word_counter(filename):
"""Given a filename, return a Counter object of lowercased words stripped of punctuation"""
try:
with open(filename, encoding='utf-8') as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = filename + " not found."
print(msg)
else:
contents = remove_punctuation(contents)
words = contents.lower().split()
word_counter = Counter(words)
return word_counter

def most_common_words(filename, results_to_return=3):
"""Counts the number of words in the file, returns the top 3 results by default"""
word_counts = parse_file_to_word_counter(filename)
for key, value in word_counts.items():
print("Key: " + str(key), "Value: " + str(value))
print(word_counts.most_common(results_to_return))

def count_word(filename, word_to_count):
"""Counts occurrences of word_to_count in the file, and return count"""
word_counts = parse_file_to_word_counter(filename)
formatted_word = remove_punctuation(word_to_count.lower())
return word_counts[formatted_word]

filename="C:/Users/Copy/Downloads/frankenstein.txt"
most_common_words(filename)
print(count_word(filename, "eBooks"))