Question

Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...

Python (Using for reference please comment which is which!)

Exercise 1: Store Unique Words as Dictionary Keys

Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key.

  1. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters "done."
  2. After finishing the loop, print the contents of the dictionary.

Hint: You can use the in operator as a fast way to check whether a string is in the dictionary.

[ ]

Put code for Exercise 1 here

Exercise 2: Maintain word counts for unique words from (Ex 1)

A common use-case for dictionaries is to store word counts. Let's modify our program from Exercise 1 to store the count for each unique word. Let's name the dictionary word_counts.

This means you need to initialize the count value to 1 for a particular word if you see it for the first time (i.e., it's not already in word_counts) and update the count (by adding 1 to it) if the word is already present in word_counts.

Hints:

  • You can reuse Exercise 1 for this part
  • You can also look at Part 2 of project 2

[ ]

#Put code for Exercise 2 here

Exercise 3: Populate a list from a file of individual records

For the noisewords file (also called stopwords), each record in the file contains a different word. For this assignment:

  1. create a function that takes in the name of a file and returns a list. If the file is not found, the list returned should contain one entry that is a message that indicates the file is was not accessed.
  2. Create a function that will remove noise words from a long string and then return just the other words
  3. Create a test loop where the user can enter a phrase and that phrase is sent to the function and the results returned and printed for the user to see.

Hints:

  • You will be creating a list by reading successive records in a file rather than a single record as most people did in the project.
  • You will need to split the string into a list in the function and then recreate the single string before returning it after the noise words are removed.
  • You will probably use the .remove() function for the list, although there are other ways.

[ ]

# Put code for Exercise 3 here

Exercise 4: Process a text file removing characters and "noise" words.

This program will work very much like Part four of Project 2 where three files are read in:

  • A document file about a topic relevant to the Pod
  • A .txt file with a list of characters to be removed
  • A .txt file with a list of "noise" words to be removed.

In addition to calling a function to remove characters and replace them with spaces, this program should count how many times different words are used in the document in a dictionary. Words that are all CAPS, all numeric, or are in the "noise" words file (ex: the, of, and, etc.) are not to be put into the dictioary of counts. Only those words that have unique content are to be put into the final dictionary count.

At the end of the program, print out the words in dictionary in descending order of the count.

Hints:

  • You CAN reuse your Project 2 code for this assignment you till need to update it.
  • You should find a noise-word.txt file on ELMS to use
  • You will need the sorted method for the Dictionary

[ ]

#Put code for Exercise 4 here

Homework Answers

Answer #1

1.

"""
Python version : 3.6
Python program that stores unique words as keys in a dictionary.
"""

# create an empty dictionary
unique_keys = {}

word = ""
# loop that continues until user enters "done" to exit
while word != "done":
   # input the word
   word = input("Enter a word ('done' to exit): ")
  
   # check if user wants to exit
   if word != "done":
       # if word is not inserted in dictionary, insert it with value as 1
       if word not in unique_keys:
           unique_keys[word] = 1

# display the contents of the dictionary
print(unique_keys)
#end of program

Code Screenshot:

Output:

2.

"""
Python version : 3.6
Python program that stores unique words as keys and its frequency as value in a dictionary.
"""

# create an empty dictionary
word_counts = {}

word = ""

# loop that continues until user enters "done" to exit
while word != "done":
  
   # input the word
   word = input("Enter a word ('done' to exit): ")
  
   # check if user wants to exit
   if word != "done":
      
       # if word is found in dictionary, increment its value by 1
       if word in word_counts:
           word_counts[word] += 1
      
       else:   # if word is not found in dictionary, insert it with value as 1
           word_counts[word] = 1
          

# display the contents of the dictionary
print(word_counts)
#end of program

Code Screenshot:

Output:

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
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible for typing (the driver) and the other reviewing each line of code and making suggestions (the navigator) you must switch roles after every problem. 1). List Construction Write a Python program that prompts the user to enter a sequence of lowercase words and stores in a list only those words whose first letter occurs again elsewhere in the word (e.g., "baboon"). Continue entering words...
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are...
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are resolved using separate chaining. The hash table will store objects of the class Data. You will decide on the size of the table, keeping in mind that the size of the table must be a prime number. A table of size between 5000-10000, should work well. You must design your hash function so that it produces few collisions. A bad hash function that induces...
WRITE IN PYTHON Using any large text file or any literature English book in .txt format....
WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information Write a module called, “book_digest”. The module must have the following functions: ● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words. Here is how your program should work...
Please answer this question using python programming only and provide a screen short for the code....
Please answer this question using python programming only and provide a screen short for the code. Reading: P4E 7; Tut 7.2, 7.2.1 Upload an original Python script that satisfies the following criteria: It has at least two functionsFunction 1: This function has one parameter, a string containing the path to an existing text file that will be read by your function Using a with statement, the function opens the file indicated by the parameter for reading The function counts the...
Write a program that translates a sentence into Pig Latin. Recall that this means that, for...
Write a program that translates a sentence into Pig Latin. Recall that this means that, for a given word: Develop this program in steps: 1 – prompt the user for a word 2 – using a while loop (NOTE: conditions should be that your index is less than the length of the word AND that the letter at the current index is a vowel) COUNT the number of consonants (i.e. non-vowels) at the start of the word. Note, for a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT