Tags: %TAGME{ tpaction="" web="Main" tag="" }% view all tags

Dictionary - Word Frequency

Step One

Consider the following dictionary:


Can we write code to add 1 to the number associated with Kenyon?

Step Two

Write a function countWord(d, w) to add a word to the dictionary d, with a count of 1, if it not in the dictionary, and increment the count if it is in the dictionary.

Note that we can tell if a word key is in the dict with:

    if word in freqList.keys(0):

Step Three

Write a function countWords(d,s) that we pass a dictionary d and a string s to it. For each word in the string it adds the word to the dictionary if it's not there, with a 1 as the count, or it increments that count for that word is if is.

Step Four

Consider the code we wrote to find and count the words in Emily Dickenson. Can we improve this codes with a dictionary?

Now let's review a solution that counts a single word:


Let's fork this and try to modify it.

Step Five

Modify the code to use the countWords function to first create an index of all the words in Emily Dickinson.

Step Six

Write a function lookupFreq(w) that looks up the frequency of word w, and return the number. If the word is not in the list, return 0. Create a loop to call this function after asking for a word.

Step Seven

Write a function displayWordFreq(d) that, giving dictionary d from above, displays all the words and their associated frequency. amplitude 1
awe 2
prospective 1
gold 3
lay 2
unmentioned 1
develops 1
weed 1

Step 8

Consider how you can print out a sorted list of words with frequency? You can't sort a dictionary.



Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2020-03-18 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback