{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Tokenize text into words and sentences" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python pip intstall -U nltk" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.25.0\n" ] } ], "source": [ "import numpy\n", "\n", "print(numpy.__version__)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.8.1\n" ] } ], "source": [ "import nltk\n", "\n", "print(nltk.__version__)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#### Uses NLTK's recommended sentence tokenizer, the PunktSentenceTokenizer\n", "#### Uses NLTK's recommended word tokenizer, the TreebankWordTokenizer and the PunktSentencetokenizer" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from nltk.tokenize import word_tokenize\n", "from nltk.tokenize import sent_tokenize" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to /home/shoney/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nltk.download('punkt')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Does this tokenizer work?', 'These are two different sentences']\n" ] } ], "source": [ "sent_tokens = sent_tokenize('Does this tokenizer work? These are two different sentences')\n", "\n", "print(sent_tokens)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Does', 'this', 'tokenizer', 'work', '?']\n" ] } ], "source": [ "word_tokens = word_tokenize('Does this tokenizer work?')\n", "\n", "print(word_tokens)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#### Punkt tokenizer\n", "\n", "https://www.nltk.org/_modules/nltk/tokenize/punkt.html\n", "http://www.nltk.org/api/nltk.tokenize.html#module-nltk.tokenize.punkt\n", "\n", "A sentence tokenizer which uses an unsupervised algorithm to build a model for abbreviation words, collocations, and words that start sentences; and then uses that model to find sentence boundaries. This approach has been shown to work well for many European languages.\n", "\n", "It must be trained on a large collection of plaintext in the target language before it can be used.\n", "\n", "The NLTK data package includes a pre-trained Punkt tokenizer for English." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to /home/shoney/nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nltk.download('punkt')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Does this tokenizer work?', 'These are two different sentences']\n" ] } ], "source": [ "sent_tokens = sent_tokenize('Does this tokenizer work? These are two different sentences')\n", "\n", "print(sent_tokens)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Does', 'this', 'tokenizer', 'work', '?']\n" ] } ], "source": [ "word_tokens = word_tokenize('Does this tokenizer work?')\n", "\n", "print(word_tokens)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.', 'Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.', 'These', 'watches', 'cost', '$', '1500', '!', 'The', 'ball', 'is', 'in', 'your', 'court', '.', 'Mr.', 'Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D', '.']\n" ] } ], "source": [ "text = \"A bird in hand is worth two in the bush. \" +\\\n", " \"Good things come to those who wait. \" +\\\n", " \"These watches cost $1500! \" +\\\n", " \"The ball is in your court. \" +\\\n", " \"Mr. Smith Goes to Washington \" +\\\n", " \"Doogie Howser M.D.\"\n", "\n", "word_tokens = word_tokenize(text, language='english')\n", "print(word_tokens)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "41" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(word_tokens)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hand', 'is', 'worth', 'two', 'in']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "word_tokens[3:8]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "from nltk.tokenize.punkt import PunktSentenceTokenizer" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "pst = PunktSentenceTokenizer()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['A bird in hand is worth two in the bush.', 'Good things come to those who wait.', 'These watches cost $1500!', 'The ball is in your court.', 'Mr.', 'Smith Goes to Washington Doogie Howser M.D.']\n" ] } ], "source": [ "sent_tokens = pst.tokenize(text)\n", "\n", "print(sent_tokens)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0, 40), (41, 76), (77, 102), (103, 129), (130, 133), (134, 177)]\n" ] } ], "source": [ "span_tokens = pst.span_tokenize(text)\n", "\n", "print(list(span_tokens))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.'],\n", " ['Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.'],\n", " ['These', 'watches', 'cost', '$', '1500', '!'],\n", " ['The', 'ball', 'is', 'in', 'your', 'court', '.'],\n", " ['Mr.'],\n", " ['Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D', '.']]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentences = pst.sentences_from_tokens(word_tokens)\n", "\n", "list(sentences)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "from nltk.tokenize import WhitespaceTokenizer" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "wt = WhitespaceTokenizer()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush.', 'Good', 'things', 'come', 'to', 'those', 'who', 'wait.', 'These', 'watches', 'cost', '$1500!', 'The', 'ball', 'is', 'in', 'your', 'court.', 'Mr.', 'Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D.']\n" ] } ], "source": [ "word_tokens = wt.tokenize(text)\n", "\n", "print(word_tokens)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Reading Local Files" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: '/datasets/biography.txt'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[24], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39;49m(\u001b[39m'\u001b[39;49m\u001b[39m/datasets/biography.txt\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mr\u001b[39;49m\u001b[39m'\u001b[39;49m) \u001b[39mas\u001b[39;00m f:\n\u001b[1;32m 2\u001b[0m file_contents \u001b[39m=\u001b[39m f\u001b[39m.\u001b[39mread()\n\u001b[1;32m 4\u001b[0m \u001b[39mprint\u001b[39m(file_contents)\n", "File \u001b[0;32m~/dev/.venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m \u001b[39mif\u001b[39;00m file \u001b[39min\u001b[39;00m {\u001b[39m0\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m}:\n\u001b[1;32m 278\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 279\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mIPython won\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt let you open fd=\u001b[39m\u001b[39m{\u001b[39;00mfile\u001b[39m}\u001b[39;00m\u001b[39m by default \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 280\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 281\u001b[0m \u001b[39m\"\u001b[39m\u001b[39myou can use builtins\u001b[39m\u001b[39m'\u001b[39m\u001b[39m open.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m )\n\u001b[0;32m--> 284\u001b[0m \u001b[39mreturn\u001b[39;00m io_open(file, \u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/datasets/biography.txt'" ] } ], "source": [ "with open('/datasets/biography.txt', 'r') as f:\n", " file_contents = f.read()\n", "\n", "print(file_contents)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "word_tokens = word_tokenize(file_contents)\n", "\n", "print(word_tokens)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Frequency distributions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nltk.probability import FreqDist" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist = FreqDist(word_tokens)\n", "\n", "print(freq_dist)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist.most_common(20)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Return the frequency of a given sample. The frequency of a sample is defined as the count of that sample divided by the total number of sample outcomes that have been recorded by this FreqDist. The count of a sample is defined as the number of times that sample outcome was recorded by this FreqDist. Frequencies are always real numbers in the range [0, 1]." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist.freq('the')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist.freq('exposure')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "fig, ax = plt.subplots(figsize=(12, 8))\n", "\n", "freq_dist.plot(20, cumulative=False)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file_contents = file_contents.lower()\n", "\n", "word_tokens = wt.tokenize(file_contents)\n", "\n", "print(word_tokens)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nltk.download('stopwords')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nltk.corpus import stopwords\n", "\n", "stop_words = set(stopwords.words('english'))\n", "stop_words.update(['.', ',', 'The'])\n", "\n", "print(stop_words)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "filtered_words = []\n", "\n", "for w in word_tokens:\n", " if w not in stop_words:\n", " filtered_words.append(w)\n", " \n", "print(filtered_words)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist = FreqDist(filtered_words)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq_dist.most_common(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(12, 8))\n", "\n", "freq_dist.plot(20, cumulative=False)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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.9.7" } }, "nbformat": 4, "nbformat_minor": 2 }