Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output.
Here is the starter outline for the homework:
d.
def count_words(text):
""" Count the number of words in text """
return 0
e.
def words_per_sentence(text):
return 0.0
f.
def word_count(text, punctuation=".,?;"):
""" Return a dictionary of word:count pairs.
(words are the keys, counts are the values). """
gettysburg_address = """
Four score and seven years ago our fathers brought forthon this continent, a new nation, conceived in Liberty,and dedicated to the proposition that all men are createdequal. Now we are engaged in a great civil war, testingwhether that nation, or any nation so conceived and sodedicated, can long endure. We are met on a greatbattlefield of that war. We have come to dedicate aportion of that field, as a final resting place forthose who here gave their lives that that nation mightlive. It is altogether fitting and proper that we shoulddo this. But, in a larger sense, we can not dedicate, wecan not consecrate, we can not hallow this ground.The brave men, living and dead, who struggled here,have consecrated it, far above our poor power to addor detract. The world will little note, nor longremember what we say here, but it can never forget whatthey did here. It is for us the living, rather, to bededicated here to the unfinished work which they whofought here have thus far so nobly advanced. It israther for us to be here dedicated to the great taskremaining before us—that from these honored dead wetake increased devotion to that cause for which theygave the last full measure of devotion that we herehighly resolve that these dead shall not have died invain that this nation, under God, shall have a newbirth of freedom and that government of the people, bythe people, for the people, shall not perish from theearth.
"""
def count_words(text:str)->int:
#First split the string into separate
lines
lines = text.split("\n")
count = 0
#For each line in text, do this
for line in lines:
#List of punctuations to
remove from text
punctuations =
list(",.?!:'\"")
#Check for each
character in text
for char in line:
if char in punctuations:
line = line.replace(char, "")
#Now split string using
whitespace
lst = line.split("
")
#Do not count any empty
string
for i in
range(len(lst)):
if len(lst[i])!=0:
count += 1
return count
def words_per_sentence(text:str)->float:
"""Function to calculate average words per
sentence in the text"""
sentences = text.split(". ")
#List to store the word count for each
sentence
lst = []
for sentence in sentences:
lst.append(count_words(sentence))
#Calculate word per sentence
wps = sum(lst)/len(lst)
return wps
def word_count(text, punctuation=".,?;"):
#List of all the words
words = []
#Dictionary for storing word count
count_dict = dict()
#First split the string into separate
lines
lines = text.split("\n")
#For each line in text, do this
for line in lines:
#Check for each character in
text
for char in line:
if char in punctuation:
line = line.replace(char, "")
#Now split string using
whitespace
lst = line.split("
")
#Append non-empty string
to list words
for s in lst:
if len(s)>0:
words.append(s)
#Generate dictionary now
for word in words:
if word in
count_dict.keys():
count_dict[word] += 1
else:
count_dict[word] = 1
return count_dict
def main():
gettysburg_address = """
Four score and seven years ago our fathers
brought forthon this continent, a new nation, conceived
in Liberty,and dedicated to the proposition that
all men are createdequal. Now we are engaged in a
great civil war, testingwhether that nation, or
any nation so conceived and sodedicated, can long
endure. We are met on a greatbattlefield of that
war. We have come to dedicate aportion of that
field, as a final resting place forthose who
here gave their lives that that nation mightlive. It is
altogether fitting and proper that we shoulddo
this. But, in a larger sense, we can not dedicate,
we can not consecrate, we can not hallow this
ground.The brave men, living and dead, who struggled
here,have consecrated it, far above our poor
power to addor detract. The world will little note, nor
longremember what we say here, but it can never
forget whatthey did here. It is for us the living,
rather, to bededicated here to the unfinished
work which they whofought here have thus far so
nobly advanced. It israther for us to be here
dedicated to the great taskremaining before us—that
from these honored dead wetake increased
devotion to that cause for which theygave the last full
measure of devotion that we herehighly resolve
that these dead shall not have died invain that this
nation, under God, shall have a newbirth of
freedom and that government of the people, bythe
people, for the people, shall not perish from
theearth.
"""
print("Count of words = ",
count_words(gettysburg_address))
print("Words per sentence = ",
words_per_sentence(gettysburg_address))
print("Word count:")
print(word_count(gettysburg_address))
if __name__=="__main__":
main()
Get Answers For Free
Most questions answered within 1 hours.