Skip to content

Commit b0e3f64

Browse files
author
codebasics
committed
pos
1 parent 9949f10 commit b0e3f64

File tree

4 files changed

+773
-0
lines changed

4 files changed

+773
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
}

7_pos/news_story.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
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.
2+
3+
The 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.
4+
5+
Removing volatile food and energy prices, so-called core CPI still rose 6.2%, against expectations for a 6% gain, clouding hopes that inflation had peaked in March.
6+
7+
The month-over-month gains also were higher than expectations — 0.3% on headline CPI versus the 0.2% estimate and a 0.6% increase for core, against the outlook for a 0.4% gain.
8+
9+
The price gains also meant that workers continued to lose ground. Real wages adjusted for inflation decreased 0.1% on the month despite a nominal increase of 0.3% in average hourly earnings. Over the past year, real earnings have dropped 2.6% even though average hourly earnings are up 5.5%.
10+
11+
Inflation has been the single biggest threat to a recovery that began early in the Covid pandemic and saw the economy in 2021 stage its biggest single-year growth level since 1984. Rising prices at the pump and in grocery stores have been one problem, but inflation has spread beyond those two areas into housing, auto sales and a host of other areas.
12+
13+
Federal Reserve officials have responded to the problem with two interest rate hikes so far this year and pledges of more until inflation comes down to the central bank’s 2% goal. However, Wednesday’s data shows that the Fed has a big job ahead.
14+
15+
Credits: cnbc.com

0 commit comments

Comments
 (0)