Skip to content

Instantly share code, notes, and snippets.

@anthonygarvan
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save anthonygarvan/18f6cc522afd93436762 to your computer and use it in GitHub Desktop.

Select an option

Save anthonygarvan/18f6cc522afd93436762 to your computer and use it in GitHub Desktop.
This is the bot logic behind the game Bot or Not, a crowdsourced Turing test. Learn more at www.anthonygarvan.com.
class Repository:
def __init__(self):
mongo_uri = 'mongodb://localhost:27017/master'
db_name = 'master'
self.client = MongoClient(mongo_uri)
self.db = self.client[db_name]
self.messages = self.db.messages
self.partner_queue = self.db.partner_queue
self.default_messages = ['Sorry I don\'t get that.',
'DOES NOT COMPUTE',
'wtf?',
'wtf',
'my neural network cannot process that.',
'ur silly.',
'ur awesome.',
'you\'re a tough nut to crack.',
'that\'s nonsense to me',
'let\'s change topics.',
'i think im in luv.',
'what are you smoking?',
'sorry, I\'m from France.',
'whatevs']
def get_bot_response(self, msg):
r = random()
similar_msgs = self.messages.find({"$text":{"$search":msg},
"is_before_guess":False,
"is_keypress":False,
"end_guess":"not"},
{"score": {"$meta":"textScore"}}).sort([("score",{"$meta":"textScore"}),
("game_mode",pymongo.DESCENDING)]).limit(20)
msg_count = similar_msgs.count()
if msg_count > 0:
msg = similar_msgs[0]
top_score = msg["score"]
top_game_mode = msg["game_mode"]
score = top_score
game_mode = top_game_mode
index_threshold = 0
while((top_score-score<.15) and top_game_mode == game_mode and index_threshold < msg_count):
score = similar_msgs[index_threshold]["score"]
game_mode = similar_msgs[index_threshold]["game_mode"]
index_threshold += 1
similar_msg = similar_msgs[randint(0,index_threshold-1)]
else:
similar_msg = None
# get response from similar message, including typing patterns
response = None
if similar_msg is not None:
response = list(self.messages.find({"conversation_id":similar_msg["conversation_id"],
"message_number":{"$gt":similar_msg["message_number"],
"$lte":similar_msg["message_number"]+1}, "is_guess":False},
sort=[("message_number", pymongo.ASCENDING)]))
if response is not None and len(response) > 0:
return response
else:
delay_start_typing = randint(1500,5000)
delay_ms = randint(3500,8000)
default_msg = self.default_messages[randint(0,len(self.default_messages)-1)]
return [{"content":True, "delay_ms":delay_ms, "is_keypress":True},
{"content":default_msg, "delay_ms":delay_ms, "is_keypress":False}]
def get_first_message_from_bot(self):
r = random()
# choose a random first message from a human
msg = self.messages.find_one({"message_number":1, "uuid":{"$ne":"bot"}, "is_guess":False, "random": {"$gte":r, "lte":r+.001}})
if msg is None:
msg = self.messages.find_one({"message_number":1, "uuid":{"$ne":"bot"}, "is_guess":False})
# include typing patterns (typing entries have non-integral message numbers.
# eg., message number .1 would be to start typing, .2 to stop typing, and 1 would be the message
msgs = list(self.messages.find({"conversation_id":msg["conversation_id"],
"message_number": {"$lte":1}}, sort=[("message_number", pymongo.ASCENDING)]))
return msgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment