Last active
August 10, 2018 15:19
-
-
Save jeroavf/dcb745618cbe1591a01a80be51d6f15a to your computer and use it in GitHub Desktop.
Chatbot example functions in python + sqlite3 for chatbot test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
# Define find_hotels() | |
def find_hotels(params): | |
# Create the base query | |
query = 'SELECT * FROM hotels' | |
# Add filter clauses for each of the parameters | |
if len(params) > 0: | |
filters = ["{}=?".format(k) for k in params] | |
query += " WHERE " + " and ".join(filters) | |
# Create the tuple of values | |
t = tuple(params.values()) | |
# Open connection to DB | |
conn = sqlite3.connect("hotels.db") | |
# Create a cursor | |
c = conn.cursor() | |
# Execute the query | |
c.execute(query,t) | |
# Return the results | |
print(c.fetchall()) | |
# Create the dictionary of column names and values | |
params = {"area": "south" , "price": "lo"} | |
# Find the hotels that match the parameters | |
print(find_hotels(params)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define negated_ents() | |
def negated_ents(phrase): | |
# Extract the entities using keyword matching | |
ents = [e for e in ["south", "north"] if e in phrase] | |
# Find the index of the final character of each entity | |
ends = sorted([phrase.index(e) + len(e) for e in ents]) | |
# Initialise a list to store sentence chunks | |
chunks = [] | |
# Take slices of the sentence up to and including each entitiy | |
start = 0 | |
for end in ends: | |
chunks.append(phrase[start:end]) | |
start = end | |
result = {} | |
# Iterate over the chunks and look for entities | |
for chunk in chunks: | |
for ent in ents: | |
if ent in chunk: | |
# If the entity is preceeded by a negation, give it the key False | |
if "not" in chunk or "n't" in chunk: | |
result[ent] = False | |
else: | |
result[ent] = True | |
return result | |
# Check that the entities are correctly assigned as True or False | |
for test in tests: | |
print(negated_ents(test[0]) == test[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define respond() | |
def respond(message): | |
# Extract the entities | |
entities = interpreter.parse(message)["entities"] | |
# Initialize an empty params dictionary | |
params = {} | |
# Fill the dictionary with entities | |
for ent in entities: | |
params[ent["entity"]] = str(ent["value"]) | |
# Find hotels that match the dictionary | |
results = find_hotels(params) | |
# Get the names of the hotels and index of the response | |
names = [r[0] for r in results] | |
n = min(len(results),3) | |
# Select the nth element of the responses array | |
return responses[n].format(*names) | |
print(respond("I want an expensive hotel in the south of town")) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define a respond function, taking the message and existing params as input | |
def respond(message, params): | |
# Extract the entities | |
entities = interpreter.parse(message)["entities"] | |
# Fill the dictionary with entities | |
for ent in entities: | |
params[ent["entity"]] = str(ent["value"]) | |
# Find the hotels | |
results = find_hotels(params) | |
names = [r[0] for r in results] | |
n = min(len(results), 3) | |
# Return the appropriate response | |
return responses[n].format(*names), params | |
# Initialize params dictionary | |
params = {} | |
# Pass the messages to the bot | |
for message in ["I want an expensive hotel", "in the north of town"]: | |
print("USER: {}".format(message)) | |
response, params = respond(message, params) | |
print("BOT: {}".format(response)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment