Remember that you can search in the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.
assets/googleu
Implement a function word_count_dict(filename) that counts how often each word appears in a text file and returns a dict of the form word_count_dict[word] = count.
word_count_dict(filename)
word_count_dict[word] = count
Use string.split() with no arguments to split on all whitespace. Use string.lower() to make all words lower case, so that 'The' and 'the' count as the same word.
string.split()
string.lower()
Please Login to take this test and advance your course progress
Try first to understand how using key=function in sorted(items, key=Function, reverse=Boolean) can help you sort items by their count number.
key=function
sorted(items, key=Function, reverse=Boolean)
Also remember that you can search in the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.
Now implement the previously explained function print_top(filename, n) using word_count_dict(filename) as a utility function.
print_top(filename, n)
Assume that n will be less than the length of the returned dict. Also, assume that you can just call word_count_dict(filename) and it will work as it's supposed to.
n
Remember that you can search in the 'assets/googleu' folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.
'assets/googleu'
Implement a print_words(filename) function that counts how often each word appears in a text file and prints:
print_words(filename)
word1 count1word2 count2...
Return the above list in order sorted by word (python will sort punctuation to come before letters -- that's fine).Use string.split() with no arguments to split on all whitespace. Use string.lower() to make all words lower case, so that 'The' and 'the' count as the same word.
Remember that you can search inside the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.
Implement the function mimic_dict(filename) that returns a dict that maps each word in the file with a list of all the words that immediately follow it inside the file. The list fo words can be in any order and should include duplicates. For example, the key "and" might have the list
mimic_dict(filename)
[ "then", "best", "then", "after", ... ]
Given that inside the file "and" was followed by "then", "best", once again "then", "after", and so on. We'll say that the empty string is what comes before the first word in the file.
Do a simple split() on whitespace to obtain all the words in a file. Rather than reading the file line by line, it's easier to read it into one giant string and split it once.
split()
Implement the function mimic(filename, n), as explained above, using the mimic_dict(filename) function as a utility function. Remember that the implemented function should return a word, then look up what words might come next and pick one at random as the next word, repeating the process for n random words (that'll print n+1 words!). Use an empty string ('') as the first word to prime things.
mimic(filename, n)
n+1
''
If you ever get stuck with a word that is not in the dict, go back to the empty string to keep things moving.
With a dict like the one returned by mimic_dict(filename) it should be fairly easy to emit random text that mimics the original. The standard python module random includes a random.choice(list) method which picks a random element from a non-empty list.
random
random.choice(list)
Assume that you can just call mimic_dict(filename) and it will work as it's supposed to.
Lecture 3's contents: - Dict type - Files and some utilities - Some exercises By Nick Parlante. Support materials and exercises: code.google.com
Please Login to post a question