Esempio n. 1
0
    def setUp(self):

        self._client = TemplateGraphClient()
        self._client_context = self._client.create_client_context("testid")

        self._graph = self._client_context.bot.brain.aiml_parser.template_parser

        self.test_sentence = Sentence(self._client_context, "test sentence")

        test_node = PatternOneOrMoreWildCardNode("*")

        self.test_sentence._matched_context = MatchContext(
            max_search_depth=100, max_search_timeout=-1)
        self.test_sentence._matched_context._matched_nodes = [
            Match(Match.WORD, test_node, 'one'),
            Match(Match.WORD, test_node, 'two'),
            Match(Match.WORD, test_node, 'three'),
            Match(Match.WORD, test_node, 'four'),
            Match(Match.WORD, test_node, 'five'),
            Match(Match.WORD, test_node, 'six'),
            Match(Match.TOPIC, test_node, '*'),
            Match(Match.THAT, test_node, '*')
        ]

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        question = Question.create_from_sentence(self.test_sentence)
        conversation._questions.append(question)
Esempio n. 2
0
 def test_question_create_from_sentence(self):
     sentence = Sentence(self._client_context.brain.tokenizer, "One Two Three")
     question = Question.create_from_sentence(sentence)
     self.assertIsNotNone(question)
     self.assertEqual(1, len(question.sentences))
     self.assertEqual(sentence.text(), question.sentence(0).text())
     with self.assertRaises(Exception):
         question.sentence(1)
Esempio n. 3
0
    def test_transcripts_questions_with_props(self):
        client = TranscriptAdminExtensionClient()
        client_context = client.create_client_context("testid")

        question = Question.create_from_sentence(Sentence(client_context.brain.tokenizer, "Hello World"))
        conversation = client_context.bot.get_conversation(client_context)
        conversation.record_dialog(question)

        extension = TranscriptAdminExtension()
        self.assertIsNotNone(extension)

        result = extension.execute(client_context, "PROPERTIES")
        self.assertIsNotNone(result)
        self.assertEqual("Questions:<br /><ul><li>Hello World - </li></ul><br />Properties:<br /><ul><li>topic = *</li></ul><br />", result)
Esempio n. 4
0
    def test_to_json(self):
        client = TestClient()
        client_context = ClientContext(client, "testid")
        client_context.bot = Bot(BotConfiguration(), client)
        client_context.bot.configuration.conversations._max_histories = 3
        client_context.brain = client_context.bot.brain

        conversation1 = Conversation(client_context)
        conversation1.properties["convo1"] = "value1"
        matched_context = MatchContext(100, 100)
        matched_context.matched_nodes.append(Match(Match.WORD, PatternWordNode("Hello"), "Hello"))
        sentence = Sentence(client_context, text="Hi", response="Hello there", matched_context=matched_context)

        question1 = Question.create_from_sentence(sentence)
        question1.properties['quest1'] = "value2"
        conversation1.record_dialog(question1)

        json_data = conversation1.to_json()
        self.assertIsNotNone(json_data)

        self.assertEqual("testclient", json_data['client_context']['clientid'])
        self.assertEqual("testid", json_data['client_context']['userid'])
        self.assertEqual("bot", json_data['client_context']['botid'])
        self.assertEqual("brain", json_data['client_context']['brainid'])
        self.assertEqual(0, json_data['client_context']['depth'])

        conversation2 = Conversation.from_json(client_context, json_data)

        self.assertEqual(conversation1._client_context.client.id, conversation2._client_context.client.id)
        self.assertEqual(conversation1._client_context.userid, conversation2._client_context.userid)
        self.assertEqual(conversation1._client_context.bot.id, conversation2._client_context.bot.id)
        self.assertEqual(conversation1._client_context.brain.id, conversation2._client_context.brain.id)
        self.assertEqual(conversation1._client_context._question_start_time,
                          conversation2._client_context._question_start_time)
        self.assertEqual(conversation1._client_context._question_depth, conversation2._client_context._question_depth)
        self.assertEqual(conversation1._client_context._id, conversation2._client_context._id)

        self.assertEqual(conversation1.properties, conversation2.properties)
        self.assertEqual(conversation1.max_histories, conversation2.max_histories)

        self.assertNotEquals(0, len(conversation1.questions))
        self.assertNotEquals(0, len(conversation2.questions))
        self.assertEqual(len(conversation1.questions), len(conversation2.questions))

        for i in range(len(conversation2.questions)):
            q1 = conversation1.questions[i]
            q2 = conversation2.questions[i]

            self.assertEqual(q1.srai, q2.srai)
            self.assertEqual(q1._current_sentence_no, q2._current_sentence_no)

            self.assertEqual(q1.properties, q2.properties)

            self.assertNotEquals(0, len(q1.sentences))
            self.assertNotEquals(0, len(q2.sentences))
            self.assertEqual(len(q1.sentences), len(q2.sentences))

            for j in range(len(q2.sentences)):
                s1 = q1.sentences[j]
                s2 = q2.sentences[j]

                self.assertEqual(s1.words, s2.words)
                self.assertEqual(s1.response, s2.response)
                self.assertEqual(s1.positivity, s2.positivity)
                self.assertEqual(s1.subjectivity, s2.subjectivity)

                mc1 = s1.matched_context
                mc2 = s2.matched_context

                self.assertEquals(mc1.template_node, mc2.template_node)
                self.assertEquals(mc1.max_search_depth, mc2.max_search_depth)
                self.assertEquals(mc1.max_search_timeout, mc2.max_search_timeout)
                time1 = mc1._total_search_start.strftime("%d/%m/%Y, %H:%M:%S")
                time2 = mc2._total_search_start.strftime("%d/%m/%Y, %H:%M:%S")
                self.assertEquals(time1, time2)

                self.assertNotEquals(0, len(mc1.matched_nodes))
                self.assertNotEquals(0, len(mc2.matched_nodes))
                self.assertEquals(len(mc1.matched_nodes), len(mc2.matched_nodes))

                for k in range(len(mc1.matched_nodes)):
                    mn1 = mc1.matched_nodes[k]
                    mn2 = mc2.matched_nodes[k]

                    self.assertEquals(mn1._matched_node_str, mn2._matched_node_str)
                    self.assertEquals(mn1._matched_node_type, mn2._matched_node_type)
                    self.assertEquals(mn1._matched_node_multi_word, mn2._matched_node_multi_word)
                    self.assertEquals(mn1._matched_node_wildcard, mn2._matched_node_wildcard)
                    self.assertEquals(mn1._matched_node_words, mn2._matched_node_words)