├── .gitattributes ├── 9781484240953.jpg ├── Chapter II └── Chapter-II Jupyter Notebok.ipynb ├── Chapter III ├── OnlineEatsBot Agent Export from DialogFlow.zip └── flask_onlineeats_demo.zip ├── Chapter IV └── Chapter IV Code horoscope_bot.zip ├── Chapter V └── horoscope_bot Source Code (Chapter V).zip ├── Contributing.md ├── LICENSE.txt └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484240953.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-chatbots-with-python/e84594620087ddde5b12c723b839d1efd605c11a/9781484240953.jpg -------------------------------------------------------------------------------- /Chapter II/Chapter-II Jupyter Notebok.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 53, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "'2.0.11'" 12 | ] 13 | }, 14 | "execution_count": 53, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "import spacy\n", 21 | "spacy.__version__" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 17, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "Collecting https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz\n", 34 | "\u001b[?25l Downloading https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz (37.4MB)\n", 35 | "\u001b[K 100% |████████████████████████████████| 37.4MB 7.9MB/s ta 0:00:0111\n", 36 | "\u001b[?25hRequirement already satisfied (use --upgrade to upgrade): en-core-web-sm==2.0.0 from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz in /usr/local/lib/python2.7/site-packages\n", 37 | "\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n", 38 | "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n", 39 | "\n", 40 | "\u001b[93m Creating a shortcut link for 'en' didn't work (maybe you don't have\n", 41 | " admin permissions?), but you can still load the model via its full\n", 42 | " package name: nlp = spacy.load('{name}')\u001b[0m\n", 43 | " Download successful but linking failed\n", 44 | "\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "!python -m spacy download en #For Python2 installation" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 54, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "I PRON\n", 62 | "am VERB\n", 63 | "learning VERB\n", 64 | "how ADV\n", 65 | "to PART\n", 66 | "build VERB\n", 67 | "chatbots NOUN\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "nlp = spacy.load('en') #Loads the spacy en model into a python object \n", 73 | "doc = nlp(u'I am learning how to build chatbots') #Creates a Doc object \n", 74 | "for token in doc: \n", 75 | " print(token.text, token.pos_) #prints the text and POS " 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 55, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "I PRON\n", 88 | "am VERB\n", 89 | "going VERB\n", 90 | "to ADP\n", 91 | "London PROPN\n", 92 | "next ADJ\n", 93 | "week NOUN\n", 94 | "for ADP\n", 95 | "a DET\n", 96 | "meeting NOUN\n", 97 | ". PUNCT\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "doc = nlp(u'I am going to London next week for a meeting.')\n", 103 | "for token in doc:\n", 104 | " print(token.text, token.pos_)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 56, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "Google google PROPN NNP compound Xxxxx True False\n", 117 | "release release NOUN NN nmod xxxx True False\n", 118 | "\" \" PUNCT `` punct \" False False\n", 119 | "Move move PROPN NNP nmod Xxxx True False\n", 120 | "Mirror mirror PROPN NNP nmod Xxxxx True False\n", 121 | "\" \" PUNCT '' punct \" False False\n", 122 | "AI ai PROPN NNP compound XX True False\n", 123 | "experiment experiment NOUN NN ROOT xxxx True False\n", 124 | "that that ADJ WDT nsubj xxxx True True\n", 125 | "matches match VERB VBZ relcl xxxx True False\n", 126 | "your -PRON- ADJ PRP$ poss xxxx True True\n", 127 | "pose pose NOUN NN dobj xxxx True False\n", 128 | "from from ADP IN prep xxxx True True\n", 129 | "80,000 80,000 NUM CD nummod dd,ddd False False\n", 130 | "images image NOUN NNS pobj xxxx True False\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "doc = nlp(u'Google release \"Move Mirror\" AI experiment that matches your pose from 80,000 images')\n", 136 | "\n", 137 | "for token in doc:\n", 138 | " print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,\n", 139 | " token.shape_, token.is_alpha, token.is_stop)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 57, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "from IPython.display import HTML, display\n", 156 | "data = [(u'Google', u'google', u'PROPN', u'NNP', u'compound', u'Xxxxx', True, False),\n", 157 | "(u'release', u'release', u'NOUN', u'NN', u'nmod', u'xxxx', True, False),\n", 158 | "(u'\"', u'\"', u'PUNCT', u'``', u'punct', u'\"', False, False),\n", 159 | "(u'Move', u'move', u'PROPN', u'NNP', u'nmod', u'Xxxx', True, False),\n", 160 | "(u'Mirror', u'mirror', u'PROPN', u'NNP', u'nmod', u'Xxxxx', True, False),\n", 161 | "(u'\"', u'\"', u'PUNCT', u\"''\", u'punct', u'\"', False, False),\n", 162 | "(u'AI', u'ai', u'PROPN', u'NNP', u'compound', u'XX', True, False),\n", 163 | "(u'experiment', u'experiment', u'NOUN', u'NN', u'ROOT', u'xxxx', True, False),\n", 164 | "(u'that', u'that', u'ADJ', u'WDT', u'nsubj', u'xxxx', True, True),\n", 165 | "(u'matches', u'match', u'VERB', u'VBZ', u'relcl', u'xxxx', True, False),\n", 166 | "(u'your', u'-PRON-', u'ADJ', u'PRP$', u'poss', u'xxxx', True, True),\n", 167 | "(u'pose', u'pose', u'NOUN', u'NN', u'dobj', u'xxxx', True, False),\n", 168 | "(u'from', u'from', u'ADP', u'IN', u'prep', u'xxxx', True, True),\n", 169 | "(u'80,000', u'80,000', u'NUM', u'CD', u'nummod', u'dd,ddd', False, False),\n", 170 | "(u'images', u'image', u'NOUN', u'NNS', u'pobj', u'xxxx', True, False)]\n", 171 | "\n", 172 | "def display_as_table(headers, data):\n", 173 | " html = '' + \"\"*len(headers) + ''\n", 174 | " html = html.format(*headers)\n", 175 | "\n", 176 | " html += \"\"\"{}
{}
\"\"\".format(\n", 177 | " ''.join(\n", 178 | " '{}'.format(''.join(str(_) for _ in row)) for row in data)\n", 179 | " )\n", 180 | " \n", 181 | " display(HTML(html))" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 58, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/html": [ 192 | "
TEXTLEMMAPOSTAGDEPSHAPEALPHASHAPE
GooglegooglePROPNNNPcompoundXxxxxTrueFalse
releasereleaseNOUNNNnmodxxxxTrueFalse
\"\"PUNCT``punct\"FalseFalse
MovemovePROPNNNPnmodXxxxTrueFalse
MirrormirrorPROPNNNPnmodXxxxxTrueFalse
\"\"PUNCT''punct\"FalseFalse
AIaiPROPNNNPcompoundXXTrueFalse
experimentexperimentNOUNNNROOTxxxxTrueFalse
thatthatADJWDTnsubjxxxxTrueTrue
matchesmatchVERBVBZrelclxxxxTrueFalse
your-PRON-ADJPRP$possxxxxTrueTrue
poseposeNOUNNNdobjxxxxTrueFalse
fromfromADPINprepxxxxTrueTrue
80,00080,000NUMCDnummoddd,dddFalseFalse
imagesimageNOUNNNSpobjxxxxTrueFalse
" 193 | ], 194 | "text/plain": [ 195 | "" 196 | ] 197 | }, 198 | "metadata": {}, 199 | "output_type": "display_data" 200 | } 201 | ], 202 | "source": [ 203 | "display_as_table(['TEXT', 'LEMMA', 'POS', 'TAG', 'DEP', 'SHAPE', 'ALPHA', 'SHAPE'], data)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 59, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "['chuckle']" 215 | ] 216 | }, 217 | "execution_count": 59, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "from spacy.lemmatizer import Lemmatizer \n", 224 | "from spacy.lang.en import LEMMA_INDEX, LEMMA_EXC, LEMMA_RULES \n", 225 | "lemmatizer = Lemmatizer(LEMMA_INDEX, LEMMA_EXC, LEMMA_RULES) \n", 226 | "lemmatizer('chuckles', 'NOUN') # 2nd param is token's part-of-speech tag \n" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 60, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "['blaze']" 238 | ] 239 | }, 240 | "execution_count": 60, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "lemmatizer('blazing', 'VERB')" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 61, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "['fast']" 258 | ] 259 | }, 260 | "execution_count": 61, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "lemmatizer('fastest', 'ADJ')" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 62, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [ 282 | "from nltk.stem.porter import *\n", 283 | "from nltk.stem.snowball import SnowballStemmer" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 63, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "porter_stemmer = PorterStemmer()\n", 293 | "snowball_stemmer = SnowballStemmer(\"english\")" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 64, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "fastest\n", 306 | "fastest\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "print(porter_stemmer.stem(\"fastest\"))\n", 312 | "print(snowball_stemmer.stem(\"fastest\"))" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 66, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "Google ORG\n", 325 | "Mountain View GPE\n", 326 | "California GPE\n", 327 | "109.65 billion US dollars MONEY\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "my_string = u\"Google has its headquarters in Mountain View, California having revenue amounted to 109.65 billion US dollars\"\n", 333 | "doc = nlp(my_string)\n", 334 | "\n", 335 | "for ent in doc.ents: \n", 336 | " print(ent.text, ent.label_) \n", 337 | " " 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 103, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "name": "stdout", 347 | "output_type": "stream", 348 | "text": [ 349 | "(u'I', u'-PRON-', u'PRON', u'PRP', u'nsubj', u'X', True, False)\n", 350 | "(u'am', u'be', u'VERB', u'VBP', u'aux', u'xx', True, True)\n", 351 | "(u'learning', u'learn', u'VERB', u'VBG', u'ROOT', u'xxxx', True, False)\n", 352 | "(u'how', u'how', u'ADV', u'WRB', u'advmod', u'xxx', True, True)\n", 353 | "(u'to', u'to', u'PART', u'TO', u'aux', u'xx', True, True)\n", 354 | "(u'build', u'build', u'VERB', u'VB', u'xcomp', u'xxxx', True, False)\n", 355 | "(u'chatbots', u'chatbot', u'NOUN', u'NNS', u'dobj', u'xxxx', True, False)\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "doc = nlp(u'I am learning how to build chatbots') \n", 361 | "for token in doc: \n", 362 | " print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, \n", 363 | " token.shape_, token.is_alpha, token.is_stop) " 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 104, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/html": [ 374 | "
TEXTLEMMAPOSTAGDEPSHAPEALPHASHAPE
I-PRON-PRONPRPnsubjXTrueFalse
ambeVERBVBPauxxxTrueTrue
learninglearnVERBVBGROOTxxxxTrueFalse
howhowADVWRBadvmodxxxTrueTrue
totoPARTTOauxxxTrueTrue
buildbuildVERBVBxcompxxxxTrueFalse
chatbotschatbotNOUNNNSdobjxxxxTrueFalse
" 375 | ], 376 | "text/plain": [ 377 | "" 378 | ] 379 | }, 380 | "metadata": {}, 381 | "output_type": "display_data" 382 | } 383 | ], 384 | "source": [ 385 | "data = [(u'I', u'-PRON-', u'PRON', u'PRP', u'nsubj', u'X', True, False),\n", 386 | "(u'am', u'be', u'VERB', u'VBP', u'aux', u'xx', True, True),\n", 387 | "(u'learning', u'learn', u'VERB', u'VBG', u'ROOT', u'xxxx', True, False),\n", 388 | "(u'how', u'how', u'ADV', u'WRB', u'advmod', u'xxx', True, True),\n", 389 | "(u'to', u'to', u'PART', u'TO', u'aux', u'xx', True, True),\n", 390 | "(u'build', u'build', u'VERB', u'VB', u'xcomp', u'xxxx', True, False),\n", 391 | "(u'chatbots', u'chatbot', u'NOUN', u'NNS', u'dobj', u'xxxx', True, False)]\n", 392 | "\n", 393 | "display_as_table(['TEXT', 'LEMMA', 'POS', 'TAG', 'DEP', 'SHAPE', 'ALPHA', 'SHAPE'], data)" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": null, 399 | "metadata": { 400 | "scrolled": true 401 | }, 402 | "outputs": [], 403 | "source": [ 404 | "my_string = u\"Mark Zuckerberg born May 14, 1984 in New York is an American technology entrepreneur and philanthropist best known for co-founding and leading Facebook as its chairman and CEO.\"\n", 405 | "doc = nlp(my_string) \n", 406 | "\n", 407 | "for ent in doc.ents: \n", 408 | " print(ent.text, ent.label_) " 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "metadata": {}, 415 | "outputs": [], 416 | "source": [ 417 | "my_string = u\"I usually wake up at 9:00 AM. 90% of my daytime goes in learning new things.\"\n", 418 | "doc = nlp(my_string) \n", 419 | "\n", 420 | "for ent in doc.ents: \n", 421 | " print(ent.text, ent.label_)" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "my_string1 = u\"Imagine Dragons are the best band.\"\n", 431 | "my_string2 = u\"Imagine dragons come and take over the city.\"\n", 432 | "\n", 433 | "doc1 = nlp(my_string1) \n", 434 | "doc2 = nlp(my_string2) \n", 435 | "\n", 436 | "for ent in doc1.ents: \n", 437 | " print(ent.text, ent.label_)\n", 438 | " \n", 439 | "for ent in doc2.ents: \n", 440 | " print(ent.text, ent.label_)" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "metadata": {}, 447 | "outputs": [], 448 | "source": [ 449 | "from spacy.lang.en.stop_words import STOP_WORDS \n", 450 | "\n", 451 | "print(STOP_WORDS) " 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": null, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [ 460 | "nlp.vocab[u'is'].is_stop " 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [ 469 | "nlp.vocab[u'hello'].is_stop " 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": null, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [ 478 | "nlp.vocab[u'with'].is_stop" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 36, 484 | "metadata": {}, 485 | "outputs": [ 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "[from, flight, Book]" 490 | ] 491 | }, 492 | "execution_count": 36, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "doc = nlp(u'Book me a flight from Bangalore to Goa') \n", 499 | "blr, goa = doc[5], doc[7] \n", 500 | "list(blr.ancestors) " 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 47, 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "data": { 510 | "text/plain": [ 511 | "[flight, Book]" 512 | ] 513 | }, 514 | "execution_count": 47, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "list(doc[4].ancestors)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 37, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "data": { 530 | "text/plain": [ 531 | "True" 532 | ] 533 | }, 534 | "execution_count": 37, 535 | "metadata": {}, 536 | "output_type": "execute_result" 537 | } 538 | ], 539 | "source": [ 540 | "doc[3].is_ancestor(doc[5])" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 44, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "data": { 550 | "text/plain": [ 551 | "[a, from, to]" 552 | ] 553 | }, 554 | "execution_count": 44, 555 | "metadata": {}, 556 | "output_type": "execute_result" 557 | } 558 | ], 559 | "source": [ 560 | "list(doc[3].children) " 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 29, 566 | "metadata": {}, 567 | "outputs": [], 568 | "source": [ 569 | "doc = nlp(u'Book a table at the restaurant and the taxi to the hotel')\n", 570 | "tasks = doc[2], doc[8] #(table, taxi)\n", 571 | "tasks_target = doc[5], doc[11] #(restaurant, hotel)" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 34, 577 | "metadata": { 578 | "scrolled": true 579 | }, 580 | "outputs": [ 581 | { 582 | "name": "stdout", 583 | "output_type": "stream", 584 | "text": [ 585 | "Booking of table belongs to restaurant\n", 586 | "Booking of taxi belongs to hotel\n" 587 | ] 588 | } 589 | ], 590 | "source": [ 591 | "for task in tasks_target:\n", 592 | " for tok in task.ancestors:\n", 593 | " if tok in tasks:\n", 594 | " print(\"Booking of {} belongs to {}\".format(tok, task))\n", 595 | " break" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 48, 601 | "metadata": {}, 602 | "outputs": [ 603 | { 604 | "name": "stdout", 605 | "output_type": "stream", 606 | "text": [ 607 | "\n", 608 | "\u001b[93m Serving on port 5000...\u001b[0m\n", 609 | " Using the 'dep' visualizer\n", 610 | "\n" 611 | ] 612 | }, 613 | { 614 | "name": "stderr", 615 | "output_type": "stream", 616 | "text": [ 617 | "127.0.0.1 - - [23/Jul/2018 04:00:04] \"GET / HTTP/1.1\" 200 8843\n", 618 | "127.0.0.1 - - [23/Jul/2018 04:00:04] \"GET /favicon.ico HTTP/1.1\" 200 8843\n" 619 | ] 620 | }, 621 | { 622 | "name": "stdout", 623 | "output_type": "stream", 624 | "text": [ 625 | "\n", 626 | " Shutting down server on port 5000.\n", 627 | "\n" 628 | ] 629 | } 630 | ], 631 | "source": [ 632 | "from spacy import displacy \n", 633 | "doc = nlp(u'Book a table at the restaurant and the taxi to the hotel') \n", 634 | "displacy.serve(doc, style='dep') " 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": 50, 640 | "metadata": {}, 641 | "outputs": [ 642 | { 643 | "name": "stdout", 644 | "output_type": "stream", 645 | "text": [ 646 | "User is referring Berlin to visit\n", 647 | "User is referring Lubeck to stay\n" 648 | ] 649 | } 650 | ], 651 | "source": [ 652 | "doc = nlp(u\"What are some places to visit in Berlin and stay in Lubeck\") \n", 653 | "places = [doc[7], doc[11]] #[Berlin, Lubeck] \n", 654 | "\n", 655 | "actions = [doc[5], doc[9]] #[visit, stay] \n", 656 | "\n", 657 | "for place in places: \n", 658 | " for tok in place.ancestors: \n", 659 | " if tok in actions: \n", 660 | " print(\"User is referring {} to {}\").format(place, tok) \n", 661 | " break " 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 106, 667 | "metadata": {}, 668 | "outputs": [ 669 | { 670 | "data": { 671 | "text/plain": [ 672 | "[Boston Dynamics, thousands, robot dogs]" 673 | ] 674 | }, 675 | "execution_count": 106, 676 | "metadata": {}, 677 | "output_type": "execute_result" 678 | } 679 | ], 680 | "source": [ 681 | "doc = nlp(u\"Boston Dynamics is gearing up to produce thousands of robot dogs\") \n", 682 | "\n", 683 | "list(doc.noun_chunks) " 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 112, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "name": "stdout", 693 | "output_type": "stream", 694 | "text": [ 695 | "(u'Deep learning', u'learning', u'nsubj', u'cracks')\n", 696 | "(u'the code', u'code', u'dobj', u'cracks')\n", 697 | "(u'messenger RNAs', u'RNAs', u'pobj', u'of')\n", 698 | "(u'protein-coding potential', u'potential', u'conj', u'RNAs')\n" 699 | ] 700 | } 701 | ], 702 | "source": [ 703 | "doc = nlp(u\"Deep learning cracks the code of messenger RNAs and protein-coding potential\") \n", 704 | "for chunk in doc.noun_chunks:\n", 705 | " print(chunk.text, chunk.root.text, chunk.root.dep_,\n", 706 | " chunk.root.head.text)" 707 | ] 708 | }, 709 | { 710 | "cell_type": "code", 711 | "execution_count": 113, 712 | "metadata": {}, 713 | "outputs": [ 714 | { 715 | "data": { 716 | "text/html": [ 717 | "
TEXTROOT.TEXTROOT.DEP_ROOT.HEAD.TEXT
Deep learninglearningnsubjcracks
the codecodedobjcracks
messenger RNAsRNAspobjof
protein-coding potentialpotentialconjRNAs
" 718 | ], 719 | "text/plain": [ 720 | "" 721 | ] 722 | }, 723 | "metadata": {}, 724 | "output_type": "display_data" 725 | } 726 | ], 727 | "source": [ 728 | "data = [(u'Deep learning', u'learning', u'nsubj', u'cracks'),\n", 729 | "(u'the code', u'code', u'dobj', u'cracks'),\n", 730 | "(u'messenger RNAs', u'RNAs', u'pobj', u'of'),\n", 731 | "(u'protein-coding potential', u'potential', u'conj', u'RNAs')]\n", 732 | "\n", 733 | "display_as_table(['TEXT', 'ROOT.TEXT', 'ROOT.DEP_', 'ROOT.HEAD.TEXT'], data)" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": 114, 739 | "metadata": {}, 740 | "outputs": [ 741 | { 742 | "name": "stdout", 743 | "output_type": "stream", 744 | "text": [ 745 | "(u'How', array([-0.29742685, 0.73939574, -0.04001453, 0.44034013, 2.8967502 ],\n", 746 | " dtype=float32))\n", 747 | "(u'are', array([-0.23435134, -1.6145049 , 1.0197453 , 0.9928169 , 0.28227055],\n", 748 | " dtype=float32))\n", 749 | "(u'you', array([ 0.10252178, -3.564711 , 2.4822793 , 4.2824993 , 3.590245 ],\n", 750 | " dtype=float32))\n", 751 | "(u'doing', array([-0.6240922 , -2.0210216 , -0.91014993, 2.7051923 , 4.189252 ],\n", 752 | " dtype=float32))\n", 753 | "(u'today', array([ 3.5409122 , -0.62185854, 2.6274266 , 2.0504875 , 0.20191991],\n", 754 | " dtype=float32))\n", 755 | "(u'?', array([ 2.8914998 , -0.25079122, 3.3764176 , 1.6942682 , 1.9849057 ],\n", 756 | " dtype=float32))\n" 757 | ] 758 | } 759 | ], 760 | "source": [ 761 | "doc = nlp(u'How are you doing today?') \n", 762 | "for token in doc: \n", 763 | " print(token.text, token.vector[:5]) " 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 116, 769 | "metadata": {}, 770 | "outputs": [], 771 | "source": [ 772 | "hello_doc = nlp(u\"hello\") \n", 773 | "hi_doc = nlp(u\"hi\") \n", 774 | "hella_doc = nlp(u\"hella\") " 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 119, 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "name": "stdout", 784 | "output_type": "stream", 785 | "text": [ 786 | "0.7879069442766685\n" 787 | ] 788 | } 789 | ], 790 | "source": [ 791 | "print hello_doc.similarity(hi_doc) " 792 | ] 793 | }, 794 | { 795 | "cell_type": "code", 796 | "execution_count": 120, 797 | "metadata": {}, 798 | "outputs": [ 799 | { 800 | "name": "stdout", 801 | "output_type": "stream", 802 | "text": [ 803 | "0.4193425861242359\n" 804 | ] 805 | } 806 | ], 807 | "source": [ 808 | "print hello_doc.similarity(hella_doc) " 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 125, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "data": { 818 | "text/plain": [ 819 | "0.785019122782813" 820 | ] 821 | }, 822 | "execution_count": 125, 823 | "metadata": {}, 824 | "output_type": "execute_result" 825 | } 826 | ], 827 | "source": [ 828 | "GoT_str1 = nlp(u\"When will next season of Game of Thrones be releasing?\")\n", 829 | "GoT_str2 = nlp(u\"Game of Thrones next season release date?\")\n", 830 | "GoT_str1.similarity(GoT_str2) " 831 | ] 832 | }, 833 | { 834 | "cell_type": "code", 835 | "execution_count": 135, 836 | "metadata": {}, 837 | "outputs": [ 838 | { 839 | "name": "stdout", 840 | "output_type": "stream", 841 | "text": [ 842 | "Word car is 100% similar to word car\n", 843 | "Word car is 71% similar to word truck\n", 844 | "Word car is 24% similar to word google\n", 845 | "Word truck is 71% similar to word car\n", 846 | "Word truck is 100% similar to word truck\n", 847 | "Word truck is 36% similar to word google\n", 848 | "Word google is 24% similar to word car\n", 849 | "Word google is 36% similar to word truck\n", 850 | "Word google is 100% similar to word google\n" 851 | ] 852 | } 853 | ], 854 | "source": [ 855 | "example_doc = nlp(u\"car truck google\")\n", 856 | "\n", 857 | "for t1 in example_doc:\n", 858 | " for t2 in example_doc:\n", 859 | " similarity_perc = int(t1.similarity(t2) * 100)\n", 860 | " print \"Word {} is {}% similar to word {}\".format(t1.text, similarity_perc, t2.text)\n" 861 | ] 862 | }, 863 | { 864 | "cell_type": "code", 865 | "execution_count": 136, 866 | "metadata": {}, 867 | "outputs": [ 868 | { 869 | "name": "stdout", 870 | "output_type": "stream", 871 | "text": [ 872 | "Brexit\n", 873 | "is\n", 874 | "the\n", 875 | "impending\n", 876 | "withdrawal\n", 877 | "of\n", 878 | "the\n", 879 | "U.K.\n", 880 | "from\n", 881 | "the\n", 882 | "European\n", 883 | "Union\n", 884 | ".\n" 885 | ] 886 | } 887 | ], 888 | "source": [ 889 | "doc = nlp(u'Brexit is the impending withdrawal of the U.K. from the European Union.')\n", 890 | "for token in doc:\n", 891 | " print(token.text)" 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": 67, 897 | "metadata": {}, 898 | "outputs": [], 899 | "source": [ 900 | "sentence1 = \"Book me a metro from Airport Station to Hong Kong Station.\"\n", 901 | "sentence2 = \"Book me a cab to Hong Kong Airport from AsiaWorld-Expo.\"\n", 902 | "\n", 903 | "import re\n", 904 | "from_to = re.compile('.* from (.*) to (.*)')\n", 905 | "to_from = re.compile('.* to (.*) from (.*)')" 906 | ] 907 | }, 908 | { 909 | "cell_type": "code", 910 | "execution_count": 69, 911 | "metadata": {}, 912 | "outputs": [], 913 | "source": [ 914 | "from_to_match = from_to.match(sentence2)\n", 915 | "to_from_match = to_from.match(sentence2)" 916 | ] 917 | }, 918 | { 919 | "cell_type": "code", 920 | "execution_count": 70, 921 | "metadata": {}, 922 | "outputs": [ 923 | { 924 | "name": "stdout", 925 | "output_type": "stream", 926 | "text": [ 927 | "to_from pattern matched correctly. Printing values\n", 928 | "\n", 929 | "From: AsiaWorld-Expo., To: Hong Kong Airport\n" 930 | ] 931 | } 932 | ], 933 | "source": [ 934 | "if from_to_match and from_to_match.groups():\n", 935 | " _from = from_to_match.groups()[0]\n", 936 | " _to = from_to_match.groups()[1]\n", 937 | " print(\"from_to pattern matched correctly. Printing values\\n\")\n", 938 | " print(\"From: {}, To: {}\".format(_from, _to))\n", 939 | " \n", 940 | "elif to_from_match and to_from_match.groups():\n", 941 | " _to = to_from_match.groups()[0]\n", 942 | " _from = to_from_match.groups()[1]\n", 943 | " print(\"to_from pattern matched correctly. Printing values\\n\")\n", 944 | " print(\"From: {}, To: {}\".format(_from, _to))" 945 | ] 946 | }, 947 | { 948 | "cell_type": "code", 949 | "execution_count": null, 950 | "metadata": {}, 951 | "outputs": [], 952 | "source": [] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": null, 957 | "metadata": {}, 958 | "outputs": [], 959 | "source": [] 960 | } 961 | ], 962 | "metadata": { 963 | "kernelspec": { 964 | "display_name": "Python 3", 965 | "language": "python", 966 | "name": "python3" 967 | }, 968 | "language_info": { 969 | "codemirror_mode": { 970 | "name": "ipython", 971 | "version": 3 972 | }, 973 | "file_extension": ".py", 974 | "mimetype": "text/x-python", 975 | "name": "python", 976 | "nbconvert_exporter": "python", 977 | "pygments_lexer": "ipython3", 978 | "version": "3.6.5" 979 | } 980 | }, 981 | "nbformat": 4, 982 | "nbformat_minor": 2 983 | } 984 | -------------------------------------------------------------------------------- /Chapter III/OnlineEatsBot Agent Export from DialogFlow.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-chatbots-with-python/e84594620087ddde5b12c723b839d1efd605c11a/Chapter III/OnlineEatsBot Agent Export from DialogFlow.zip -------------------------------------------------------------------------------- /Chapter III/flask_onlineeats_demo.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-chatbots-with-python/e84594620087ddde5b12c723b839d1efd605c11a/Chapter III/flask_onlineeats_demo.zip -------------------------------------------------------------------------------- /Chapter IV/Chapter IV Code horoscope_bot.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-chatbots-with-python/e84594620087ddde5b12c723b839d1efd605c11a/Chapter IV/Chapter IV Code horoscope_bot.zip -------------------------------------------------------------------------------- /Chapter V/horoscope_bot Source Code (Chapter V).zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-chatbots-with-python/e84594620087ddde5b12c723b839d1efd605c11a/Chapter V/horoscope_bot Source Code (Chapter V).zip -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2019 Sumit Raj 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Building Chatbots with Python*](https://www.apress.com/9781484240953) by Sumit Raj (Apress, 2019). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484240953.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. --------------------------------------------------------------------------------