{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Natural Language Toolkit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's load _The Gold Bug_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "THE GOLD-BUG\n", "\n", " What ho! what ho! this fellow is dancing mad!\n", "\n", " He hath been b\n" ] } ], "source": [ "with open(\"data/goldBug.txt\", \"r\") as f:\n", " goldBugString = f.read()\n", "print(goldBugString[:100])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's tokenize!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['the', 'gold-bug', 'what', 'ho', '!', 'what', 'ho', '!', 'this', 'fellow']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import nltk\n", "goldBugTokens = nltk.word_tokenize(goldBugString.lower())\n", "goldBugTokens[:10]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['the', 'what', 'ho', 'what', 'ho', 'this', 'fellow']\n", "['the', 'what', 'ho', 'what', 'ho', 'this', 'fellow']\n" ] } ], "source": [ "filterTokens = []\n", "for word in goldBugTokens[:10]:\n", " if word.isalpha():\n", " filterTokens.append(word)\n", "print(filterTokens)\n", "\n", "print([word for word in goldBugTokens[:10] if word.isalpha()])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "goldBugWords = [word for word in goldBugTokens if any([char for char in word if char.isalpha()])]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('the', 877),\n", " ('of', 465),\n", " ('and', 359),\n", " ('i', 336),\n", " ('to', 329),\n", " ('a', 327),\n", " ('in', 238),\n", " ('it', 213),\n", " ('you', 162),\n", " ('was', 137)]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wordFrequencies = nltk.FreqDist(goldBugWords)\n", "wordFrequencies.most_common(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now']\n" ] } ], "source": [ "stopwords = nltk.corpus.stopwords.words(\"English\")\n", "print(stopwords)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('upon', 81),\n", " ('de', 73),\n", " (\"'s\", 56),\n", " ('jupiter', 53),\n", " ('legrand', 47),\n", " ('one', 38),\n", " ('said', 35),\n", " ('well', 35),\n", " ('massa', 34),\n", " ('could', 33),\n", " ('bug', 32),\n", " ('skull', 29),\n", " ('parchment', 27),\n", " ('made', 25),\n", " ('tree', 25),\n", " ('first', 24),\n", " ('time', 24),\n", " ('two', 23),\n", " ('much', 23),\n", " ('us', 23)]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "goldBugFilteredWords = [word for word in goldBugWords if not word in stopwords]\n", "nltk.FreqDist(goldBugFilteredWords).most_common(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }