I'm having a bit of difficulty figuring this out. This function is supposed to do the following/adhere to the following guidelines:
def mostCommonWords(filename, N):
return "stub"
- Read the file from filename in your function and returns a dictionary
with the frequency of each word as its value.
- Words are separated by whitespace characters, but do not include
the following punctuation characters (,.!?;). You can assume contractions
count as one word (i.e. "don't", "you'll", etc. are one word).
- The split and strip functions may be useful.
- You can assume contractions count as one word
(i.e. "don't", "you'll", etc. are one word).
- Your function should open the file for reading, and close
the file before returning.
I have the helper function completed:
def wordFrequency(filename):
frequency = {}
file = open(filename, 'r')
for line in file.readlines():
for word in line.strip().split():
if word not in frequency:
frequency[word] = 0
frequency[word] += 1
file.close()
return frequency
However, I am unsure how to go from here. Could anyone provide some guidance?
I guess the next step would be to learn how to sort your result from highest to lowest, and then decide a way to represent this information to the user. Probably the simplest would be to print it.
I guess the N parameter intends to limit how many values(words) you print?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.