def test_compile(self): r = Responder().add_response("f?", 0) c1 = Context().add_entry_phrase("a+", 1).add_response("b+", 2) c1.add_chained_phrases(("x", 3), ("y", 4), ("z", 5)) c2 = Context().add_entry_phrase("q+", 6).add_response("t*", 7) c2.add_chained_phrases(("x", 3), ("y", 4), ("z", 5)) r.add_contexts(c1, c2) # Verify nothing's been compiled yet for responsedict in iterate_redicts(r): self.assertFalse(responsedict.compiled) # Get type of compiled regex retype = type(re.compile("abc+")) # Compile responder r.compile() # Verify everything's compiled now for responsedict in iterate_redicts(r): self.assertTrue(len(responsedict.compiled) > 0) for compiled in responsedict.compiled: self.assertEqual(type(compiled), retype)
(["(.* )?(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)))
def test_add_contexts(self): num_tests = 100 r = Responder() contexts = [Context() for _ in range(num_tests)] r.add_contexts(*contexts) self.assertEqual(len(contexts), len(r.contexts))