Skip to content

Instantly share code, notes, and snippets.

@sloria
Created September 6, 2013 18:35
Show Gist options
  • Save sloria/6468055 to your computer and use it in GitHub Desktop.
Save sloria/6468055 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "A Tour of TextBlob"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A Tour of TextBlob"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from text.blob import TextBlob"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text = \"TextBlob and Ipython Notebook make a perfect match.\"\n",
"blob = TextBlob(text)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's see the magic."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.tags"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"[(u'TextBlob', u'NNP'),\n",
" (u'and', u'CC'),\n",
" (u'Ipython', u'NNP'),\n",
" (u'Notebook', u'NNP'),\n",
" (u'make', u'VB'),\n",
" (u'a', u'DT'),\n",
" (u'perfect', u'JJ'),\n",
" (u'match', u'NN')]"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.noun_phrases"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 6,
"text": [
"WordList([u'textblob', u'ipython notebook'])"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.sentiment"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"(1.0, 1.0)"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob = TextBlob(\"I had a great time last night. The beer was good. But the hangover was horrible.\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.words"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"text": [
"WordList([u'I', u'had', u'a', u'great', u'time', u'last', u'night', u'The', u'beer', u'was', u'good', u'But', u'the', u'hangover', u'was', u'horrible'])"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.sentences"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": [
"[Sentence('I had a great time last night.'),\n",
" Sentence('The beer was good.'),\n",
" Sentence('But the hangover was horrible.')]"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for s in blob.sentences:\n",
" print(s)\n",
" print(s.sentiment)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I had a great time last night.\n",
"(0.4, 0.4083333333333333)\n",
"The beer was good.\n",
"(0.7, 0.6000000000000001)\n",
"But the hangover was horrible.\n",
"(-1.0, 1.0)\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.translate(to=\"it\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 15,
"text": [
"TextBlob(u'Ho avuto un grande momento ieri sera. La birra era buona . Ma la sbornia era orribile .')"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"blob.words.count(\"was\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 17,
"text": [
"2"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment