I need help with part 2
My code for part 1 follows:
import re
# Input file name
file = input('Enter file name : ')
# Define the function
def file_to_wordlist(fname):
f = open(fname+'.txt','r')
lines = list(f.readlines())
wordlist = []
for i in lines :
t = re.sub(r'[^\w\s]','',i)
t = t.strip().lower()
wordlist = wordlist + t.split(' ')
wordlist = [i for i in wordlist if len(i)>0]
return wordlist
Extracted text: avoid using that approach, because the point of this exercise is to learn what a dictionary is and does. def wordlist_to_wordfreq(wordlist): wordfreq = {} # your code here return wordfreq %3D
Extracted text: Later in the course, we will use two basic but important data structures: dictionaries and priority queues. In this exercise, you will be asked to use both to parse and process text. To test your code, you'll need some text dump. Project Gutenberg (https://www.gutenberg.org/) has thousands of books in various formats, including plain text. For example, here you can find the full text of Alice in Wonderland: https://www . gutenberg.org/files/11/11-0.txt. You don't need to parse the entire book – a chapter or two are sufficient. 1. The first function you need to write takes a filename as input (fname) and returns a list con- taining the words that appear in the text. Only the words, no numbers, no punctuation. The words must be stored lowercase. Use the function str.lower () to make a string lowercase (see https://docs.python.org/2/library/stdtypes.html#str.lower). Use the function str.translate () (see https://docs.python.org/2/library/stdtypes.html#str.translate) to remove punctuation. To split a string into words, use str.split() (see https://docs. python.org/2/1ibrary/stdtypes.html#str.split). Treat the apostrophe as punctuation, so "I'm" would become “I m". Also, consider words like "rabbit-hole" in the original text as two separated words: "rabbit" and “hole". def file_to_wordlist(fname): wordlist = [] %3D # your code here return wordlist 2. The second function you need to write takes the word list generated by the previous func- tion and returns a dictionary (see https://docs.python.org/2/tutorial/datastructures. html#dictionaries) in which every word is associated with the number of times it appears in the word list. On StackOverflow you might find solutions that suggest to use Counter. Please