def test_add_default_response(self): r = Responder() r.add_responses(("a", 1), ("b", 2)).add_default_response(3) self.assertEqual(r.get_response("a")[0], 1) self.assertEqual(r.get_response("b")[0], 2) self.assertEqual(r.get_response("c")[0], 3) self.assertEqual(r.get_response("gfnhvmnbvjm")[0], 3)
def test_add_context(self): c1 = Context().add_entry_phrase("a", 0).add_response("b", 1) c2 = Context().add_entry_phrase("x", 2).add_response("y", 3) c1.add_context(c2) r = Responder().add_context(c1).add_response("q", 4) # Verify we're not in a context self.assertEqual(r.get_response("q")[0], 4) self.assertEqual(r.get_response("b")[0], NoResponse) self.assertEqual(r.get_response("y")[0], NoResponse) # Enter first context, verify we're in it self.assertEqual(r.get_response("a")[0], 0) self.assertEqual(r.get_response("b")[0], 1) # Verify we're not in the subcontext self.assertEqual(r.get_response("y")[0], NoResponse) # Verify we're still in the first context self.assertEqual(r.get_response("b")[0], 1) # Enter subcontext contained in first context, verify we're in it self.assertEqual(r.get_response("x")[0], 2) self.assertEqual(r.get_response("y")[0], 3) # Verify first context isn't active anymore self.assertEqual(r.get_response("b")[0], NoResponse) # ... but we should be able to re-enter it, since it's a top-level # context added to the responder self.assertEqual(r.get_response("a")[0], 0) self.assertEqual(r.get_response("b")[0], 1)
def test_add_contexts(self): c1 = Context().add_entry_phrase("a", 0).add_response("b", 1) c2 = Context().add_entry_phrase("c", 2).add_response("d", 3) c3 = Context().add_entry_phrase("e", 4).add_response("f", 5) c1.add_contexts(c2, c3) r = Responder().add_context(c1).add_response("q", 6) # Verify we're not in a context self.assertEqual(r.get_response("q")[0], 6) self.assertEqual(r.get_response("b")[0], NoResponse) self.assertEqual(r.get_response("d")[0], NoResponse) self.assertEqual(r.get_response("f")[0], NoResponse) # Verify we can't enter either of the subcontexts self.assertEqual(r.get_response("c")[0], NoResponse) self.assertEqual(r.get_response("e")[0], NoResponse) # Enter first context self.assertEqual(r.get_response("a")[0], 0) self.assertEqual(r.get_response("b")[0], 1) # Verify two subcontexts are not active self.assertEqual(r.get_response("d")[0], NoResponse) self.assertEqual(r.get_response("f")[0], NoResponse) # Enter first subcontext self.assertEqual(r.get_response("c")[0], 2) self.assertEqual(r.get_response("d")[0], 3) # Verify we can't enter 2nd subcontext from here self.assertEqual(r.get_response("e")[0], NoResponse) self.assertEqual(r.get_response("b")[0], NoResponse) # Go back to first context self.assertEqual(r.get_response("a")[0], 0) self.assertEqual(r.get_response("b")[0], 1) # Enter second subcontext self.assertEqual(r.get_response("e")[0], 4) self.assertEqual(r.get_response("f")[0], 5) # Verify we can't enter 1st subcontext from here self.assertEqual(r.get_response("c")[0], NoResponse) self.assertEqual(r.get_response("b")[0], NoResponse)
def test_add_entry_phrase(self): c = Context().add_entry_phrase("e", 1).add_response("f", 2) r = Responder().add_response("a", 3).add_context(c) # Verify not in context self.assertEqual(r.get_response("a")[0], 3) self.assertEqual(r.get_response("f")[0], NoResponse) self.assertFalse(r.context) # Enter context self.assertEqual(r.get_response("e")[0], 1) self.assertEqual(r.get_response("f")[0], 2) self.assertIs(r.context, c) # Exit context, verify we have exited self.assertEqual(r.get_response("a")[0], 3) self.assertEqual(r.get_response("f")[0], NoResponse) self.assertFalse(r.context) # Verify we can re-enter self.assertEqual(r.get_response("e")[0], 1) self.assertEqual(r.get_response("f")[0], 2) self.assertIs(r.context, c)
def test_add_entry_phrases(self): c = Context().add_entry_phrases(("a", 1), ("b", 2)) c.add_response("c", 3) r = Responder().add_response("d", 4).add_context(c) # Verify not in context self.assertEqual(r.get_response("d")[0], 4) self.assertEqual(r.get_response("c")[0], NoResponse) self.assertFalse(r.context) # Enter context self.assertEqual(r.get_response("a")[0], 1) self.assertEqual(r.get_response("c")[0], 3) self.assertIs(r.context, c) # Exit context self.assertEqual(r.get_response("d")[0], 4) # Enter context self.assertEqual(r.get_response("b")[0], 2) self.assertEqual(r.get_response("c")[0], 3) self.assertIs(r.context, c)
def test_add_responses(self): r = Responder() r.add_responses(("abc*", 8), ("def+", 9)) self.assertEqual(r.get_response("ab")[0], 8) self.assertEqual(r.get_response("abc")[0], 8) self.assertEqual(r.get_response("abccccc")[0], 8) self.assertEqual(r.get_response("def")[0], 9) self.assertEqual(r.get_response("deff")[0], 9) self.assertEqual(r.get_response("deffff")[0], 9)
(["(.* )?(talk about|tell( me)? about) cats?.*"], ["Sure, I love cats"])) cat_context.add_responses( (["(.* )?favou?rite thing about (them|cats?).*"], ["They are fuzzy"]), (["(.* )?(do )?you have (one|(a )?cat).*" ], ["No, computer programs can't have cats."])) # Add a context for talking about cats dog_context = Context() dog_context.add_entry_phrases((["(.* )?(talk about|tell( me)? about) dogs?.*"], ["Sure, I think dogs are great"])) dog_context.add_responses( (["(.* )?favou?rite thing about (them|dogs?).*"], ["They are loyal"]), (["(.* )?(do )?you have (one|(a )?dog).*" ], ["No, computer programs can't have dogs."])) responder.add_default_response( ["Oh, really?", "Mmhmm.", "Indeed.", "How fascinating."]) responder.add_responses( (["(.* )?hello.*"], ["How do you do?", "Hello!", "Oh, hi."]), (["(. *)?(good)?bye.*"], ["Alright then, goodbye.", "See ya.", "Bye."])) responder.add_contexts(cat_context, dog_context) # Simple prompt to get input from command line and pass to responder while True: text = raw_input(" > ") resp, groups = responder.get_response(text) print("\n\"%s\"\n" % (random.choice(resp)))
if now_td < bedtime_td: return "No, your bedtime is at %s" % bedtimestr if now_td > bedtime_td: return "Yes, your bedtime was at %s" % bedtimestr return "Your bedtime is now!" def default_handler(groups): return "I don't understand that" responder = Responder() responder.add_responses( (["(.* )?bedtime is (.*).*"], set_bedtime_handler), (["(.* )?(what|when).*bedtime.*"], ask_bedtime_handler), (["(.* )?what( time is it|( i|'?)s the time).*"], current_time_handler), (["(.* )?(is )?(that|it) past (my )?bedtime.*"], bedtime_handler)) responder.add_default_response(default_handler) # Simple prompt to get input from command line and pass to responder while True: text = raw_input(" > ") handler, groups = responder.get_response(text) response = handler(groups) print("\n\"%s\"\n" % response)
def test_add_entry_phrase_list_or_string(self): c1 = Context().add_entry_phrase("a|b|c", 1).add_response("d", 2) c2 = Context().add_entry_phrase(["a", "b", "c"], 3).add_response("e", 4) r1 = Responder().add_response("x", 5).add_context(c1) r2 = Responder().add_response("y", 6).add_context(c2) self.assertEqual(r1.get_response("x")[0], 5) self.assertEqual(r1.get_response("d")[0], NoResponse) self.assertEqual(r2.get_response("y")[0], 6) self.assertEqual(r1.get_response("e")[0], NoResponse) # Verify we can enter context c1 with all 3 version of entry phrase self.assertEqual(r1.get_response("a")[0], 1) self.assertEqual(r1.get_response("d")[0], 2) self.assertEqual(r1.get_response("x")[0], 5) self.assertEqual(r1.get_response("b")[0], 1) self.assertEqual(r1.get_response("d")[0], 2) self.assertEqual(r1.get_response("x")[0], 5) self.assertEqual(r1.get_response("c")[0], 1) self.assertEqual(r1.get_response("d")[0], 2) self.assertEqual(r1.get_response("x")[0], 5) # Verify we can enter context c2 with all 3 version of entry phrase self.assertEqual(r2.get_response("a")[0], 3) self.assertEqual(r2.get_response("e")[0], 4) self.assertEqual(r2.get_response("y")[0], 6) self.assertEqual(r2.get_response("b")[0], 3) self.assertEqual(r2.get_response("e")[0], 4) self.assertEqual(r2.get_response("y")[0], 6) self.assertEqual(r2.get_response("c")[0], 3) self.assertEqual(r2.get_response("e")[0], 4) self.assertEqual(r2.get_response("y")[0], 6)