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)
Beispiel #2
0
    def test_add_context(self):
        num_tests = 100
        r = Responder()

        for i in range(num_tests):
            r.add_context(Context())
            self.assertEqual(len(r.contexts), i + 1)
Beispiel #3
0
 def test_add_contexts_invalid(self):
     r = Responder()
     self.assertRaises(ValueError, r.add_contexts, 5)
     self.assertRaises(ValueError, r.add_contexts, "test")
     self.assertRaises(ValueError, r.add_contexts, True)
     self.assertRaises(ValueError, r.add_contexts, Responder())
     self.assertRaises(ValueError, r.add_contexts, Context(), 5)
Beispiel #4
0
    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)
Beispiel #5
0
    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)
Beispiel #6
0
    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)
    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_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)
    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_context_invalid(self):
     c = Context()
     self.assertRaises(ValueError, c.add_context, 5)
     self.assertRaises(ValueError, c.add_context, True)
     self.assertRaises(ValueError, c.add_context, "test")
     self.assertRaises(ValueError, c.add_context, Responder())
Beispiel #12
0
 def test_add_response_invalid(self):
     r = Responder()
     self.assertRaises(ValueError, r.add_response, 8, None)
     self.assertRaises(ValueError, r.add_response, True, None)
Beispiel #13
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)
Beispiel #14
0
 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))
Beispiel #15
0
        return "No, your bedtime is at %s" % bedtimestr

    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)
Beispiel #16
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)
Beispiel #17
0
#     "No, computer programs can't have cats."
#     > OK, let's talk about dogs now
#     "Sure, I think dogs are great"
#     > do you have one?
#     "No, computer programs can't have dogs."
#     > what's your favourite thing about them?
#     "They are loyal"

import random
import time

from chatbot_utils.responder import Responder, Context

random.seed(time.time())

responder = Responder()

# Add a context for talking about cats
cat_context = Context()
cat_context.add_entry_phrases(
    (["(.* )?(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"]))
Beispiel #18
0
 def test_add_responses_invalid(self):
     r = Responder()
     self.assertRaises(ValueError, r.add_responses, (8, 0))
     self.assertRaises(ValueError, r.add_responses, (True, 0))
     self.assertRaises(ValueError, r.add_responses, ("valid", 0),
                       (False, 0))