コード例 #1
0
    def test_add_response_list_or_string(self):
        r1 = Responder()
        r2 = Responder()

        r1.add_response("a|b", 1)
        r1.add_default_response(2)

        r2.add_response(["a", "b"], 1)
        r2.add_default_response(2)

        for r in [r1, r2]:
            self.assertEqual(r.get_response("a")[0], 1)
            self.assertEqual(r.get_response("b")[0], 1)
            self.assertEqual(r.get_response("e")[0], 2)
            self.assertEqual(r.get_response("gfnhvmnbvjm")[0], 2)
コード例 #2
0
    def test_add_responses_list_or_string(self):
        r1 = Responder()
        r2 = Responder()

        r1.add_responses(("a|b", 1), ("c|d", 2))
        r1.add_default_response(3)

        r2.add_responses((["a", "b"], 1), (["c", "d"], 2))
        r2.add_default_response(3)

        for r in [r1, r2]:
            self.assertEqual(r.get_response("a")[0], 1)
            self.assertEqual(r.get_response("b")[0], 1)
            self.assertEqual(r.get_response("c")[0], 2)
            self.assertEqual(r.get_response("d")[0], 2)
            self.assertEqual(r.get_response("e")[0], 3)
            self.assertEqual(r.get_response("gfnhvmnbvjm")[0], 3)
コード例 #3
0
    (["(.* )?(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)))
コード例 #4
0
    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)