Building Chatbots in Python
Alan Nichol
Co-founder and CTO, Rasa
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'}
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=?'
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