Introduction to conversational software Alan Nichol Co-founder and - - PowerPoint PPT Presentation

introduction to conversational software
SMART_READER_LITE
LIVE PREVIEW

Introduction to conversational software Alan Nichol Co-founder and - - PowerPoint PPT Presentation

DataCamp Building Chatbots in Python BUILDING CHATBOTS IN PYTHON Introduction to conversational software Alan Nichol Co-founder and CTO, Rasa DataCamp Building Chatbots in Python A short history Conversational software is not a new idea!


slide-1
SLIDE 1

DataCamp Building Chatbots in Python

Introduction to conversational software

BUILDING CHATBOTS IN PYTHON

Alan Nichol

Co-founder and CTO, Rasa

slide-2
SLIDE 2

DataCamp Building Chatbots in Python

A short history

Conversational software is not a new idea! Dates back to at least 1960s First wave: command line applications ELIZA: 1966

slide-3
SLIDE 3

DataCamp Building Chatbots in Python

Course content

Implementing smalltalk, ELIZA style How to use regex and ML to extract meaning from free-form text Build chatbots that can Query a database Plan a trip Help you order coffee Handling statefulness

slide-4
SLIDE 4

DataCamp Building Chatbots in Python

EchoBot I

In [1]: USER: Hello! Out[1]: BOT: I can hear you, you said: 'Hello!' In [2]: USER: How are you? Out[2]: BOT: I can hear you, you said: 'How are you?'

slide-5
SLIDE 5

DataCamp Building Chatbots in Python

EchoBot II

In [1]: def respond(message): : return "I can hear you! you said: {}".format(message) In [2]: def send_message(message): : # calls respond() to get response In [3]: send_message("hello!") Out[3]: USER: hello! ...: BOT : I can hear you! you said: hello!

slide-6
SLIDE 6

DataCamp Building Chatbots in Python

EchoBot III

In [1]: import time In [2]: time.sleep(0.5)

slide-7
SLIDE 7

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON

slide-8
SLIDE 8

DataCamp Building Chatbots in Python

Creating a personality

BUILDING CHATBOTS IN PYTHON

Alan Nichol

Co-founder and CTO, Rasa

slide-9
SLIDE 9

DataCamp Building Chatbots in Python

Why personality?

Difference between a command line app and a chatbot Makes chatbots and voice assistants more accessible and fun to use Your users will expect it!

slide-10
SLIDE 10

DataCamp Building Chatbots in Python

Smalltalk

In [1]: responses = { : "what's your name?": "my name is EchoBot", : "what's the weather today?": "it's sunny!" : } In [2]: def respond(message): : if message in responses: : return responses[message] In [3]: respond("what's your name?") Out[3]: 'my name is EchoBot'

slide-11
SLIDE 11

DataCamp Building Chatbots in Python

Including variables

In [1]: responses = { : "what's today's weather?": "it's {} today" : } In [2]: weather_today = "cloudy" In [3]: def respond(message): : if message in responses: : return responses[message].format(weather_today) : In [4]: respond("what's today's weather?") Out[4]: "it's cloudy today"

slide-12
SLIDE 12

DataCamp Building Chatbots in Python

Choosing responses

In [1]: responses = { : "what's your name?": [ : "my name is EchoBot", : "they call me EchoBot", : "the name's Bot, Echo Bot" : ] : } In [2]: import random In [3]: def respond(message): : if message in responses: : return random.choice(responses[message]) In [4]: respond("what's your name?") Out[4]: "the name's Bot, Echo Bot"

slide-13
SLIDE 13

DataCamp Building Chatbots in Python

Asking questions

In [1]: responses = [ "tell me more!", "why do you think that?" ] In [2]: import random In [3]: def respond(message): : return random.choice(responses) In [4]: respond("I think you're really great") Out[4]: 'why do you think that?'

slide-14
SLIDE 14

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON

slide-15
SLIDE 15

DataCamp Building Chatbots in Python

Text processing with regular expressions

BUILDING CHATBOTS IN PYTHON

Alan Nichol

Co-founder and CTO, Rasa

slide-16
SLIDE 16

DataCamp Building Chatbots in Python

Regular expressions

Match messages against known patterns Extract key phrases Transform sentences grammatically

slide-17
SLIDE 17

DataCamp Building Chatbots in Python

The regex behind ELIZA

USER: "do you remember when you ate strawberries in the garden?" ELIZA: "How could I forget when I ate strawberries in the garden?"

slide-18
SLIDE 18

DataCamp Building Chatbots in Python

Pattern matching

In [1]: import re In [2]: pattern = "do you remember .*" In [3]: message = "do you remember when you ate strawberries in the garden" In [4]: match = re.search(pattern, message) In [5]: if match: : print("string matches!") Out[5]: string matches!

slide-19
SLIDE 19

DataCamp Building Chatbots in Python

Extracting key phrases

In [1]: import re In [2]: pattern = "if (.*)" In [3]: message = "what would happen if bots took over the world" In [4]: match = re.search(pattern, message) In [5]: match.group(0) Out[5]: 'what would happen if bots took over the world' In [6]: match.group(1) Out[6]: 'bots took over the world'

slide-20
SLIDE 20

DataCamp Building Chatbots in Python

Grammatical transformation

In [1]: import re In [2]: def swap_pronouns(phrase): ...: if 'I' in phrase: ...: return re.sub('I', 'you', phrase) ...: if 'my' in phrase: ...: return re.sub('my', 'your', phrase) ...: ...: else: ...: return phrase In [3]: swap_pronouns("I walk my dog") Out[3]: 'You walk your dog'

slide-21
SLIDE 21

DataCamp Building Chatbots in Python

Putting it all together

In [1]: pattern = 'do you remember (.*)' In [2]: message = 'do you remember when you ate strawberries in the garden' In [3]: phrase = pattern.search(pattern, message).group(1) In [4]: phrase Out[4]: 'when you ate strawberries in the garden' In [5]: response = choose_response(pattern) In [6]: response Out[6]: 'how could I forget {}' In [7]: phrase = swap_pronouns(phrase) In [8]: phrase Out[8]: 'when I ate strawberries in the garden' In [9]: response.format(phrase) Out[9]: 'how could I forget when I ate strawberries in the garden'

slide-22
SLIDE 22

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON