migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
45
python-csi160/week10/part4.py
Normal file
45
python-csi160/week10/part4.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
import string
|
||||
from alice import alice_text
|
||||
|
||||
def word_frequency(text):
|
||||
"""Generates a word frequency dictionary for the supplied text. All punctuation is stripped and the case is converted to lowercase.
|
||||
|
||||
Example dictionary: {'the': 2, 'cat': 1, 'bit': 1, 'dog': 1}
|
||||
|
||||
param text: (string) The text to be analyzed
|
||||
return: (dict) Word frequency dictionary
|
||||
"""
|
||||
# Strip Punctuation
|
||||
for character in string.punctuation:
|
||||
text = text.replace(character, "")
|
||||
|
||||
# Convert to lower case
|
||||
text = text.lower()
|
||||
|
||||
# Generate a list of all of the words
|
||||
words = text.split()
|
||||
|
||||
word_frequency = {}
|
||||
for word in words:
|
||||
if word in word_frequency:
|
||||
word_frequency[word] += 1
|
||||
else:
|
||||
word_frequency[word] = 1
|
||||
return word_frequency
|
||||
|
||||
|
||||
# Code for testing purposes. Do not modify
|
||||
short_sentence = "The silly cat is the silly face."
|
||||
print(f'Analyzing word frequency of "{short_sentence}"')
|
||||
print(word_frequency("The silly cat is the silly face."))
|
||||
|
||||
print()
|
||||
try:
|
||||
print('In Alice and Wonderland,')
|
||||
alice_freq = word_frequency(alice_text)
|
||||
print(f'The word Alice occurs {alice_freq["alice"]} times')
|
||||
print(f'The word melancholy occurs {alice_freq["melancholy"]} times')
|
||||
print(f'{len(alice_freq)} different words are used in the book.')
|
||||
except Exception as e:
|
||||
print('Exception raised when analyzing Alice in Wonderland:')
|
||||
print(e)
|
Loading…
Add table
Add a link
Reference in a new issue