Find the most common words

Find the most common words

The code and directions are mentioned below-

The structure should look something like this

deffind_words(high_l, c_d)

#find the keys in c_d that correspond to the values in high_l

defget_highest_counts(c_d):

#find s highest values in c_d

#return that list

defget_word_count(s):

#make a dict of the word counts in s

#return that dict

def count_5(str):

counts = get_word_count(string)

highest = get_highest_counts(counts) #you can use this as a parameter because it is defined in the line before and it holds a value from being defined

top_5 = find_words (highest, counts)

return top_5 

Dictionaries

Overview

Write a module, most_common.py, with a function to determine the 5 most common
words in a string using a dictionary. Your function should take a string as a
parameter and return a list of the 5 most common words in that string.
Within the main body of your code, define a tuple of at least 3 text passages, each of
which should have 5 or more sentences. Here is a link to a site with
movie quotes to save you time: http://www.filmsite.org/greatfilmquotes.html.
Your program should print out a menu to the user with the titles of the passages in
your tuple and allow them to select which excerpt to count the common words in. Then, call your function with the chosen text passed in as an argument, and print out the returned value of the 5 most common words in the text.
Finally, allow the user to choose whether or not to pick another text to analyze. If
they choose yes, print out the menu again and repeat. If not, end the program. 

Solution 

deffind_words(high_l, c_d):

words=[]

i=0

while(len(words)<5):

for key in c_d:

if(c_d[key]==high_l[i]):

words.append((key,c_d[key]))

if(len(words)>=5):

break

i=i+1

if(i>len(high_l)-1):

break

return words

#find the keys in c_d that correspond to the values in high_l

defget_highest_counts(c_d):

h_counts=[]

minH=1000000

while(len(h_counts)<5):

max=0

for key in c_d:

ifc_d[key] > max and c_d[key]<minH :

max = c_d[key]

minH=max

h_counts.append(max)

#find s highest values in c_d

returnh_counts

#return that list

defget_word_count(s):

tokens = s.split(” “)

d={}

for i in range(len(tokens)):

if(not ((tokens[i]==””) or tokens[i]==” “)):

if(tokens[i] in d):

d[tokens[i]]+=1

else:

d[tokens[i]]=1

return d

#make a dict of the word counts in s

#return that dict

def count_5(st):

counts = get_word_count(st)

highest = get_highest_counts(counts) #you can use this as a parameter because it is defined in the line before and it holds a value from being defined

top_5 = find_words (highest, counts)

return top_5

# this function prints the passage choices and returns the user selection

defgetOption():

success = False

val=0

while(not success):

print(“Welcome to word counting\nPlease select a passage to count words”)

print(“1.Fight Club Rules”)

print(“2.Black Hawk Down quote”)

print(“3.The Departed Quote”)

print(“Please Enter option number of passage”)

try:

i = input()

val = int(i)

if(val>0 and val<4):

success=True

else:

print(“Please enter a valid option”)

exceptValueError:

print(“Please enter a valid option”)

returnval

# processes the quote statements to remove punctuations

defprocessString(str):

str=str.replace(“.”,” “)

str=str.replace(“,”,” “)

str=str.replace(“?”,””)

str=str.replace(“:”,””)

str=str.replace(”  “,” “)

str=str.lower()

returnstr

def main():

done = False

# looping to execute program while user chooses to continue

while (not done):

str1 = “1st RULE: You do not talk about FIGHT CLUB.2nd RULE: You DO NOT talk about FIGHT CLUB.3rd RULE: If someone says stop or goes limp, taps out the fight is over.4th RULE: Only two guys to a fight.5th RULE: One fight at a time.6th RULE: No shirts, no shoes.7th RULE: Fights will go on as long as they have to.8th RULE: If this is your first night at FIGHT CLUB, you HAVE to fight.”

str2 = “When I go home, people ask me: ‘Hey Hoot, why do you do it, man? Why, you some kinda war junkie?’ I won’t say a god-damn word. Why? They won’t understand. They won’t understand why we do it. They won’t understand it’s about the men next to ya, and that’s it. That’s all it is.”

str3 = “When you decide to be somethin’, you can be it. That’s what they don’t tell ya in the church. When I was your age they would say we could become cops or criminals. Today, what I’m sayin’ is this: when you’re facin’ a loaded gun, what’s the difference?”

quotes = (str1,str2,str3)

option  =getOption()

str=quotes[option-1]

print()

print(“counting words from below passage”)

print()

print(str)

print()

str = processString(str)

top_5=count_5(str)

print()

print(“top 5 words and their counts are “)

print()

for word in top_5:

print(word[0],word[1])

print()

# looping to validate user choice

while(True):

choice=input(“would you like to choose another passage? enter yes/no\n”)

choice = choice.lower()

if(choice==”no”):

print(“Exiting program”)

done = True

break

elif choice==”yes”:

break

else:

print(“please enter yes/no”)

main()