{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 6 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 6.5: Word Frequency" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T53. Create a new empty dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T54. As we saw earlier, trying to access an item that is not in the dictionary will cause an error:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T55. Use the `setdefault` method to initialize a count:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T56. Ask Python to print the contents of the dictionary to verify the new item has been added:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T57. Repeat the statement that updates the count and print the dictionary again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T58. Repeat the call to `setdefault` but this time with a different value:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T59. The return value from the previous call is a hint the new value was ignored. Print the dictionary again to make sure:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T60. Make a string to use in tests of `split`, `strip`, and `lower`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T61. To remove the dashes on each side of the string, pass the character `'-'` to `strip`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T62. If you ask Python to print `s` you will see that `strip`, like other string methods, does not modify `s` (strings are immutable):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T63. Calling the `lower` method will return a copy with all characters in lowercase:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T64. Combine the calls to `strip` and `lower` into a single statement:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T65. Import the string named `punctuation` from Python's string library:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T66. Tell Python to print the string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T67. Because the dash character is in `punctuation` we can also pass this string to `strip` to remove dashes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `wf` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the definition of the word frequency function (`wf`) from Figure 6.9 in the code cell below.\n", "* If you want to copy and paste the code you'll find it at `wf.py`\n", "* If you are going to enter your own version, consider copying the `wf` function you wrote for Section 6.3 and using it as a starting point and then typing in the definition of the `tokenize` function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T70. Make a string to test the tokenizer:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T71. Notice how `split` includes the comma with the second appearance of the word \"Tyger\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T72. Call `tokenize` and pass it your test string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T77. Test the `wf` function on the quote by Cicero:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T78. William Blake's poem named The Tyger is in a file named `tyger.txt`. Test the `wf` function on the complete poem, saving the result in a variable named `count`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T79. Find out how often \"Tyger\" appears in this file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ Do you think it's important that we call `strip` before calling `lower` when creating tokens? Explain the difference between these two expressions:\n", "
\n",
"s.strip(punctuation).lower()\n",
"s.lower().strip(punctuation)\n",
"\n",
"Would either one work in the `tokenize` function?"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}