responses Hi Hi Hello Hello Hello Sup how are you doing pretty good th

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
{
"responses": {
"Hi": ["Hi", "Hello"],
"Hello": ["Hello", "Sup"],
"how are you doing": ["pretty good, thank you"],
"what day is it": ["it is monday"]
}
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package punkt to /Users/lobster/nltk_data...\n",
"[nltk_data] Package punkt is already up-to-date!\n",
"[nltk_data] Downloading package wordnet to /Users/lobster/nltk_data...\n",
"[nltk_data] Package wordnet is already up-to-date!\n",
"[nltk_data] Downloading package stopwords to\n",
"[nltk_data] /Users/lobster/nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import nltk\n",
"import os\n",
"import os.path\n",
"import json\n",
"import string\n",
"import random\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"from nltk.corpus import stopwords\n",
"\n",
"nltk.download('punkt') # first-time use only\n",
"nltk.download('wordnet') # first-time use only\n",
"nltk.download('stopwords') # first-time use only"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
"RESPONSE_TRESHOLD = 0.6"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"# it seems that nltk stopwords list is too big, lets use our own\n",
"class OurStopwords:\n",
" def words(self, _):\n",
" return ['a', 'the', 'is', 'are', 'will', 'was', 'were']\n",
" \n",
"stopwords = OurStopwords()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load database"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def LemTokens(tokens):\n",
" return [lemmer.lemmatize(token) for token in tokens]\n",
"\n",
"remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)\n",
"\n",
"def LemNormalize(text):\n",
" return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DB created\n"
]
}
],
"source": [
"class ResponseDB:\n",
" def __init__(self):\n",
" print(\"DB created\")\n",
" self.data = {}\n",
" if os.path.isfile(\"responses.db\"):\n",
" with open('responses.db') as f:\n",
" data = json.load(f)\n",
" self.data = data['responses']\n",
" \n",
" self.recomputeTfIdf()\n",
" \n",
" \n",
" def recomputeTfIdf(self):\n",
" self.tfidf = TfidfVectorizer()\n",
" self.tfidf_matrix = self.tfidf.fit_transform(list(self.data.keys()))\n",
" \n",
" \n",
" def getAnswersProb(self, raw):\n",
" phrase = LemNormalize(raw)\n",
" phrase = [e for e in phrase if e not in stopwords.words('english')]\n",
" print(phrase)\n",
" \n",
" tfidf = self.tfidf.transform( [' '.join(phrase)] )\n",
" print(\"tfidf\", tfidf)\n",
" print(\"tfidf_matrix\", self.tfidf_matrix)\n",
" \n",
" similarity = cosine_similarity(tfidf, self.tfidf_matrix)[0]\n",
" similarity = [(i, sim) for i, sim in enumerate(similarity)]\n",
" similarity = sorted(similarity, key=lambda x: x[1], reverse=True)\n",
" print(\"similarity\", similarity)\n",
" print(\"Top 3 phrases:\")\n",
" \n",
" result = []\n",
" for el in similarity:\n",
" result.append((el[1], self.data[list(self.data.keys())[el[0]]] ))\n",
" \n",
" \n",
" for i in range(max(len(similarity), 3)):\n",
" print(\" \", similarity[i], self.data[list(self.data.keys())[similarity[i][0]]])\n",
" \n",
" return result\n",
" \n",
" def getAnswers(self, raw):\n",
" ans = self.getAnswersProb(raw)\n",
" \n",
" result = [ e for e in ans if e[0] > RESPONSE_TRESHOLD ]\n",
" if len(result) == 0:\n",
" return None\n",
" \n",
" return random.choice(random.choice(result)[1])\n",
"\n",
" \n",
"\n",
"db = ResponseDB()"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['hi']\n",
"tfidf (0, 4)\t1.0\n",
"tfidf_matrix (0, 4)\t1.0\n",
" (1, 3)\t1.0\n",
" (2, 5)\t0.5\n",
" (2, 0)\t0.5\n",
" (2, 9)\t0.5\n",
" (2, 2)\t0.5\n",
" (3, 8)\t0.5\n",
" (3, 1)\t0.5\n",
" (3, 6)\t0.5\n",
" (3, 7)\t0.5\n",
"similarity [(0, 1.0), (1, 0.0), (2, 0.0), (3, 0.0)]\n",
"Top 3 phrases:\n",
" (0, 1.0) ['Hi', 'Hello']\n",
" (1, 0.0) ['Hello', 'Sup']\n",
" (2, 0.0) ['pretty good, thank you']\n",
" (3, 0.0) ['it is monday']\n"
]
},
{
"data": {
"text/plain": [
"[(1.0, ['Hi', 'Hello']),\n",
" (0.0, ['Hello', 'Sup']),\n",
" (0.0, ['pretty good, thank you']),\n",
" (0.0, ['it is monday'])]"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.getAnswersProb(\"Hi\")"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['hello']\n",
"tfidf (0, 3)\t1.0\n",
"tfidf_matrix (0, 4)\t1.0\n",
" (1, 3)\t1.0\n",
" (2, 5)\t0.5\n",
" (2, 0)\t0.5\n",
" (2, 9)\t0.5\n",
" (2, 2)\t0.5\n",
" (3, 8)\t0.5\n",
" (3, 1)\t0.5\n",
" (3, 6)\t0.5\n",
" (3, 7)\t0.5\n",
"similarity [(1, 1.0), (0, 0.0), (2, 0.0), (3, 0.0)]\n",
"Top 3 phrases:\n",
" (1, 1.0) ['Hello', 'Sup']\n",
" (0, 0.0) ['Hi', 'Hello']\n",
" (2, 0.0) ['pretty good, thank you']\n",
" (3, 0.0) ['it is monday']\n"
]
},
{
"data": {
"text/plain": [
"'Hello'"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.getAnswers(\"Hello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}