Question

Write a function called most_common_word that takes a phrase as input and returns the most common...

Write a function called most_common_word that takes a phrase as input and returns the most common word in the phrase. If multiple words are equally common then return the first word alphabetically.

Example:

    >>> phrase = "she sells sea shells by the sea shore"

>>> unique_words(phrase)
['by', 'sea', 'sells', 'she', 'shells', 'shore', 'the']

>>>count_word(phrase, 'sea') 2

>>>count_all_words(phrase) [['by', 1],

     ['sea', 2],
     ['sells', 1],
     ['she', 1],
     ['shells', 1],
     ['shore', 1],
     ['the', 1]]

>>>most_common_word(phrase) 'sea'

Python Script for ATOM or REPL, (NOT SHELL)

Homework Answers

Answer #1

RAW CODE

phrase = "she sells sea shells by the sea shore"

def most_common_word(data):
    data = data.split()
    frequency = {}
    for word in data:
        frequency[word] = data.count(word)
    return max(sorted(frequency),key=frequency.get) 


print(most_common_word(phrase))

SCREENSHOT (CODE AND OUTPUT)

##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called count_word that takes a phrase and a word as arguments and returns...
Write a function called count_word that takes a phrase and a word as arguments and returns a count of the number of occurrences of that particular word in the phrase. The function should return an integer. Python Script in ATOM or REPL
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
Please write the python code for a function called called count_all_words that takes a phrase as...
Please write the python code for a function called called count_all_words that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. (See example below.) Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a function called sum_half that takes as input a square matrix A and computes the...
Write a function called sum_half that takes as input a square matrix A and computes the sum of its elements that are in the upper right triangular part of A, that is, elements in the diagonal and elements that are to the right of it. For example, if the input is [1 2 3; 4 5 6; 7 8 9], then the function would return 26. (That is, 1+2+3+5+6+9) Note, the function triu is not allowed. Please write as you...
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements:...
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements: Has a main function: That asks the user to input a sentence Then asks the user to enter a list of words they want to replace (separated by spaces) Calls a function called replaceMultiWords passing those two parameters, which returns a filtered string Prints out the filtered (censored) string to the console Has a replaceMultiWords function that: Takes a string "sentence" and a string...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...