SenpyClientUse.ipynb 6.64 KB
Newer Older
militarpancho's avatar
militarpancho committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
6
7
8
9
10
11
12
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-04-10T17:05:31.465571Z",
     "start_time": "2017-04-10T19:05:31.458282+02:00"
    },
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
13
14
15
16
17
18
   "source": [
    "# Client"
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
   "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
   },
militarpancho's avatar
militarpancho committed
34
35
36
37
38
39
40
   "source": [
    "Demo Endpoint\n",
    "-------------"
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
41
42
43
44
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
45
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
    "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."
militarpancho's avatar
militarpancho committed
47
48
49
50
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51
   "execution_count": null,
militarpancho's avatar
militarpancho committed
52
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53
54
55
56
57
58
59
    "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
militarpancho's avatar
militarpancho committed
60
61
62
63
64
   },
   "outputs": [],
   "source": [
    "from senpy.client import Client\n",
    "\n",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65
    "c = Client('http://latest.senpy.cluster.gsi.dit.upm.es/api')\n"
militarpancho's avatar
militarpancho committed
66
67
68
69
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
70
71
72
73
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
74
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
    "Now, let's use that client analyse some queries:"
militarpancho's avatar
militarpancho committed
76
77
78
79
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
   "execution_count": null,
militarpancho's avatar
militarpancho committed
81
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
82
83
84
85
86
87
88
    "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
militarpancho's avatar
militarpancho committed
89
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
90
   "outputs": [],
militarpancho's avatar
militarpancho committed
91
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
92
93
    "r = c.analyse('I like sugar!!', algorithm='sentiment140')\n",
    "r"
militarpancho's avatar
militarpancho committed
94
95
96
97
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
98
99
100
101
102
103
104
105
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-04-10T17:08:19.616754Z",
     "start_time": "2017-04-10T19:08:19.610767+02:00"
    },
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
106
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
107
    "As you can see, that gave us the full JSON result. A more concise way to print it would be:"
militarpancho's avatar
militarpancho committed
108
109
110
111
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
112
   "execution_count": null,
militarpancho's avatar
militarpancho committed
113
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
114
115
116
117
118
119
120
    "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
militarpancho's avatar
militarpancho committed
121
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
122
   "outputs": [],
militarpancho's avatar
militarpancho committed
123
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
124
125
    "for entry in r.entries:\n",
    "      print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity']))"
militarpancho's avatar
militarpancho committed
126
127
128
129
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
130
131
132
133
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
134
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
135
    "We can also obtain a list of available plugins with the client:"
militarpancho's avatar
militarpancho committed
136
137
138
139
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
140
   "execution_count": null,
militarpancho's avatar
militarpancho committed
141
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
142
143
144
145
146
147
148
    "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
militarpancho's avatar
militarpancho committed
149
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
150
   "outputs": [],
militarpancho's avatar
militarpancho committed
151
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
152
    "c.plugins()"
militarpancho's avatar
militarpancho committed
153
154
155
156
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
157
158
159
160
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
161
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
162
    "Or, more concisely:"
militarpancho's avatar
militarpancho committed
163
164
165
166
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
167
   "execution_count": null,
militarpancho's avatar
militarpancho committed
168
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
169
170
171
172
173
174
175
    "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
militarpancho's avatar
militarpancho committed
176
177
178
   },
   "outputs": [],
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
179
    "c.plugins().keys()"
militarpancho's avatar
militarpancho committed
180
181
182
183
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
184
185
186
187
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
188
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
189
190
191
192
193
194
195
196
197
198
199
200
    "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."
militarpancho's avatar
militarpancho committed
201
202
203
204
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
205
   "execution_count": null,
militarpancho's avatar
militarpancho committed
206
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
207
208
209
210
211
212
213
    "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
militarpancho's avatar
militarpancho committed
214
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
215
   "outputs": [],
militarpancho's avatar
militarpancho committed
216
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
217
    "!docker run -ti --name 'SenpyEndpoint' -d -p 6000:5000 gsiupm/senpy:0.8.6 --host 0.0.0.0 --default-plugins"
militarpancho's avatar
militarpancho committed
218
219
220
221
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
222
223
224
225
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
226
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
227
    "To use this endpoint:"
militarpancho's avatar
militarpancho committed
228
229
230
231
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
232
   "execution_count": null,
militarpancho's avatar
militarpancho committed
233
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
234
235
236
237
238
239
240
    "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
militarpancho's avatar
militarpancho committed
241
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
242
   "outputs": [],
militarpancho's avatar
militarpancho committed
243
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
244
    "c_local = Client('http://127.0.0.1:6000/api')"
militarpancho's avatar
militarpancho committed
245
246
247
248
   ]
  },
  {
   "cell_type": "markdown",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
249
250
251
252
   "metadata": {
    "deletable": true,
    "editable": true
   },
militarpancho's avatar
militarpancho committed
253
   "source": [
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
254
    "That's all! After you are done with your analysis, stop the docker container:"
militarpancho's avatar
militarpancho committed
255
256
257
258
   ]
  },
  {
   "cell_type": "code",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
259
   "execution_count": null,
militarpancho's avatar
militarpancho committed
260
   "metadata": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
261
262
263
264
265
266
267
    "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
militarpancho's avatar
militarpancho committed
268
   },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
269
   "outputs": [],
militarpancho's avatar
militarpancho committed
270
271
272
273
274
275
276
277
278
   "source": [
    "!docker stop SenpyEndpoint\n",
    "!docker rm SenpyEndpoint"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
279
   "display_name": "Python 3",
militarpancho's avatar
militarpancho committed
280
281
282
283
284
285
286
287
288
289
290
291
292
   "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",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
   "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
militarpancho's avatar
militarpancho committed
313
314
315
316
317
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}