예제 #1
0
class Adaptation(object):
    """
    This is a base from which ChatterBot inherits utility methods
    and a context attribute that allows access of adapters to be
    shared between other adapters. This also makes it possible to
    share other context information such as a name, or the current
    conversation with each of the adapters.
    """

    def __init__(self, **kwargs):
        self.storage_adapters = []
        self.io_adapters = []

        self.logic = MultiLogicAdapter(**kwargs)
        self.logic.set_context(self)

    def add_adapter(self, adapter, **kwargs):
        NewAdapter = import_module(adapter)

        adapter = NewAdapter(**kwargs)

        if issubclass(NewAdapter, StorageAdapter):
            self.storage_adapters.append(adapter)
        elif issubclass(NewAdapter, LogicAdapter):
            self.logic.add_adapter(adapter)
        elif issubclass(NewAdapter, IOAdapter):
            self.io_adapters.append(adapter)
        else:
            raise UnknownAdapterTypeException()
예제 #2
0
    def test_set_context(self):
        adapter = MultiLogicAdapter()
        adapter.set_context(self.chatbot)

        # Test that the multi adapter's context is set
        self.assertEqual(adapter.context, self.chatbot)

        # Test that all sub adapters have the context set
        for sub_adapter in adapter.adapters:
            self.assertEqual(sub_adapter.context, self.chatbot)
예제 #3
0
    def __init__(self, **kwargs):
        self.storage_adapters = []

        self.io = MultiIOAdapter(**kwargs)

        self.logic = MultiLogicAdapter(**kwargs)
        self.logic.set_context(self)

        # Add required system adapter
        self.add_adapter("chatterbot.adapters.logic.NoKnowledgeAdapter")
예제 #4
0
class MultiLogicAdapterTestCase(ChatBotTestCase):

    def setUp(self):
        super(MultiLogicAdapterTestCase, self).setUp()
        self.adapter = MultiLogicAdapter()
        self.adapter.set_context(self.chatbot)

    def test_sub_adapter_agreement(self):
        """
        In the case that multiple adapters agree on a given
        statement, this statement should be returned with the
        highest confidence available from these matching options.
        """
        self.adapter.add_adapter(TestAdapterA())
        self.adapter.add_adapter(TestAdapterB())
        self.adapter.add_adapter(TestAdapterC())

        confidence, statement = self.adapter.process(Statement('Howdy!'))

        self.assertEqual(confidence, 0.5)
        self.assertEqual(statement, 'Good morning.')

    def test_get_greatest_confidence(self):
        statement = 'Hello'
        options = [
            (0.50, 'Hello'),
            (0.85, 'Hello'),
            (0.42, 'Hello')
        ]
        value = self.adapter.get_greatest_confidence(statement, options)

        self.assertEqual(value, 0.85)

    def test_add_adapter(self):
        sub_adapter = TestAdapterA()
        adapter_count_before = len(self.adapter.adapters)
        self.adapter.add_adapter(sub_adapter)
        adapter_count_after = len(self.adapter.adapters)

        self.assertEqual(adapter_count_after, adapter_count_before + 1)

    def test_set_context(self):
        adapter = MultiLogicAdapter()
        adapter.set_context(self.chatbot)

        # Test that the multi adapter's context is set
        self.assertEqual(adapter.context, self.chatbot)

        # Test that all sub adapters have the context set
        for sub_adapter in adapter.adapters:
            self.assertEqual(sub_adapter.context, self.chatbot)
예제 #5
0
    def __init__(self, **kwargs):
        self.storage_adapters = []
        self.io_adapters = []

        self.logic = MultiLogicAdapter(**kwargs)
        self.logic.set_context(self)

        # Add required system adapter
        self.add_adapter("chatterbot.adapters.logic.NoKnowledgeAdapter")
예제 #6
0
 def setUp(self):
     super(MultiLogicAdapterTestCase, self).setUp()
     self.adapter = MultiLogicAdapter()
     self.adapter.set_context(self.chatbot)
예제 #7
0
    def __init__(self, **kwargs):
        self.storage_adapters = []
        self.io_adapters = []

        self.logic = MultiLogicAdapter(**kwargs)
        self.logic.set_context(self)