In: Computer Science
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:
g.
def big_words(text, min_length=10):
""" Return a list of big words whose length is at least min_length """
return []
h.
def common_words(text, min_frequency=10):
""" Return words occurring at least min_frequency times
as a list of (word, count) pairs """
return []
i.
def most_common_big_word(text, min_word_size=5):
""" Returns the most common BIG word and its frequency
as a tuple. A big word is defined as having a
length >= min_word_size """
return ("some_big_word", 1000)
j.
def avg_word_length(text):
""" Returns the average length (number of letters) of the words in text """
return 0.0
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.
"""
Python Code:
def big_words(text, min_length=10):
""" Return a list of big words whose length is at least min_length """
text = text.replace('.', ',').replace(' ', ',').replace('\n',',')
returnList = [string for string in text.split(',') if len(string)>=min_length]
return returnList
def common_words(text, min_frequency=10):
""" Return words occurring at least min_frequency times
as a list of (word, count) pairs """
countDict = {}
text = text.replace('.', ',').replace(' ', ',').replace('\n',',')
for string in text.split(','):
if string in countDict.keys():
countDict[string] = countDict[string] + 1
else:
countDict[string] = 0
returnList = [(key,countDict[key]) for key in countDict.keys() if countDict[key]>=min_frequency and key != '']
return returnList
def most_common_big_word(text, min_word_size=5):
""" Returns the most common BIG word and its frequency
as a tuple. A big word is defined as having a
length >= min_word_size """
bigWords = big_words(text,min_word_size)
comwords = common_words(text, 0)
comBigCount = [count for word,count in comwords if word in bigWords]
mostComBigWord = [word for word,count in comwords if count == max(comBigCount) and word in bigWords ]
mostComBigWordCount = [count for word,count in comwords if count == max(comBigCount) and word in bigWords ]
if len(mostComBigWord)>0:
return (mostComBigWord[0], mostComBigWordCount[0])
else:
return ('','')
def avg_word_length(text):
""" Returns the average length (number of letters) of the words in text """
text = text.replace('.', ',').replace(' ', ',')
strings = [len(string) for string in text.split(',') if string != '']
return sum(strings)/len(strings)
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.
"""
print(big_words(gettysburg_address))
print(common_words(gettysburg_address))
print(most_common_big_word(gettysburg_address))
print(avg_word_length(gettysburg_address))
Sample Output:
['proposition', 'createdequal', 'testingwhether', 'sodedicated',
'greatbattlefield', 'altogether', 'consecrate', 'consecrated',
'longremember', 'bededicated', 'unfinished', 'taskremaining',
'herehighly', 'government']
[('that', 11)]
('nation', 4)
4.663967611336032