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.
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:
[ ]
#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:
Hints:
[ ]
# 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:
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:
[ ]
#Put code for Exercise 4 here
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:
Get Answers For Free
Most questions answered within 1 hours.