Skip to content

Instantly share code, notes, and snippets.

@Manikanta-Munnangi
Created November 18, 2019 14:41
Show Gist options
  • Save Manikanta-Munnangi/fee8b07a85c11710ec2f2dc8c3eb2be3 to your computer and use it in GitHub Desktop.
Save Manikanta-Munnangi/fee8b07a85c11710ec2f2dc8c3eb2be3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# spacy import convention\n",
"import spacy\n",
"\n",
"# load the english model into nlp object.\n",
"nlp=spacy.load('en_core_web_sm') "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Sample text\n",
"text=\"\"\"Success isn't easy, and that's a good thing - at least in business. \n",
"If it was easy, everybody would be doing it and your competition would be outrageous!\"\"\"\n",
"\n",
"# returns doc container with attributes \n",
"doc=nlp(text)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# loop through doc and return tokens with .text\n",
"# token.root access the actual noun.\n",
"tokenized=[(token.text,token.root) for token in doc.noun_chunks]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Success', Success),\n",
" ('a good thing', thing),\n",
" ('business', business),\n",
" ('it', it),\n",
" ('everybody', everybody),\n",
" ('it', it),\n",
" ('your competition', competition)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# print result (noun phrases)\n",
"tokenized"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment