|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 4, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import spacy\n", |
| 10 | + "nlp = spacy.load(\"en_core_web_sm\")" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "<h3>Read a new story</h3>" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 3, |
| 23 | + "metadata": { |
| 24 | + "scrolled": true |
| 25 | + }, |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "data": { |
| 29 | + "text/plain": [ |
| 30 | + "'Inflation rose again in April, continuing a climb that has pushed consumers to the brink and is threatening the economic expansion, the Bureau of Labor Statistics reported Wednesday.\\n\\nThe consumer price index, a broad-based measure of prices for goods and services, increased 8.3% from a year ago, higher than the Dow Jones estimate for an 8.1% gain. That represented a slight ease from March’s peak but was still close to the highest level since the summer of 1982.\\n\\nRemoving volatile food and ene'" |
| 31 | + ] |
| 32 | + }, |
| 33 | + "execution_count": 3, |
| 34 | + "metadata": {}, |
| 35 | + "output_type": "execute_result" |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "with open(\"news_story.txt\",\"r\") as f:\n", |
| 40 | + " news_text = f.read()\n", |
| 41 | + " \n", |
| 42 | + "news_text[:500]" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "<h3>Extract NOUN and NUM tokens</h3>" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 16, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "doc = nlp(news_text)\n", |
| 59 | + "\n", |
| 60 | + "numeral_tokens = []\n", |
| 61 | + "noun_tokens = []\n", |
| 62 | + "\n", |
| 63 | + "for token in doc:\n", |
| 64 | + " if token.pos_ == \"NOUN\":\n", |
| 65 | + " noun_tokens.append(token)\n", |
| 66 | + " elif token.pos_ == 'NUM':\n", |
| 67 | + " numeral_tokens.append(token)" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 10, |
| 73 | + "metadata": { |
| 74 | + "scrolled": true |
| 75 | + }, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "data": { |
| 79 | + "text/plain": [ |
| 80 | + "[8.3, 8.1, 1982, 6.2, 6, 0.3, 0.2, 0.6, 0.4, 0.1]" |
| 81 | + ] |
| 82 | + }, |
| 83 | + "execution_count": 10, |
| 84 | + "metadata": {}, |
| 85 | + "output_type": "execute_result" |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "numeral_tokens[:10]" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 17, |
| 95 | + "metadata": { |
| 96 | + "scrolled": true |
| 97 | + }, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "data": { |
| 101 | + "text/plain": [ |
| 102 | + "[Inflation,\n", |
| 103 | + " climb,\n", |
| 104 | + " consumers,\n", |
| 105 | + " brink,\n", |
| 106 | + " expansion,\n", |
| 107 | + " consumer,\n", |
| 108 | + " price,\n", |
| 109 | + " index,\n", |
| 110 | + " measure,\n", |
| 111 | + " prices]" |
| 112 | + ] |
| 113 | + }, |
| 114 | + "execution_count": 17, |
| 115 | + "metadata": {}, |
| 116 | + "output_type": "execute_result" |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "noun_tokens[:10]" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "<h3>Print a count of all POS tags</h3>" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": 12, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/plain": [ |
| 138 | + "{92: 96,\n", |
| 139 | + " 100: 29,\n", |
| 140 | + " 86: 15,\n", |
| 141 | + " 85: 39,\n", |
| 142 | + " 96: 17,\n", |
| 143 | + " 97: 33,\n", |
| 144 | + " 90: 34,\n", |
| 145 | + " 95: 4,\n", |
| 146 | + " 87: 13,\n", |
| 147 | + " 89: 10,\n", |
| 148 | + " 84: 23,\n", |
| 149 | + " 103: 7,\n", |
| 150 | + " 93: 19,\n", |
| 151 | + " 94: 4,\n", |
| 152 | + " 98: 8,\n", |
| 153 | + " 101: 1}" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "execution_count": 12, |
| 157 | + "metadata": {}, |
| 158 | + "output_type": "execute_result" |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "count = doc.count_by(spacy.attrs.POS)\n", |
| 163 | + "count" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": 13, |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [ |
| 171 | + { |
| 172 | + "name": "stdout", |
| 173 | + "output_type": "stream", |
| 174 | + "text": [ |
| 175 | + "NOUN | 96\n", |
| 176 | + "VERB | 29\n", |
| 177 | + "ADV | 15\n", |
| 178 | + "ADP | 39\n", |
| 179 | + "PROPN | 17\n", |
| 180 | + "PUNCT | 33\n", |
| 181 | + "DET | 34\n", |
| 182 | + "PRON | 4\n", |
| 183 | + "AUX | 13\n", |
| 184 | + "CCONJ | 10\n", |
| 185 | + "ADJ | 23\n", |
| 186 | + "SPACE | 7\n", |
| 187 | + "NUM | 19\n", |
| 188 | + "PART | 4\n", |
| 189 | + "SCONJ | 8\n", |
| 190 | + "X | 1\n" |
| 191 | + ] |
| 192 | + } |
| 193 | + ], |
| 194 | + "source": [ |
| 195 | + "for k,v in count.items():\n", |
| 196 | + " print(doc.vocab[k].text, \"|\",v)" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "metadata": { |
| 201 | + "kernelspec": { |
| 202 | + "display_name": "Python 3", |
| 203 | + "language": "python", |
| 204 | + "name": "python3" |
| 205 | + }, |
| 206 | + "language_info": { |
| 207 | + "codemirror_mode": { |
| 208 | + "name": "ipython", |
| 209 | + "version": 3 |
| 210 | + }, |
| 211 | + "file_extension": ".py", |
| 212 | + "mimetype": "text/x-python", |
| 213 | + "name": "python", |
| 214 | + "nbconvert_exporter": "python", |
| 215 | + "pygments_lexer": "ipython3", |
| 216 | + "version": "3.8.5" |
| 217 | + } |
| 218 | + }, |
| 219 | + "nbformat": 4, |
| 220 | + "nbformat_minor": 4 |
| 221 | +} |
0 commit comments