Exploring a DB with natural language

Building Chatbots in Python

Alan Nichol

Co-founder and CTO, Rasa

Example messages

  • "Show me a great hotel"
  • "I'm looking for a cheap hotel in the south of town"
  • "Anywhere so long as it's central"
Building Chatbots in Python

Parameters from text

message = "a cheap hotel in the north"

data = interpreter.parse(message) data
{'entities': [{'end': '7', 'entity': 'price', 'start': 2, 'value': 'lo'},
  {'end': 26, 'entity': 'location', 'start': 21, 'value': 'north'}],
 'intent': {'confidence': 0.9, 'name': 'hotel_search'}}
params = {}

for ent in data["entities"]: params[ent["entity"]] = ent["value"] params
{'location': 'north', 'price': 'lo'}
Building Chatbots in Python
query = "select name FROM hotels"

filters = ["{}=?".format(k) for k in params.keys()] filters
['price=?', 'location=?']
conditions = " and ".join(filters)
conditions
 'price=? and location=?'
final_q = " WHERE ".join([query, conditions])

final_q
'SELECT name FROM hotels WHERE price=? and location=?'
Building Chatbots in Python

Responses

responses = [
        "I'm sorry :( I couldn't find anything like that", 
        "what about {}?", 
        "{} is one option, but I know others too :)"
        ]

results = c.fetchall() len(results)
4
index = min(len(results), len(responses)-1)
responses[index]
'{} is one option, but I know others too :)'
Building Chatbots in Python

Let's practice!

Building Chatbots in Python

Preparing Video For Download...