...
|
...
|
@@ -1,24 +1,34 @@
|
|
1
|
+import re
|
|
2
|
+import sys
|
1
|
3
|
import hashlib
|
2
|
|
-import urllib
|
3
|
|
-import urllib2
|
4
|
|
-import cookielib
|
|
4
|
+
|
|
5
|
+if sys.version_info >= (3, 0):
|
|
6
|
+ from urllib.request import build_opener, HTTPCookieProcessor, urlopen
|
|
7
|
+ from urllib.parse import urlencode
|
|
8
|
+ import http.cookiejar as cookielib
|
|
9
|
+
|
|
10
|
+else:
|
|
11
|
+ from urllib import urlencode, urlopen
|
|
12
|
+ from urllib2 import build_opener, HTTPCookieProcessor
|
|
13
|
+ import cookielib
|
|
14
|
+
|
5
|
15
|
import uuid
|
6
|
16
|
import xml.dom.minidom
|
7
|
17
|
|
8
|
18
|
"""
|
9
|
19
|
chatterbotapi
|
10
|
20
|
Copyright (C) 2011 pierredavidbelanger@gmail.com
|
11
|
|
-
|
|
21
|
+
|
12
|
22
|
This program is free software: you can redistribute it and/or modify
|
13
|
23
|
it under the terms of the GNU Lesser General Public License as published by
|
14
|
24
|
the Free Software Foundation, either version 3 of the License, or
|
15
|
25
|
(at your option) any later version.
|
16
|
|
-
|
|
26
|
+
|
17
|
27
|
This program is distributed in the hope that it will be useful,
|
18
|
28
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
29
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
30
|
GNU Lesser General Public License for more details.
|
21
|
|
-
|
|
31
|
+
|
22
|
32
|
You should have received a copy of the GNU Lesser General Public License
|
23
|
33
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
24
|
34
|
"""
|
...
|
...
|
@@ -27,6 +37,8 @@ import xml.dom.minidom
|
27
|
37
|
# API
|
28
|
38
|
#################################################
|
29
|
39
|
|
|
40
|
+
|
|
41
|
+
|
30
|
42
|
class ChatterBotType:
|
31
|
43
|
|
32
|
44
|
CLEVERBOT = 1
|
...
|
...
|
@@ -91,18 +103,18 @@ class _CleverbotSession(ChatterBotSession):
|
91
|
103
|
self.vars['islearning'] = '1'
|
92
|
104
|
self.vars['cleanslate'] = 'false'
|
93
|
105
|
self.cookieJar = cookielib.CookieJar()
|
94
|
|
- self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar))
|
|
106
|
+ self.opener = build_opener(HTTPCookieProcessor(self.cookieJar))
|
95
|
107
|
self.opener.open(self.bot.baseUrl)
|
96
|
108
|
|
97
|
109
|
def think_thought(self, thought):
|
98
|
110
|
self.vars['stimulus'] = thought.text
|
99
|
|
- data = urllib.urlencode(self.vars)
|
|
111
|
+ data = urlencode(self.vars)
|
100
|
112
|
data_to_digest = data[9:self.bot.endIndex]
|
101
|
113
|
data_digest = hashlib.md5(data_to_digest).hexdigest()
|
102
|
114
|
data = data + '&icognocheck=' + data_digest
|
103
|
115
|
url_response = self.opener.open(self.bot.serviceUrl, data)
|
104
|
116
|
response = url_response.read()
|
105
|
|
- response_values = response.split('\r')
|
|
117
|
+ response_values = re.split(r'\\r|\r', response)
|
106
|
118
|
#self.vars['??'] = _utils_string_at_index(response_values, 0)
|
107
|
119
|
self.vars['sessionid'] = _utils_string_at_index(response_values, 1)
|
108
|
120
|
self.vars['logurl'] = _utils_string_at_index(response_values, 2)
|
...
|
...
|
@@ -152,8 +164,8 @@ class _PandorabotsSession(ChatterBotSession):
|
152
|
164
|
|
153
|
165
|
def think_thought(self, thought):
|
154
|
166
|
self.vars['input'] = thought.text
|
155
|
|
- data = urllib.urlencode(self.vars)
|
156
|
|
- url_response = urllib2.urlopen('http://www.pandorabots.com/pandora/talk-xml', data)
|
|
167
|
+ data = urlencode(self.vars)
|
|
168
|
+ url_response = urlopen('http://www.pandorabots.com/pandora/talk-xml', data)
|
157
|
169
|
response = url_response.read()
|
158
|
170
|
response_dom = xml.dom.minidom.parseString(response)
|
159
|
171
|
response_thought = ChatterBotThought()
|
...
|
...
|
@@ -178,3 +190,155 @@ def _utils_string_at_index(strings, index):
|
178
|
190
|
return strings[index]
|
179
|
191
|
else:
|
180
|
192
|
return ''
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+ CLEVERBOT = 1
|
|
197
|
+ JABBERWACKY = 2
|
|
198
|
+ PANDORABOTS = 3
|
|
199
|
+
|
|
200
|
+class ChatterBotFactory:
|
|
201
|
+
|
|
202
|
+ def create(self, type, arg = None):
|
|
203
|
+ if type == ChatterBotType.CLEVERBOT:
|
|
204
|
+ return _Cleverbot('http://www.cleverbot.com', 'http://www.cleverbot.com/webservicemin', 35)
|
|
205
|
+ elif type == ChatterBotType.JABBERWACKY:
|
|
206
|
+ return _Cleverbot('http://jabberwacky.com', 'http://jabberwacky.com/webservicemin', 29)
|
|
207
|
+ elif type == ChatterBotType.PANDORABOTS:
|
|
208
|
+ if arg == None:
|
|
209
|
+ raise Exception('PANDORABOTS needs a botid arg')
|
|
210
|
+ return _Pandorabots(arg)
|
|
211
|
+ return None
|
|
212
|
+
|
|
213
|
+class ChatterBot:
|
|
214
|
+
|
|
215
|
+ def create_session(self):
|
|
216
|
+ return None
|
|
217
|
+
|
|
218
|
+class ChatterBotSession:
|
|
219
|
+
|
|
220
|
+ def think_thought(self, thought):
|
|
221
|
+ return thought
|
|
222
|
+
|
|
223
|
+ def think(self, text):
|
|
224
|
+ thought = ChatterBotThought()
|
|
225
|
+ thought.text = text
|
|
226
|
+ return self.think_thought(thought).text
|
|
227
|
+
|
|
228
|
+class ChatterBotThought:
|
|
229
|
+
|
|
230
|
+ pass
|
|
231
|
+
|
|
232
|
+#################################################
|
|
233
|
+# Cleverbot impl
|
|
234
|
+#################################################
|
|
235
|
+
|
|
236
|
+class _Cleverbot(ChatterBot):
|
|
237
|
+
|
|
238
|
+ def __init__(self, baseUrl, serviceUrl, endIndex):
|
|
239
|
+ self.baseUrl = baseUrl
|
|
240
|
+ self.serviceUrl = serviceUrl
|
|
241
|
+ self.endIndex = endIndex
|
|
242
|
+
|
|
243
|
+ def create_session(self):
|
|
244
|
+ return _CleverbotSession(self)
|
|
245
|
+
|
|
246
|
+class _CleverbotSession(ChatterBotSession):
|
|
247
|
+
|
|
248
|
+ def __init__(self, bot):
|
|
249
|
+ self.bot = bot
|
|
250
|
+ self.vars = {}
|
|
251
|
+ self.vars['start'] = 'y'
|
|
252
|
+ self.vars['icognoid'] = 'wsf'
|
|
253
|
+ self.vars['fno'] = '0'
|
|
254
|
+ self.vars['sub'] = 'Say'
|
|
255
|
+ self.vars['islearning'] = '1'
|
|
256
|
+ self.vars['cleanslate'] = 'false'
|
|
257
|
+ self.cookieJar = cookielib.CookieJar()
|
|
258
|
+ self.opener = build_opener(HTTPCookieProcessor(self.cookieJar))
|
|
259
|
+ self.opener.open(self.bot.baseUrl)
|
|
260
|
+
|
|
261
|
+ def think_thought(self, thought):
|
|
262
|
+ self.vars['stimulus'] = thought.text
|
|
263
|
+ data = urlencode(self.vars)
|
|
264
|
+ data_to_digest = data[9:self.bot.endIndex]
|
|
265
|
+ data_digest = hashlib.md5(data_to_digest.encode('utf-8')).hexdigest()
|
|
266
|
+ data = data + '&icognocheck=' + data_digest
|
|
267
|
+ url_response = self.opener.open(self.bot.serviceUrl, data.encode('utf-8'))
|
|
268
|
+ response = str(url_response.read())
|
|
269
|
+ response_values = re.split(r'\\r|\r', response)
|
|
270
|
+ #self.vars['??'] = _utils_string_at_index(response_values, 0)
|
|
271
|
+ self.vars['sessionid'] = _utils_string_at_index(response_values, 1)
|
|
272
|
+ self.vars['logurl'] = _utils_string_at_index(response_values, 2)
|
|
273
|
+ self.vars['vText8'] = _utils_string_at_index(response_values, 3)
|
|
274
|
+ self.vars['vText7'] = _utils_string_at_index(response_values, 4)
|
|
275
|
+ self.vars['vText6'] = _utils_string_at_index(response_values, 5)
|
|
276
|
+ self.vars['vText5'] = _utils_string_at_index(response_values, 6)
|
|
277
|
+ self.vars['vText4'] = _utils_string_at_index(response_values, 7)
|
|
278
|
+ self.vars['vText3'] = _utils_string_at_index(response_values, 8)
|
|
279
|
+ self.vars['vText2'] = _utils_string_at_index(response_values, 9)
|
|
280
|
+ self.vars['prevref'] = _utils_string_at_index(response_values, 10)
|
|
281
|
+ #self.vars['??'] = _utils_string_at_index(response_values, 11)
|
|
282
|
+ self.vars['emotionalhistory'] = _utils_string_at_index(response_values, 12)
|
|
283
|
+ self.vars['ttsLocMP3'] = _utils_string_at_index(response_values, 13)
|
|
284
|
+ self.vars['ttsLocTXT'] = _utils_string_at_index(response_values, 14)
|
|
285
|
+ self.vars['ttsLocTXT3'] = _utils_string_at_index(response_values, 15)
|
|
286
|
+ self.vars['ttsText'] = _utils_string_at_index(response_values, 16)
|
|
287
|
+ self.vars['lineRef'] = _utils_string_at_index(response_values, 17)
|
|
288
|
+ self.vars['lineURL'] = _utils_string_at_index(response_values, 18)
|
|
289
|
+ self.vars['linePOST'] = _utils_string_at_index(response_values, 19)
|
|
290
|
+ self.vars['lineChoices'] = _utils_string_at_index(response_values, 20)
|
|
291
|
+ self.vars['lineChoicesAbbrev'] = _utils_string_at_index(response_values, 21)
|
|
292
|
+ self.vars['typingData'] = _utils_string_at_index(response_values, 22)
|
|
293
|
+ self.vars['divert'] = _utils_string_at_index(response_values, 23)
|
|
294
|
+ response_thought = ChatterBotThought()
|
|
295
|
+ response_thought.text = _utils_string_at_index(response_values, 16)
|
|
296
|
+ return response_thought
|
|
297
|
+
|
|
298
|
+#################################################
|
|
299
|
+# Pandorabots impl
|
|
300
|
+#################################################
|
|
301
|
+
|
|
302
|
+class _Pandorabots(ChatterBot):
|
|
303
|
+
|
|
304
|
+ def __init__(self, botid):
|
|
305
|
+ self.botid = botid
|
|
306
|
+
|
|
307
|
+ def create_session(self):
|
|
308
|
+ return _PandorabotsSession(self)
|
|
309
|
+
|
|
310
|
+class _PandorabotsSession(ChatterBotSession):
|
|
311
|
+
|
|
312
|
+ def __init__(self, bot):
|
|
313
|
+ self.vars = {}
|
|
314
|
+ self.vars['botid'] = bot.botid
|
|
315
|
+ self.vars['custid'] = uuid.uuid1()
|
|
316
|
+
|
|
317
|
+ def think_thought(self, thought):
|
|
318
|
+ self.vars['input'] = thought.text
|
|
319
|
+ data = urlencode(self.vars)
|
|
320
|
+ url_response = urlopen('http://www.pandorabots.com/pandora/talk-xml', data)
|
|
321
|
+ response = url_response.read()
|
|
322
|
+ response_dom = xml.dom.minidom.parseString(response)
|
|
323
|
+ response_thought = ChatterBotThought()
|
|
324
|
+ that_elements = response_dom.getElementsByTagName('that')
|
|
325
|
+ if that_elements is None or len(that_elements) == 0 or that_elements[0] is None:
|
|
326
|
+ return ''
|
|
327
|
+ that_elements_child_nodes = that_elements[0].childNodes
|
|
328
|
+ if that_elements_child_nodes is None or len(that_elements_child_nodes) == 0 or that_elements_child_nodes[0] is None:
|
|
329
|
+ return ''
|
|
330
|
+ that_elements_child_nodes_data = that_elements_child_nodes[0].data
|
|
331
|
+ if that_elements_child_nodes_data is None:
|
|
332
|
+ return ''
|
|
333
|
+ response_thought.text = that_elements_child_nodes_data.strip()
|
|
334
|
+ return response_thought
|
|
335
|
+
|
|
336
|
+#################################################
|
|
337
|
+# Utils
|
|
338
|
+#################################################
|
|
339
|
+
|
|
340
|
+def _utils_string_at_index(strings, index):
|
|
341
|
+ if len(strings) > index:
|
|
342
|
+ return strings[index]
|
|
343
|
+ else:
|
|
344
|
+ return ''
|
181
|
345
|
\ No newline at end of file
|