예제 #1
0
    def test_question_sentiment(self):

        question = Question.create_from_text(self.client_context,
                                             "Hello There. How Are you")

        for sentence in question.sentences:
            sentence.calculate_sentinment_score(self.client_context)

        positivity, subjectivity = question.calculate_sentinment_score()
        self.assertEqual(0.0, positivity)
        self.assertEqual(0.0, subjectivity)

        question = Question.create_from_text(
            self.client_context, "I hate you. Your car is rubbish")
        question.recalculate_sentinment_score(self.client_context)

        positivity, subjectivity = question.calculate_sentinment_score()
        self.assertEqual(-0.4, positivity)
        self.assertEqual(0.45, subjectivity)
예제 #2
0
    def create_from_json(self, json_data):

        for key, value in json_data['properties'].items():
            self._properties[key] = value

        for json_question in json_data['questions']:
            self._questions.append(
                Question.from_json(self._client_context, json_question))

        self.recalculate_sentiment_score(self._client_context)
예제 #3
0
    def test_combine_answers(self):
        question = Question()
        sentence1 = Sentence(self._client_context.brain.tokenizer, "Hi")
        sentence1._response = "Hello"
        question._sentences.append(sentence1)
        sentence2 = Sentence(self._client_context.brain.tokenizer, "Hi Again")
        question._sentences.append(sentence2)
        sentence2._response = "World"

        self.assertEqual(2, len(question._sentences))
        self.assertEqual(question._sentences[0]._response, "Hello")
        self.assertEqual(question._sentences[1]._response, "World")

        sentences = question.combine_sentences()
        self.assertEqual("Hi. Hi Again", sentences)

        combined = question.combine_answers()
        self.assertIsNotNone(combined)
        self.assertEqual(combined, "Hello. World")
예제 #4
0
    def test_node_no_sentences(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        question = Question()
        conversation.record_dialog(question)
        self._client_context.bot._conversation_mgr._conversations["testid"] = conversation

        self.assertEqual("", root.resolve(self._client_context))
예제 #5
0
    def test_node_no_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(
            self._client_context, "Hello world",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text(
            self._client_context, "How are you",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        self._client_context.bot._conversation_mgr._conversations[
            "testid"] = conversation

        self.assertEqual("", root.resolve(self._client_context))
예제 #6
0
 def from_json(self, client_context, json_data):
     if json_data is not None:
         json_questions = json_data['questions']
         for json_question in json_questions:
             json_sentences = json_question['sentences']
             for json_sentence in json_sentences:
                 question = Question.create_from_text(
                     self._client_context, json_sentence['question'])
                 question.sentence(0).response = json_sentence['response']
                 self._questions.append(question)
         self.recalculate_sentiment_score(client_context)
예제 #7
0
    def test_conversation_sentiment(self):
        conversation = Conversation(self.client_context)

        question1 = Question.create_from_text(self.client_context,
                                              "I am so unhappy")
        conversation.record_dialog(question1)

        question2 = Question.create_from_text(self.client_context,
                                              "I do not like the colour red")
        conversation.record_dialog(question2)

        question3 = Question.create_from_text(self.client_context,
                                              "Custard makes me feel sick")
        conversation.record_dialog(question3)

        conversation.recalculate_sentiment_score(self.client_context)

        positivity, subjectivity = conversation.calculate_sentiment_score()
        self.assertEqual(-0.4380952380952381, positivity)
        self.assertEqual(0.5857142857142857, subjectivity)
예제 #8
0
 def test_question_multi_sentence(self):
     question = Question.create_from_text(self._client_context,
                                          "Hello There. How Are you")
     self.assertIsNotNone(question)
     self.assertEqual(2, len(question.sentences))
     self.assertEqual("Hello There",
                      question.sentence(0).text(self._client_context))
     self.assertEqual("How Are you",
                      question.sentence(1).text(self._client_context))
     with self.assertRaises(Exception):
         question.sentence(2)
예제 #9
0
 def test_next_previous_nth_sentences(self):
     question = Question.create_from_text(self._client_context,
                                          "Hello There. How Are you")
     self.assertEqual(
         "How Are you",
         question.current_sentence().text(self._client_context))
     self.assertEqual(
         "How Are you",
         question.previous_nth_sentence(0).text(self._client_context))
     self.assertEqual(
         "Hello There",
         question.previous_nth_sentence(1).text(self._client_context))
예제 #10
0
    def test_node_with_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(self._client_context, "Hello world", self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text(self._client_context, "How are you", self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.WORD, match, "Matched"))
        question.current_sentence()._matched_context = context

        conversation.record_dialog(question)
        self._client_context.bot._conversation_mgr._conversations["testid"] = conversation

        self.assertEqual("Matched", root.resolve(self._client_context))
예제 #11
0
    def test_attrib_with_html(self):
        template = ET.fromstring("""
            <template>
                <a target="_new" href="http://www.google.com/search?q=&lt;star /&gt;"> Google Search </a>
            </template>
            """)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(self._client_context, "GOOGLE AIML", self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "OK"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1)
        context.add_match(Match(Match.WORD, match, "AIML"))
        question.current_sentence()._matched_context = context
        self._client_context.bot._conversation_mgr._conversations["testid"] = conversation

        ast = self._graph.parse_template_expression(template)
        self.assertIsNotNone(ast)
        self.assertIsInstance(ast, TemplateNode)
        self.assertIsNotNone(ast.children)
        self.assertEqual(len(ast.children), 1)

        xml_node = ast.children[0]
        self.assertIsNotNone(xml_node)
        self.assertIsInstance(xml_node, TemplateXMLNode)

        attribs = xml_node.attribs
        self.assertEquals(2, len(attribs))

        self.assertIsInstance(attribs['target'], TemplateWordNode)
        target = attribs['target']
        self.assertEquals(len(target.children), 0)
        self.assertEquals("_new", target.word)

        self.assertIsInstance(attribs['href'], TemplateNode)
        href = attribs['href']
        self.assertEquals(len(href.children), 3)

        self.assertIsInstance(href.children[0], TemplateWordNode)
        self.assertEquals('http://www.google.com/search?q=', href.children[0].word)

        self.assertIsInstance(href.children[1], TemplateNode)
        self.assertEquals(1, len(href.children[1].children))
        star = href.children[1].children[0]
        self.assertIsInstance(star, TemplateStarNode)

        self.assertIsInstance(href.children[2], TemplateWordNode)
        self.assertEquals('', href.children[2].word)

        result = xml_node.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEquals(result, '<a target="_new" href="http://www.google.com/search?q=AIML">Google Search</a>')
예제 #12
0
    def test_resolve_with_defaults_inside_topic(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateTopicStarNode()
        self.assertIsNotNone(node)
        self.assertIsInstance(node.index, TemplateNode)

        root.append(node)
        self.assertEqual(len(root.children), 1)

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(
            self._client_context, "Hello world",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text(
            self._client_context, "How are you",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=-1,
                               tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.TOPIC, match, "Matched"))
        question.current_sentence()._matched_context = context
        conversation.record_dialog(question)

        self._client_context.bot._conversation_mgr._conversations[
            "testid"] = conversation

        self.assertEqual("Matched", root.resolve(self._client_context))
예제 #13
0
    def test_store_conversation(self):
        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation = Conversation(client_context)

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        conversation.record_dialog(question1)

        convo_store = ConversationStore()
        with self.assertRaises(NotImplementedError):
            convo_store.store_conversation(client_context, conversation)
예제 #14
0
    def test_combine_answers(self):
        question = Question()
        sentence1 = Sentence(self._client_context, "Hi")
        sentence1._response = "Hello"
        question.sentences.append(sentence1)
        sentence2 = Sentence(self._client_context, "Hi Again")
        question.sentences.append(sentence2)
        sentence2._response = "World"

        self.assertEqual(2, len(question.sentences))
        self.assertEqual(question.sentences[0]._response, "Hello")
        self.assertEqual(question.sentences[1]._response, "World")

        sentences = question.combine_sentences(self._client_context)
        self.assertEqual("Hi. Hi Again", sentences)

        combined = question.combine_answers()
        self.assertIsNotNone(combined)
        self.assertEqual(combined, "Hello. World")

        self.assertEquals("Hi = Hello, Hi Again = World",
                          question.debug_info(self._client_context))
예제 #15
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)
예제 #16
0
    def test_sentiment_feeling_last_10(self):
        extension = SentimentExtension()
        self.assertIsNotNone(extension)

        # Need to create a conversation first

        conversation = self.client_context.bot.get_conversation(self.client_context)
        self.assertIsNotNone(conversation)
        question = Question.create_from_text(self.client_context, "Hello", self.client_context.bot.sentence_splitter)
        conversation.record_dialog(question)

        result = extension.execute(self.client_context, "SENTIMENT FEELING LAST 10")
        self.assertIsNotNone(result)
        self.assertEqual("SENTIMENT FEELING NEUTRAL AND NEUTRAL", result)
예제 #17
0
    def test_to_document_with_id(self):

        client = TestClient()
        client_context = client.create_client_context("testuser")

        convo = Convo(client_context)

        question = Question.create_from_text(client_context, "Hello world")
        question.current_sentence()._response = "Hello matey"
        convo.record_dialog(question)

        conversation = Conversation(client_context, convo)
        conversation.id = '666'

        doc = conversation.to_document()
        self.assertEqual(
            {
                '_id': '666',
                'clientid': 'testclient',
                'userid': 'testuser',
                'botid': 'bot',
                'brainid': 'brain',
                'conversation': {
                    'client_context': {
                        'clientid': 'testclient',
                        'userid': 'testuser',
                        'botid': 'bot',
                        'brainid': 'brain',
                        'depth': 0
                    },
                    'questions': [{
                        'srai':
                        False,
                        'sentences': [{
                            'words': ['Hello', 'world'],
                            'response': 'Hello matey',
                            'positivity': 0.0,
                            'subjectivity': 0.5
                        }],
                        'current_sentence_no':
                        -1,
                        'properties': {}
                    }],
                    'max_histories':
                    100,
                    'properties': {
                        'topic': '*'
                    }
                }
            }, doc)
예제 #18
0
    def conversation_asserts(self, storage_engine, visit=True):

        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation = Conversation(client_context)

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        conversation.record_dialog(question1)

        convo_store = storage_engine.conversation_store()
        convo_store.store_conversation(client_context, conversation)
        convo_store.commit()
예제 #19
0
    def test_conversation_in_db(self):
        config = SQLStorageConfiguration()
        engine = SQLStorageEngine(config)
        engine.initialise()
        store = SQLConversationStore(engine)

        store.empty()

        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation1 = Conversation(client_context)

        conversation1.properties['ckey1'] = "cvalue1"
        conversation1.properties['ckey2'] = "cvalue2"

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        question1.sentence(0)._positivity = 0.5
        question1.sentence(0)._subjectivity = 0.6
        question1.properties['qkey1'] = "qvalue1"
        question1.properties['qkey2'] = "qvalue2"

        conversation1.record_dialog(question1)

        store.store_conversation(client_context, conversation1)

        store.commit()

        conversation2 = Conversation(client_context)

        store.load_conversation(client_context, conversation2)

        self.assertEquals(conversation2.properties['ckey1'], "cvalue1")
        self.assertEquals(conversation2.properties['ckey2'], "cvalue2")

        self.assertEquals(conversation2.questions[0].sentence(0).response,
                          "Hi")
        self.assertEquals(conversation2.questions[0].sentence(0)._positivity,
                          0.5)
        self.assertEquals(conversation2.questions[0].sentence(0)._subjectivity,
                          0.6)

        self.assertEquals(conversation2.questions[0].properties['qkey1'],
                          "qvalue1")
        self.assertEquals(conversation2.questions[0].properties['qkey2'],
                          "qvalue2")

        store.empty()
예제 #20
0
    def _read_questions_from_db(self, client_context, conversationid, conversation):
        questiondaos = self._storage_engine.session.query(QuestionDAO).\
                            filter(QuestionDAO.conversationid==conversationid)

        for questiondao in questiondaos:
            YLogger.debug(client_context, "Loading question %s", questiondao)

            question = Question(questiondao.srai)

            self._read_properties_from_db(client_context, conversationid, questiondao.id, ConversationPropertyDAO.QUESTION,
                                          question.properties)

            self._read_sentences_from_db(client_context, questiondao.id, question)

            conversation.record_dialog(question)
예제 #21
0
    def test_repr_with_id(self):
        client = TestClient()
        client_context = client.create_client_context("testuser")

        convo = Convo(client_context)

        question = Question.create_from_text(client_context, "Hello world")
        question.current_sentence()._response = "Hello matey"
        convo.record_dialog(question)

        conversation = Conversation(client_context, convo)
        conversation.id = '1'

        self.assertEquals(
            "<Conversation(id='1', client='testclient', user='******', bot='bot', brain='brain')",
            str(conversation))
예제 #22
0
    def test_store_converstion_no_logger(self):
        config = LoggerStorageConfiguration()
        store = MockLoggerConversationStore(config)

        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation = Conversation(client_context)

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        conversation.record_dialog(question1)

        store.store_conversation(client_context, conversation)

        self.assertFalse(store.logged)
예제 #23
0
    def test_get_user_global_missing(self):

        client = PropertiesAdminExtensionClient()
        client_context = client.create_client_context("testid")

        question = Question()
        conversation = client_context.bot.get_conversation(client_context)
        conversation.record_dialog(question)
        conversation.set_property("PROP1", "Value1")

        extension = PropertiesAdminExtension()
        self.assertIsNotNone(extension)

        result = extension.execute(client_context, "GET USER GLOBAL")
        self.assertIsNotNone(result)
        self.assertEqual("Missing variable name for GET USER", result)
예제 #24
0
    def test_question_set_sentencesy(self):
        question = Question.create_from_text(self._client_context, "")
        self.assertIsNotNone(question)
        self.assertEqual(0, len(question.sentences))

        question.sentences = [
            Sentence(self._client_context, "Sentence One"),
            Sentence(self._client_context, "Sentence Two")
        ]
        self.assertEqual(2, len(question.sentences))
        self.assertEqual("Sentence One = N/A, Sentence Two = N/A",
                         question.debug_info(self._client_context))

        self.assertEquals(-1, question.current_sentence_no)
        question.current_sentence_no = 1
        self.assertEquals(1, question.current_sentence_no)
예제 #25
0
    def test_get_user_other(self):

        client = PropertiesAdminExtensionClient()
        client_context = client.create_client_context("testid")

        question = Question()
        conversation = client_context.bot.get_conversation(client_context)
        conversation.record_dialog(question)
        conversation.set_property("PROP1", "Value1")

        extension = PropertiesAdminExtension()
        self.assertIsNotNone(extension)

        result = extension.execute(client_context, "GET USER XXXX")
        self.assertIsNotNone(result)
        self.assertEqual("Invalid GET USER var type [XXXX]", result)
예제 #26
0
    def test_store_conversation(self):
        config = LoggerStorageConfiguration()
        engine = LoggerStorageEngine(config)
        engine.initialise()
        store = LoggerConversationStore(engine)

        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation = Conversation(client_context)

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        conversation.record_dialog(question1)

        store.store_conversation(client_context, conversation)
예제 #27
0
    def test_nlu_match_as_no_nlu_result(self):

        self.add_pattern_to_graph(pattern='<nlu intent="transportation"/>',
                                  topic="*",
                                  that="*",
                                  template="1")

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        self.assertIsNotNone(conversation)
        question = Question.create_from_text(
            self._client_context, "Hello",
            self._client_context.bot.sentence_splitter)
        conversation.record_dialog(question)

        context = self.match_sentence("YEMPTY", topic="*", that="*")
        self.assertIsNone(context)
예제 #28
0
    def test_get_user_incomplete(self):

        client = PropertiesAdminExtensionClient()
        client_context = client.create_client_context("testid")

        question = Question()
        conversation = client_context.bot.get_conversation(client_context)
        conversation.record_dialog(question)
        conversation.set_property("PROP1", "Value1")

        extension = PropertiesAdminExtension()
        self.assertIsNotNone(extension)

        result = extension.execute(client_context, "GET USER")
        self.assertIsNotNone(result)
        self.assertEqual("Invalid syntax for GET USER, LOCAL or GLOBAL",
                         result)
예제 #29
0
    def assert_conversation_storage(self,
                                    store,
                                    can_empty=True,
                                    test_load=True):

        if can_empty is True:
            store.empty()

        client = TestClient()
        client_context = client.create_client_context("user1")

        conversation = Conversation(client_context)

        question1 = Question.create_from_text(client_context, "Hello There")
        question1.sentence(0).response = "Hi"
        conversation.record_dialog(question1)

        store.store_conversation(client_context, conversation)

        store.commit()

        if test_load is True:
            conversation2 = Conversation(client_context)
            self.assertTrue(
                store.load_conversation(client_context, conversation2))

            self.assertEqual(1, len(conversation2.questions))
            self.assertEqual(1, len(conversation2.questions[0].sentences))
            self.assertEqual(
                "Hello There",
                conversation2.questions[0].sentences[0].text(client_context))
            self.assertEqual("Hi",
                             conversation2.questions[0].sentences[0].response)

        client2 = TestClient()
        client_context2 = client2.create_client_context("user2")

        if test_load is True:
            conversation3 = Conversation(client_context)
            self.assertFalse(
                store.load_conversation(client_context2, conversation3))

        if can_empty is True:
            store.empty()
예제 #30
0
    def test_nluslot_no_item(self):
        root = TemplateNode()
        node = TemplateNluSlotNode()
        node._slotName = TemplateWordNode("nlu_slot")
        node._itemName = TemplateWordNode("startOffset")
        root.append(node)

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        question = Question.create_from_text(
            self._client_context, "Hello",
            self._client_context.bot.sentence_splitter)
        conversation.record_dialog(question)
        nlu_result = '{"intents": [], "slots": [{"slot": "nlu_slot", "entity": "value"}]}'
        conversation.current_question().set_property("__SYSTEM_NLUDATA__",
                                                     nlu_result)

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("unknown", result)