Skip to content

Commit

Permalink
pos
Browse files Browse the repository at this point in the history
  • Loading branch information
codebasics committed May 11, 2022
1 parent 9949f10 commit b0e3f64
Show file tree
Hide file tree
Showing 4 changed files with 773 additions and 0 deletions.
221 changes: 221 additions & 0 deletions 7_pos/Exercise/pos_exercise_solution.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import spacy\n",
"nlp = spacy.load(\"en_core_web_sm\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Read a new story</h3>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'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'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with open(\"news_story.txt\",\"r\") as f:\n",
" news_text = f.read()\n",
" \n",
"news_text[:500]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Extract NOUN and NUM tokens</h3>"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"doc = nlp(news_text)\n",
"\n",
"numeral_tokens = []\n",
"noun_tokens = []\n",
"\n",
"for token in doc:\n",
" if token.pos_ == \"NOUN\":\n",
" noun_tokens.append(token)\n",
" elif token.pos_ == 'NUM':\n",
" numeral_tokens.append(token)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[8.3, 8.1, 1982, 6.2, 6, 0.3, 0.2, 0.6, 0.4, 0.1]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numeral_tokens[:10]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[Inflation,\n",
" climb,\n",
" consumers,\n",
" brink,\n",
" expansion,\n",
" consumer,\n",
" price,\n",
" index,\n",
" measure,\n",
" prices]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"noun_tokens[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Print a count of all POS tags</h3>"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{92: 96,\n",
" 100: 29,\n",
" 86: 15,\n",
" 85: 39,\n",
" 96: 17,\n",
" 97: 33,\n",
" 90: 34,\n",
" 95: 4,\n",
" 87: 13,\n",
" 89: 10,\n",
" 84: 23,\n",
" 103: 7,\n",
" 93: 19,\n",
" 94: 4,\n",
" 98: 8,\n",
" 101: 1}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count = doc.count_by(spacy.attrs.POS)\n",
"count"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NOUN | 96\n",
"VERB | 29\n",
"ADV | 15\n",
"ADP | 39\n",
"PROPN | 17\n",
"PUNCT | 33\n",
"DET | 34\n",
"PRON | 4\n",
"AUX | 13\n",
"CCONJ | 10\n",
"ADJ | 23\n",
"SPACE | 7\n",
"NUM | 19\n",
"PART | 4\n",
"SCONJ | 8\n",
"X | 1\n"
]
}
],
"source": [
"for k,v in count.items():\n",
" print(doc.vocab[k].text, \"|\",v)"
]
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
15 changes: 15 additions & 0 deletions 7_pos/news_story.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.

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.

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.

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.

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%.

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.

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.

Credits: cnbc.com
Loading

0 comments on commit b0e3f64

Please sign in to comment.