I need help with part 2 My code for part 1 follows: import re # Input file namefile = input('Enter file name : ') # Define the functiondef file_to_wordlist(fname): f = open(fname+'.txt','r') lines =...


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


avoid using that approach, because the point of this exercise is to learn what a dictionary is<br>and does.<br>def wordlist_to_wordfreq(wordlist):<br>wordfreq = {}<br># your code here<br>return wordfreq<br>%3D<br>

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
Later in the course, we will use two basic but important data structures: dictionaries and priority<br>queues. In this exercise, you will be asked to use both to parse and process text.<br>To test your code, you'll need some text dump. Project Gutenberg (https://www.gutenberg.org/)<br>has thousands of books in various formats, including plain text. For example, here you can find<br>the full text of Alice in Wonderland: https://www . gutenberg.org/files/11/11-0.txt. You don't<br>need to parse the entire book – a chapter or two are sufficient.<br>1. The first function you need to write takes a filename as input (fname) and returns a list con-<br>taining the words that appear in the text. Only the words, no numbers, no punctuation. The<br>words must be stored lowercase. Use the function str.lower () to make a string lowercase<br>(see https://docs.python.org/2/library/stdtypes.html#str.lower). Use the function<br>str.translate () (see https://docs.python.org/2/library/stdtypes.html#str.translate)<br>to remove punctuation. To split a string into words, use str.split() (see https://docs.<br>python.org/2/1ibrary/stdtypes.html#str.split). Treat the apostrophe as punctuation,<br>so

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
Jun 07, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here