Question

Write a Python program stored in a file q6.py that takes a text (without punctuation) as...

Write a Python program stored in a file q6.py that takes a text
(without punctuation) as input and prints the number of occurrences of each word.
Your program is case-insensitive, meaning that uppercase and lowercase of the same
letter is considered the same. Printing should be done in decreasing order of the number
of occurrences. If several words have the same number of occurrences, they should be
printed in alphabetical order. A new line must be printed between the words that have
different number of appearances.
Use a Python dictionary to store the letters and the count of letters.

ex:

Enter text : You cannot end a sentence with because because
because is a conjunction

Word Statistics :
because appeared 3 times .

a appeared 2 times .

cannot appeared 1 times .
conjunction appeared 1 times .
end appeared 1 times .
is appeared 1 times .
sentence appeared 1 times .
with appeared 1 times .
you appeared 1 times

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the complete code as per the requirements. It is well explained inside the code using comments.

# read text
text = input('Enter text: ')

# split the words
words = text.split()

# create a dictinary
freq = {}

# loop through all words
for word in words:
    word = word.lower()
    # if word exists in dictionary
    # increment the value by 1
    if word in freq:
        freq[word] += 1
    # otherwise initialize the value by 1
    else:
        freq[word] = 1

# sort the words in decreasing value
sorted_words = sorted(freq, key=freq.get, reverse=True)

count = freq[sorted_words[0]]
# print the word statistics
print('Word Statistics :')
for word in sorted_words:
    # check for new line
    if count != freq[word]:
        print()
    print(word, 'appeared', freq[word], 'times.')
    count = freq[word]

Below is the screenshot of code:

Below is the sample output:

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a Python program stored in a file q6.py that takes a text (without punctuation) as...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a program that inputs a text file. The program should print the unique words in...

    Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Note: The sort function sorts first numbers, then capital letters, then lowercase letters. That is fine for this assignment as you can see from the sample output. But even if the same word appears multiple times in the file it should only be displayed once in the output. (python)

  • Part 1. Write a program that takes a string as input, strips whitespace and punctuation from...

    Part 1. Write a program that takes a string as input, strips whitespace and punctuation from the words, and converts them to lowercase. Hint: The string module provides strings named whitespace, which contains space, tab, newline, etc., and punctuation which contains the punctuation characters. Let’s see if we can make Python swear: >>> import string >>> print string.punctuation !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ Also, you might consider using the string methods strip, replace and translate (Chapter 2 of "Introducing Python" is a great reference...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • For this exercise code a program in python which will read the text from the file...

    For this exercise code a program in python which will read the text from the file in.txt. Then this program converts all the lowercase letters to the corresponding uppercase letters. The results will be written to the file out.txt. The ide for python i am using is python 3.7 input: it will be cold as hell in the winter output: IT WILL BE COLD AS HELL IN THE WINTER

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • This program is in python and thanks fro whoever help me. In this program, you will...

    This program is in python and thanks fro whoever help me. In this program, you will build an English to Hmong translator program. Hmong is a language widely spoken by most Southeast Asian living in the twin cities. The program lets the user type in a sentence in English and then translate it to a Hmong sentence. The program does not care about grammar or punctuation marks. That means your program should remove punctuation marks from the English words before...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT