mirror of
https://github.com/gsi-upm/senpy
synced 2024-11-25 09:32:29 +00:00
188c33332a
It requires pandoc, which cannot be installed with pip. We can either link to the nbfile or convert the file manually/automatically: ``` nbconvert SenpyClientUse.ipynb --to rst ```
318 lines
6.6 KiB
Plaintext
318 lines
6.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2017-04-10T17:05:31.465571Z",
|
|
"start_time": "2017-04-10T19:05:31.458282+02:00"
|
|
},
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"# Client"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"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
|
|
},
|
|
"source": [
|
|
"Demo Endpoint\n",
|
|
"-------------"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from senpy.client import Client\n",
|
|
"\n",
|
|
"c = Client('http://latest.senpy.cluster.gsi.dit.upm.es/api')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Now, let's use that client analyse some queries:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"r = c.analyse('I like sugar!!', algorithm='sentiment140')\n",
|
|
"r"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2017-04-10T17:08:19.616754Z",
|
|
"start_time": "2017-04-10T19:08:19.610767+02:00"
|
|
},
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"As you can see, that gave us the full JSON result. A more concise way to print it would be:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for entry in r.entries:\n",
|
|
" print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity']))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We can also obtain a list of available plugins with the client:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"c.plugins()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Or, more concisely:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"c.plugins().keys()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"!docker run -ti --name 'SenpyEndpoint' -d -p 6000:5000 gsiupm/senpy:0.8.6 --host 0.0.0.0 --default-plugins"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"To use this endpoint:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"c_local = Client('http://127.0.0.1:6000/api')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"deletable": true,
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"That's all! After you are done with your analysis, stop the docker container:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"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
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"!docker stop SenpyEndpoint\n",
|
|
"!docker rm SenpyEndpoint"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"anaconda-cloud": {},
|
|
"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.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
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|