class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() def test_no_choices(self): from chatterbot.adapters.exceptions import EmptyDatasetException possible_choices = [] statement = Statement("Hello") with self.assertRaises(EmptyDatasetException): self.adapter.get(statement, possible_choices) def test_get_closest_statement(self): """ Note, the content of the in_response_to field for each of the test statements is only required because the logic adapter will filter out any statements that are not in response to a known statement. """ possible_choices = [ Statement("This is a lovely bog.", in_response_to=[Response("This is a lovely bog.")]), Statement("This is a beautiful swamp.", in_response_to=[Response("This is a beautiful swamp.")]), Statement("It smells like swamp.", in_response_to=[Response("It smells like swamp.")]) ] statement = Statement("This is a lovely swamp.") confidence, match = self.adapter.get(statement, possible_choices) self.assertEqual("This is a lovely bog.", match)
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() def test_no_choices(self): from chatterbot.adapters.exceptions import EmptyDatasetException possible_choices = [] statement = Statement("Hello") with self.assertRaises(EmptyDatasetException): self.adapter.get(statement, possible_choices) def test_get_closest_statement(self): possible_choices = [ Statement("This is a lovely bog."), Statement("This is a beautiful swamp."), Statement("It smells like swamp.") ] statement = Statement("This is a lovely swamp.") confidence, match = self.adapter.get(statement, possible_choices) self.assertEqual("This is a lovely bog.", match)
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() # Add a mock storage adapter to the context self.adapter.set_context(MockContext()) def test_no_choices(self): self.adapter.context.storage.filter = MagicMock(return_value=[]) statement = Statement("Hello") with self.assertRaises(ClosestMeaningAdapter.EmptyDatasetException): self.adapter.get(statement) def test_get_closest_statement(self): """ Note, the content of the in_response_to field for each of the test statements is only required because the logic adapter will filter out any statements that are not in response to a known statement. """ possible_choices = [ Statement("This is a lovely bog.", in_response_to=[Response("This is a lovely bog.")]), Statement("This is a beautiful swamp.", in_response_to=[Response("This is a beautiful swamp.")]), Statement("It smells like swamp.", in_response_to=[Response("It smells like swamp.")]) ] self.adapter.context.storage.filter = MagicMock(return_value=possible_choices) statement = Statement("This is a lovely swamp.") confidence, match = self.adapter.get(statement) self.assertEqual("This is a lovely bog.", match)
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() # Add a mock storage adapter to the context self.adapter.set_context(MockContext()) def test_no_choices(self): self.adapter.context.storage.filter = MagicMock(return_value=[]) statement = Statement("Hello") with self.assertRaises(ClosestMeaningAdapter.EmptyDatasetException): self.adapter.get(statement) def test_get_closest_statement(self): """ Note, the content of the in_response_to field for each of the test statements is only required because the logic adapter will filter out any statements that are not in response to a known statement. """ possible_choices = [ Statement("This is a lovely bog.", in_response_to=[Response("This is a lovely bog.")]), Statement("This is a beautiful swamp.", in_response_to=[Response("This is a beautiful swamp.")]), Statement("It smells like swamp.", in_response_to=[Response("It smells like swamp.")]) ] self.adapter.context.storage.filter = MagicMock( return_value=possible_choices) statement = Statement("This is a lovely swamp.") confidence, match = self.adapter.get(statement) self.assertEqual("This is a lovely bog.", match)
def __init__(self, **kwargs): super(SearchHubLogicAdapter, self).__init__(**kwargs) self.host = kwargs.get("searchhub_host") self.port = kwargs.get("searchhub_port") self.shub_url = "http://{0}:{1}/api/apollo/query-pipelines/lucidfind-default/collections/lucidfind/select".format(self.host, self.port) self.shub_display_url = "http://{0}:{1}/p:%20?{2}" #note the %20 is a hack around how shub handles the projects, eventually we'll pass in filters here print "URL: {0}".format(self.shub_url) self.closest = ClosestMeaningAdapter()
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() def test_no_choices(self): possible_choices = [] close = self.adapter.get("Hello", possible_choices) self.assertEqual("Hello", close) def test_get_closest_statement(self): possible_choices = [ "This is a lovely bog.", "This is a beautiful swamp.", "It smells like swamp." ] close = self.adapter.get("This is a lovely swamp.", possible_choices) self.assertEqual("This is a lovely bog.", close)
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter(None) def test_no_choices(self): possible_choices = [] statement = Statement("Hello") with self.assertRaises(EmptyDatasetException): self.adapter.get(statement, possible_choices) def test_get_closest_statement(self): possible_choices = [ Statement("This is a lovely bog."), Statement("This is a beautiful swamp."), Statement("It smells like swamp.") ] statement = Statement("This is a lovely swamp.") close = self.adapter.get(statement, possible_choices) self.assertEqual("This is a lovely bog.", close)
def setUp(self): self.adapter = ClosestMeaningAdapter()
def setUp(self): self.adapter = ClosestMeaningAdapter() # Add a mock storage adapter to the context self.adapter.set_context(MockContext())
class SearchHubLogicAdapter(LogicAdapter): def __init__(self, **kwargs): super(SearchHubLogicAdapter, self).__init__(**kwargs) self.host = kwargs.get("searchhub_host") self.port = kwargs.get("searchhub_port") self.shub_url = "http://{0}:{1}/api/apollo/query-pipelines/lucidfind-default/collections/lucidfind/select".format(self.host, self.port) self.shub_display_url = "http://{0}:{1}/p:%20?{2}" #note the %20 is a hack around how shub handles the projects, eventually we'll pass in filters here print "URL: {0}".format(self.shub_url) self.closest = ClosestMeaningAdapter() def can_process(self, statement): #print statement.extra_data is_q = False is_bot_ask = False if "is_question" in statement.extra_data: is_q = statement.extra_data["is_question"] if "is_bot_ask" in statement.extra_data: is_bot_ask = statement.extra_data["is_bot_ask"] if statement.text.lower().startswith("shub:"): return True return is_q or is_bot_ask def process(self, statement): print "calculating: {0}, {1}".format(statement, statement.extra_data) result = None input_channel = statement.extra_data["slack_channel"] is_q = statement.extra_data["is_question"] is_bot_ask = statement.extra_data["is_bot_ask"] text = statement.text if statement.text.lower().startswith("shub:"):#If the user specifically asks for 'searchhub' (shub), then strip it off text = text[5:] is_bot_ask = True params = { "wt":"json", "q": text, "rows": 3, "fl": "id,title,subject,body,score", "fq":"isBot:false" } #TODO: filter projects based on channels print "Params: {0}".format(params) response = requests.get(self.shub_url, params) if response.status_code != 200: return 0, None rsp = response.json() #let's see if we have a decent response confidence = 0 if "response" in rsp: #print rsp num_found = rsp["response"]["numFound"] print num_found if num_found > 0: url = self.shub_display_url.format(self.host, self.port, urllib.urlencode({"q": text})) response = "SearchHub says...\n\tSee full results: {0}\n\n".format(url) docs = rsp["response"]["docs"] #print "docs: {0}".format(docs[0]) #confidence = docs[0]["score"] #Confidence is the average of the similarity scores, for now i = 0 for doc in docs: display = "" if "title" in doc: display = doc["title"] elif "subject" in doc: display = doc["subject"] #see how similar the display value is to the original similarity = self.closest.get_similarity(text, display) print similarity confidence += similarity#should we add here? Probably not, but let's try it response += "{0}:\n\t{1}\n\t{2}\n".format(i, doc["id"], display.encode('utf-8')) i += 1 result = Statement(response) #pass through the metadata, so we have channel info if i > 0: confidence = confidence / i if is_bot_ask: # boost confidence if the user specifically invoked the kraken confidence += 10 else: print "Couldn't find results for {0}".format(params) result = Statement("Shub no find answer.") confidence = 0 #print "Conf: {0}, Res: {1}".format(confidence, result) else: #print "Couldn't find results for {0}".format(params) result = Statement("Shub no find answer.") confidence = 0 result.extra_data = statement.extra_data return confidence, result
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() # Add a mock storage adapter to the context self.adapter.set_context(MockContext()) def test_no_choices(self): self.adapter.context.storage.filter = MagicMock(return_value=[]) statement = Statement('Hello') with self.assertRaises(ClosestMeaningAdapter.EmptyDatasetException): self.adapter.get(statement) def test_get_closest_statement(self): """ Note, the content of the in_response_to field for each of the test statements is only required because the logic adapter will filter out any statements that are not in response to a known statement. """ possible_choices = [ Statement('This is a lovely bog.', in_response_to=[Response('This is a lovely bog.')]), Statement('This is a beautiful swamp.', in_response_to=[Response('This is a beautiful swamp.')]), Statement('It smells like swamp.', in_response_to=[Response('It smells like swamp.')]) ] self.adapter.context.storage.filter = MagicMock( return_value=possible_choices ) statement = Statement('This is a lovely swamp.') confidence, match = self.adapter.get(statement) self.assertEqual('This is a lovely bog.', match) def test_different_punctuation(self): possible_choices = [ Statement('Who are you?'), Statement('Are you good?'), Statement('You are good') ] self.adapter.context.storage.get_response_statements = MagicMock( return_value=possible_choices ) statement = Statement('Are you good') confidence, match = self.adapter.get(statement) self.assertEqual('Are you good?', match) def test_no_known_responses(self): """ In the case that a match is selected which has no known responses. In this case a random response will be returned, but the confidence should be zero because it is a random choice. """ self.adapter.context.storage.update = MagicMock() self.adapter.context.storage.filter = MagicMock( return_value=[] ) self.adapter.context.storage.get_random = MagicMock( return_value=Statement("Random") ) confidence, match = self.adapter.process(Statement("Blah")) self.assertEqual(confidence, 0) self.assertEqual(match.text, "Random")
def __init__(self): self.storage = StorageAdapter() self.storage.get_random = Mock( side_effect=ClosestMeaningAdapter.EmptyDatasetException())
class ClosestMeaningAdapterTests(TestCase): def setUp(self): self.adapter = ClosestMeaningAdapter() # Add a mock storage adapter to the context self.adapter.set_context(MockContext()) def test_no_choices(self): self.adapter.context.storage.filter = MagicMock(return_value=[]) statement = Statement('Hello') with self.assertRaises(ClosestMeaningAdapter.EmptyDatasetException): self.adapter.get(statement) def test_get_closest_statement(self): """ Note, the content of the in_response_to field for each of the test statements is only required because the logic adapter will filter out any statements that are not in response to a known statement. """ possible_choices = [ Statement('This is a lovely bog.', in_response_to=[Response('This is a lovely bog.')]), Statement('This is a beautiful swamp.', in_response_to=[Response('This is a beautiful swamp.')]), Statement('It smells like swamp.', in_response_to=[Response('It smells like swamp.')]) ] self.adapter.context.storage.filter = MagicMock( return_value=possible_choices) statement = Statement('This is a lovely swamp.') confidence, match = self.adapter.get(statement) self.assertEqual('This is a lovely bog.', match) def test_different_punctuation(self): possible_choices = [ Statement('Who are you?'), Statement('Are you good?'), Statement('You are good') ] self.adapter.context.storage.get_response_statements = MagicMock( return_value=possible_choices) statement = Statement('Are you good') confidence, match = self.adapter.get(statement) self.assertEqual('Are you good?', match) def test_no_known_responses(self): """ In the case that a match is selected which has no known responses. In this case a random response will be returned, but the confidence should be zero because it is a random choice. """ self.adapter.context.storage.update = MagicMock() self.adapter.context.storage.filter = MagicMock(return_value=[]) self.adapter.context.storage.get_random = MagicMock( return_value=Statement("Random")) confidence, match = self.adapter.process(Statement("Blah")) self.assertEqual(confidence, 0) self.assertEqual(match.text, "Random")