Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use defaultdict in buildWordDict #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Chapter09_NaturalLanguages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"from bs4 import BeautifulSoup\n",
"import re\n",
"import string\n",
"from collections import Counter\n",
"from collections import Counter, defaultdict\n",
"\n",
"def cleanSentence(sentence):\n",
" sentence = sentence.split(' ')\n",
Expand Down Expand Up @@ -187,13 +187,8 @@
" # Filter out empty words\n",
" words = [word for word in words if word != '']\n",
"\n",
" wordDict = {}\n",
" wordDict = defaultdict(lambda: defaultdict(int))\n",
" for i in range(1, len(words)):\n",
" if words[i-1] not in wordDict:\n",
" # Create a new dictionary for this word\n",
" wordDict[words[i-1]] = {}\n",
" if words[i] not in wordDict[words[i-1]]:\n",
" wordDict[words[i-1]][words[i]] = 0\n",
" wordDict[words[i-1]][words[i]] += 1\n",
" return wordDict\n",
"\n",
Expand Down Expand Up @@ -305,8 +300,17 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
}