示例#1
0
    def process_sentence(self, sentence):
        """This processes a w_question or yes_no_question sentence"""
        self._sentence = sentence
        #StatementBuilder
        builder = StatementBuilder(self._current_speaker)
        self._statements, situation_id = builder.process_sentence(
            self._sentence)
        logger.info(
            level_marker(level=2, color="yellow") +
            "Generated statements based on the sentence components: " +
            colored_print(str(self._statements), None, "magenta"))

        #Case the question is a y_n_question : check the fact in the ontology
        if sentence.data_type == YES_NO_QUESTION:
            self._statements = self._set_situation_id(self._statements)
            logger.info(
                level_marker(level=2, color="yellow") +
                "Generated statements for this question: " +
                colored_print(str(self._statements), None, "magenta"))
            #Processing :Do you know something?
            if self.process_on_knowing_concept:
                #self._statements = [agent knows object]
                for s in self._statements:
                    self._answer = True

                    if "knows" in s:
                        [agent, object] = s.split(" knows ")

                        onto_lookup = []
                        logger.debug(
                            level_marker(level=2, color="yellow") +
                            "Looking up for " + object + " in " + agent +
                            "'s model")
                        onto_lookup = ResourcePool(
                        ).ontology_server.lookupForAgent(
                            ResourcePool().get_model_mapping(agent), object)

                        self._answer = self._answer and (True if onto_lookup
                                                         else False)

                self.process_on_knowing_concept = False
            else:
                logger.debug(
                    level_marker(level=2, color="yellow") +
                    "Checking in the ontology: " +
                    colored_print(str(self._statements), None, "magenta"))
                self._answer = ResourcePool().ontology_server.check(
                    self._statements)

        #Case the question is a w_question : find the concept the concept that answers the question
        if sentence.data_type == W_QUESTION:
            #
            self._query_on_field = self._set_query_on_field(sentence)
            statements_with_bound_tokens = self._remove_statements_with_no_unbound_tokens(
                self._statements)

            #Agent from which the ontology model is queried
            agent = self._default_agent

            self._answer = []

            #For all the possible subjects of a same question
            if not self._sentence.sn:
                statements_to_query = [
                    self._extend_statement_from_sentence_aim(
                        statements_with_bound_tokens)
                ]

            else:
                statements_to_query = [
                    self._extend_statement_from_sentence_aim(
                        statements_with_bound_tokens, sn)
                    for sn in self._sentence.sn
                ]

            for statements in statements_to_query:
                if self.process_on_knowing_concept:
                    #Get agent in a statement such as [agent knows object]
                    for s in statements:
                        if "knows" in s:
                            [agent, object] = s.split(" knows ")
                            break

                    #No need of statements such as [S knows O]
                    [statements.remove(s) for s in statements if "knows" in s]
                    self.process_on_knowing_concept = False

                if statements:
                    logger.info(
                        level_marker(level=2, color="yellow") +
                        "Generated statements for this question: " +
                        colored_print(str(self._statements), None, "magenta"))

                    answers = []
                    if self._process_on_location:

                        prepositions_list = ResourcePool(
                        ).preposition_rdf_object_property.copy()
                        roles = dict([(preposition, prepositions_list[preposition][0]) \
                                      for preposition in prepositions_list.keys() \
                                      if 'objectFoundInLocation' in prepositions_list[preposition]])

                        #Case of ojectFound in location
                        stmts = []
                        prepositions_already_used = []
                        for role in roles:
                            if roles[role] in prepositions_already_used:
                                continue

                            stmts = [
                                s.replace('objectFoundInLocation', roles[role])
                                for s in statements
                            ]

                            logger.debug(
                                level_marker(level=2, color="yellow") +
                                "Searching in " + agent + " model: " +
                                colored_print(str(stmts), None, "magenta"))
                            answers = ResourcePool(
                            ).ontology_server.findForAgent(
                                ResourcePool().get_model_mapping(agent),
                                '?concept', stmts)

                            prepositions_already_used.append(roles[role])

                            if answers:
                                ResourcePool().mark_active(answers)
                                self._answer.append([[role], answers])

                        #Case of object found in location + direction
                        stmts = [
                            s.replace('objectFoundInLocation', 'isAt')
                            for s in statements
                        ]
                        for role in ResourcePool().direction_words:
                            logger.debug(
                                level_marker(level=2, color="yellow") +
                                "Searching in " + agent + " model: " +
                                colored_print(
                                    str(stmts + [
                                        '?concept is' + role.capitalize() +
                                        'Of ?obj', '?concept rdf:type Location'
                                    ]), None, "magenta"))
                            answers = ResourcePool(
                            ).ontology_server.findForAgent(
                                ResourcePool().get_model_mapping(agent),
                                '?obj', stmts + [
                                    '?concept is' + role.capitalize() +
                                    'Of ?obj', '?concept rdf:type Location'
                                ])

                            if answers:
                                ResourcePool().mark_active(answers)
                                self._answer.append([[role], answers])

                    else:
                        logger.debug(
                            level_marker(level=2, color="yellow") +
                            "Searching in " + agent + " model: " +
                            colored_print(str(statements), None, "magenta"))
                        answers = ResourcePool().ontology_server.findForAgent(
                            ResourcePool().get_model_mapping(agent),
                            '?concept', statements)

                        if answers:
                            ResourcePool().mark_active(answers)
                            self._answer.append([[], answers])
                else:
                    pass

                self._statements.extend(statements)

        stmts, sit_id = self._build_situation_statements()
        return self._answer, stmts, sit_id
示例#2
0
    def process_sentence(self, sentence):
        """This processes a w_question or yes_no_question sentence"""
        self._sentence = sentence
        #StatementBuilder
        builder = StatementBuilder(self._current_speaker)
        self._statements, situation_id = builder.process_sentence(self._sentence)
        logger.info(level_marker(level=2,
                                 color="yellow") + "Generated statements based on the sentence components: " + colored_print(
            str(self._statements), None, "magenta"))

        #Case the question is a y_n_question : check the fact in the ontology
        if sentence.data_type == YES_NO_QUESTION:
            self._statements = self._set_situation_id(self._statements)
            logger.info(
                level_marker(level=2, color="yellow") + "Generated statements for this question: " + colored_print(
                    str(self._statements), None, "magenta"))
            #Processing :Do you know something?
            if self.process_on_knowing_concept:
                #self._statements = [agent knows object]
                for s in self._statements:
                    self._answer = True

                    if "knows" in s:
                        [agent, object] = s.split(" knows ")

                        onto_lookup = []
                        try:
                            logger.debug(level_marker(level=2,
                                                      color="yellow") + "Looking up for " + object + " in " + agent + "'s model")
                            onto_lookup = ResourcePool().ontology_server.lookupForAgent(agent, object)
                        except AttributeError:
                            pass

                        self._answer = self._answer and (True if onto_lookup else False)

                self.process_on_knowing_concept = False
            else:
                try:
                    logger.debug(level_marker(level=2, color="yellow") + "Checking in the ontology: " + colored_print(
                        str(self._statements), None, "magenta"))
                    self._answer = ResourcePool().ontology_server.check(self._statements)
                except AttributeError: #the ontology server is not started of doesn't know the method
                    pass

        #Case the question is a w_question : find the concept the concept that answers the question
        if sentence.data_type == W_QUESTION:
            #
            self._query_on_field = self._set_query_on_field(sentence)
            statements_with_bound_tokens = self._remove_statements_with_no_unbound_tokens(self._statements)

            #Agent from which the ontology model is queried
            agent = self._default_agent

            self._answer = []

            #For all the possible subjects of a same question
            if not self._sentence.sn:
                statements_to_query = [self._extend_statement_from_sentence_aim(statements_with_bound_tokens)]

            else:
                statements_to_query = [self._extend_statement_from_sentence_aim(statements_with_bound_tokens, sn) for sn
                                       in self._sentence.sn]

            for statements in statements_to_query:
                if self.process_on_knowing_concept:
                    #Get agent in a statement such as [agent knows object]
                    for s in statements:
                        if "knows" in s:
                            [agent, object] = s.split(" knows ")
                            break

                    #No need of statements such as [S knows O]
                    [statements.remove(s) for s in statements if "knows" in s]
                    self.process_on_knowing_concept = False

                if statements:
                    logger.info(level_marker(level=2,
                                             color="yellow") + "Generated statements for this question: " + colored_print(
                        str(self._statements), None, "magenta"))

                    answers = []
                    if self._process_on_location:

                        prepositions_list = ResourcePool().preposition_rdf_object_property.copy()
                        roles = dict([(preposition, prepositions_list[preposition][0]) \
                                      for preposition in prepositions_list.keys() \
                                      if 'objectFoundInLocation' in prepositions_list[preposition]])

                        #Case of ojectFound in location
                        stmts = []
                        prepositions_already_used = []
                        for role in roles:
                            if roles[role] in prepositions_already_used:
                                continue

                            stmts = [s.replace('objectFoundInLocation', roles[role]) for s in statements]
                            try:
                                logger.debug(level_marker(level=2,
                                                          color="yellow") + "Searching in " + agent + " model: " + colored_print(
                                    str(stmts), None, "magenta"))
                                answers = ResourcePool().ontology_server.findForAgent(agent, '?concept', stmts)
                            except AttributeError: #the ontology server is not started of doesn't know the method
                                pass

                            prepositions_already_used.append(roles[role])

                            if answers:
                                ResourcePool().mark_active(answers)
                                self._answer.append([[role], answers])

                        #Case of object found in location + direction
                        stmts = [s.replace('objectFoundInLocation', 'isAt') for s in statements]
                        for role in ResourcePool().direction_words:
                            try:
                                logger.debug(level_marker(level=2,
                                                          color="yellow") + "Searching in " + agent + " model: " + colored_print(
                                    str(stmts + ['?concept is' + role.capitalize() + 'Of ?obj',
                                                 '?concept rdf:type Location']), None, "magenta"))
                                answers = ResourcePool().ontology_server.findForAgent(agent, '?obj', stmts + [
                                    '?concept is' + role.capitalize() + 'Of ?obj', '?concept rdf:type Location'])
                            except AttributeError: #the ontology server is not started of doesn't know the method
                                pass

                            if answers:
                                ResourcePool().mark_active(answers)
                                self._answer.append([[role], answers])

                    else:
                        try:
                            logger.debug(level_marker(level=2,
                                                      color="yellow") + "Searching in " + agent + " model: " + colored_print(
                                str(statements), None, "magenta"))
                            answers = ResourcePool().ontology_server.findForAgent(agent, '?concept', statements)
                        except AttributeError: #the ontology server is not started of doesn't know the method
                            pass

                        if answers:
                            ResourcePool().mark_active(answers)
                            self._answer.append([[], answers])
                else:
                    pass

                self._statements.extend(statements)

        stmts, sit_id = self._build_situation_statements()
        return self._answer, stmts, sit_id
示例#3
0
    def __init__(self):
        """
        This class consits in building a map of object property regarding the 
        Question's aim attribute and the query_on_field parameter.
        Below is a description of what should be generated depending of the question that is processed.
               
        'What and which-question': 
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ'
                extend statements with [?event OBJ ?concept]
                where OBJ depends on the verb. 
                e.g: if verb == "get":
                        OBJ = "actsOnObject"
                     if verb == "drive":
                        OBJ = "involves"
                    ...                 
                                
            Case # query_on_field = None
                Nothing to extend
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                Nothing to extend

        'Who-question':
            Case # query_on_field = None
                extend statements with [* performedBy ?concept] 
            
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ' and state verbs
                extend statements with [* rdfs:label ?concept]
                E.g: Who is the car's driver?
                
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ' and action verbs
                same as "what" and "which" question for 'QUERY_ON_DIRECT_OBJ'
            
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                extend statements with [* receivedBy ?concept]       
        'Where-question':
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                extend statements with [* objectFoundInLocation ?concept]  
        """

        self.dic_aim = {}

        #Dictionaries related to thematice roles verbs
        verbs_dic = ResourcePool().thematic_roles.verbs
        self.dic_on_direct_obj = dict([(verb, role.id)
                                       for verb in verbs_dic.keys()
                                       for role in verbs_dic[verb].roles[0:1]])
        self.dic_on_direct_obj[None] = 'involves'
        self.dic_on_direct_obj['be'] = 'owl:sameAs'

        self.dic_on_indirect_obj = dict([(verb, role.id)
                                         for verb in verbs_dic.keys()
                                         for role in verbs_dic[verb].roles[1:2]
                                         ])
        self.dic_on_indirect_obj['be'] = None
        self.dic_on_indirect_obj[None] = 'owl:sameAs'

        #Dictionary for sentence.aim = 'place'
        self.dic_place = {None: self.dic_on_indirect_obj.copy()}
        self.dic_place[None]['be'] = 'objectFoundInLocation'

        #Dictionary for sentence.aim = 'thing'
        self.dic_thing = {None: self.dic_on_direct_obj.copy()}

        #Dictionary for sentence.aim = 'manner'
        self.dic_manner = {None: {'be': 'owl:topObjectProperty'}}

        #Dictionary for sentence.aim = 'people'
        self.dic_people = {
            'QUERY_ON_DIRECT_OBJ': self.dic_on_direct_obj.copy(),
            'QUERY_ON_INDIRECT_OBJ': self.dic_on_indirect_obj.copy()
        }

        #Dictionary for all question aims
        #What-question
        self.dic_aim['thing'] = self.dic_thing
        #Who-question
        self.dic_aim['people'] = self.dic_people
        #Where-question
        self.dic_aim['place'] = self.dic_place
        #How-Question
        self.dic_aim['manner'] = self.dic_manner
        #which-question
        self.dic_aim['choice'] = self.dic_thing
示例#4
0
    def __init__(self):
        """
        This class consits in building a map of object property regarding the 
        Question's aim attribute and the query_on_field parameter.
        Below is a description of what should be generated depending of the question that is processed.
               
        'What and which-question': 
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ'
                extend statements with [?event OBJ ?concept]
                where OBJ depends on the verb. 
                e.g: if verb == "get":
                        OBJ = "actsOnObject"
                     if verb == "drive":
                        OBJ = "involves"
                    ...                 
                                
            Case # query_on_field = None
                Nothing to extend
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                Nothing to extend

        'Who-question':
            Case # query_on_field = None
                extend statements with [* performedBy ?concept] 
            
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ' and state verbs
                extend statements with [* rdfs:label ?concept]
                E.g: Who is the car's driver?
                
            Case # query_on_field = 'QUERY_ON_DIRECT_OBJ' and action verbs
                same as "what" and "which" question for 'QUERY_ON_DIRECT_OBJ'
            
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                extend statements with [* receivedBy ?concept]       
        'Where-question':
            Case # query_on_field = 'QUERY_ON_INDIRECT_OBJ'
                extend statements with [* objectFoundInLocation ?concept]  
        """

        self.dic_aim = {}


        #Dictionaries related to thematice roles verbs
        verbs_dic = ResourcePool().thematic_roles.verbs
        self.dic_on_direct_obj = dict(
            [(verb, role.id) for verb in verbs_dic.keys() for role in verbs_dic[verb].roles[0:1]])
        self.dic_on_direct_obj[None] = 'involves'
        self.dic_on_direct_obj['be'] = 'owl:sameAs'

        self.dic_on_indirect_obj = dict(
            [(verb, role.id) for verb in verbs_dic.keys() for role in verbs_dic[verb].roles[1:2]])
        self.dic_on_indirect_obj['be'] = None
        self.dic_on_indirect_obj[None] = 'owl:sameAs'

        #Dictionary for sentence.aim = 'place' 
        self.dic_place = {None: self.dic_on_indirect_obj.copy()}
        self.dic_place[None]['be'] = 'objectFoundInLocation'

        #Dictionary for sentence.aim = 'thing' 
        self.dic_thing = {None: self.dic_on_direct_obj.copy()}

        #Dictionary for sentence.aim = 'manner' 
        self.dic_manner = {None: {'be': 'owl:topObjectProperty'}}

        #Dictionary for sentence.aim = 'people'
        self.dic_people = {'QUERY_ON_DIRECT_OBJ': self.dic_on_direct_obj.copy(),
                           'QUERY_ON_INDIRECT_OBJ': self.dic_on_indirect_obj.copy()}

        #Dictionary for all question aims        
        #What-question
        self.dic_aim['thing'] = self.dic_thing
        #Who-question
        self.dic_aim['people'] = self.dic_people
        #Where-question
        self.dic_aim['place'] = self.dic_place
        #How-Question
        self.dic_aim['manner'] = self.dic_manner
        #which-question
        self.dic_aim['choice'] = self.dic_thing