2017-04-07 12:19:08 +00:00
{
"cells": [
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-10T17:05:31.465571Z",
"start_time": "2017-04-10T19:05:31.458282+02:00"
},
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
"# Client"
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"source": [
"The built-in senpy client allows you to query any Senpy endpoint. We will illustrate how to use it with the public demo endpoint, and then show you how to spin up your own endpoint using docker."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
"Demo Endpoint\n",
"-------------"
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"To start using senpy, simply create a new Client and point it to your endpoint. In this case, the latest version of Senpy at GSI."
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:12.827640Z",
"start_time": "2017-04-10T19:29:12.818617+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
"outputs": [],
"source": [
"from senpy.client import Client\n",
"\n",
2017-04-10 18:04:36 +00:00
"c = Client('http://latest.senpy.cluster.gsi.dit.upm.es/api')\n"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"Now, let's use that client analyse some queries:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:14.011657Z",
"start_time": "2017-04-10T19:29:13.701808+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"r = c.analyse('I like sugar!!', algorithm='sentiment140')\n",
"r"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-10T17:08:19.616754Z",
"start_time": "2017-04-10T19:08:19.610767+02:00"
},
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"As you can see, that gave us the full JSON result. A more concise way to print it would be:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:14.854213Z",
"start_time": "2017-04-10T19:29:14.842068+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"for entry in r.entries:\n",
" print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity']))"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"We can also obtain a list of available plugins with the client:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:16.245198Z",
"start_time": "2017-04-10T19:29:16.056545+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"c.plugins()"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"Or, more concisely:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:17.663275Z",
"start_time": "2017-04-10T19:29:17.484623+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
"outputs": [],
"source": [
2017-04-10 18:04:36 +00:00
"c.plugins().keys()"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"Local Endpoint\n",
"--------------"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"To run your own instance of senpy, just create a docker container with the latest Senpy image. Using `--default-plugins` you will get some extra plugins to start playing with the API."
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:20.637539Z",
"start_time": "2017-04-10T19:29:19.938322+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"!docker run -ti --name 'SenpyEndpoint' -d -p 6000:5000 gsiupm/senpy:0.8.6 --host 0.0.0.0 --default-plugins"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"To use this endpoint:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:21.263976Z",
"start_time": "2017-04-10T19:29:21.260595+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"c_local = Client('http://127.0.0.1:6000/api')"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "markdown",
2017-04-10 18:04:36 +00:00
"metadata": {
"deletable": true,
"editable": true
},
2017-04-07 12:19:08 +00:00
"source": [
2017-04-10 18:04:36 +00:00
"That's all! After you are done with your analysis, stop the docker container:"
2017-04-07 12:19:08 +00:00
]
},
{
"cell_type": "code",
2017-04-10 18:04:36 +00:00
"execution_count": null,
2017-04-07 12:19:08 +00:00
"metadata": {
2017-04-10 18:04:36 +00:00
"ExecuteTime": {
"end_time": "2017-04-10T17:29:33.226686Z",
"start_time": "2017-04-10T19:29:22.392121+02:00"
},
"collapsed": false,
"deletable": true,
"editable": true
2017-04-07 12:19:08 +00:00
},
2017-04-10 18:04:36 +00:00
"outputs": [],
2017-04-07 12:19:08 +00:00
"source": [
"!docker stop SenpyEndpoint\n",
"!docker rm SenpyEndpoint"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
2017-04-10 18:04:36 +00:00
"display_name": "Python 3",
2017-04-07 12:19:08 +00:00
"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",
2017-04-10 18:04:36 +00:00
"version": "3.6.0"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "68px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
2017-04-07 12:19:08 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 1
}