def _send_state(state): models = [ResourcePool().default_model] policy = {"method": "add", "models": models} state_id = generate_id(False) statements = [ state_id + " rdf:type " + state, "myself experiences " + state_id ] logger.info(colored_print("Setting my mood to " + state, "magenta")) #logger.warning(colored_print("Not setting my mood due to Pellet bugs!", "magenta")) ResourcePool().ontology_server.revise(statements, policy)
def _send_state(state): models = [ResourcePool().default_model] policy = {"method": "add", "models": models} state_id = generate_id(False) statements = [state_id + " rdf:type " + state, "myself experiences " + state_id] try: logger.info(colored_print("Setting my mood to " + state, "magenta")) #logger.warning(colored_print("Not setting my mood due to Pellet bugs!", "magenta")) ResourcePool().ontology_server.revise(statements, policy) except AttributeError: #the ontology server is not started of doesn't know the method pass
def process_adjectives(self, nominal_group, ng_id, negative_object): """For any adjectives, we add it in the ontology with the objectProperty 'hasFeature' except if a specific category has been specified in the adjectives list. """ for adj in nominal_group.adj: # Case of 'other' # E.g: the other cube: if adj[0].lower() == "other": self.process_on_other = True pass #self._statements.append(ng_id + " owl:differentFrom " + nominal_group.id) elif adj[0].lower() == "same": pass #TODO: case of class Feature # Apple are yellow fruits # Case of features else: #Getting the object property if there exists a specific class object_property = '' try: object_property = " has" + ResourcePool().adjectives[adj[0]] + " " #Default case, creating hasFeature object Property except KeyError: object_property = " hasFeature " #Case negative assertion if negative_object: negative_adj = generate_id(with_question_mark=not nominal_group._resolved) self._statements.append(ng_id + object_property + negative_adj) self._statements.append(negative_adj + ' owl:differentFrom ' + adj[0]) #Case Affirmative assertion else: if isinstance(ng_id, basestring): ng_id = [ng_id] self._statements += [id + object_property + adj[0] for id in ng_id]
def set_nominal_group_id(self, ng): if ng.id: return ng.id onto = ResourcePool().ontology_server.lookupForAgent( ResourcePool().get_model_mapping(self._current_speaker), ng.noun[0]) if onto: for c in onto: if "instance" in c: return c[0] if ng.noun[0].lower() in ['i', 'me']: return self._current_speaker if ng.noun[0].lower() == 'you': return 'myself' id = generate_id() self._unclarified_ids.append(id) return id
def set_nominal_group_id(self, ng): if ng.id: return ng.id onto = '' try: onto = ResourcePool().ontology_server.lookupForAgent(self._current_speaker, ng.noun[0]) except AttributeError: #the ontology server is not started of doesn't know the method pass if onto: for c in onto: if "INSTANCE" in c: return c[0] if ng.noun[0].lower() in ['i', 'me']: return self._current_speaker if ng.noun[0].lower() == 'you': return 'myself' id = generate_id() self._unclarified_ids.append(id) return id
def process_sentence_adverb(self, verbal_group): id = generate_id(with_question_mark=False) for a in verbal_group.advrb: if a in ResourcePool().location_adverbs: self._statements += ["%s hasGoal %s" % (self.situation_id, id), "%s rdf:type %sZone" % (id, a.capitalize())]
def process_indirect_complement(self, indirect_cmpls, verb, sit_id): for ic in indirect_cmpls: # Case 1: if there is no preposition, the indirect complement is obviously an indirect object. # Therefore, it receives the action # e.g. I gave you a ball also means I gave a ball 'to' you # see http://www.englishlanguageguide.com/english/grammar/indirect-object.asp #Case 2: if there is a preposition, the indirect complement is either an indirect object or an adverbial # if the main verb is specified as a thematic role, we extract the matching object_property to the preposition. # e.g I moved the bottle 'to' the table. The object_property takes the value "hasGoal" # # if the preposition is 'to' , by default we assume the indirect complement is an indirect object # e.g. I gave a ball to Jido. The object_property takes the value "receivedBy" # otherwise, the default processing is to create an object_property by concatenating is and the preposition # e.g. I bought a ball for Jido. The object_property takes the value "isFor" i_stmt_builder = NominalGroupStatementBuilder(ic.gn, self._current_speaker) for ic_noun in ic.gn: #Indirect object ID if ic_noun.id: ic_noun_id = ic_noun.id else: ic_noun_id = i_stmt_builder.set_nominal_group_id(ic_noun) #Proposition role icmpl_role = None icmpl_qualification = None # Case of no preposition if not ic.prep: icmpl_role = " receivedBy " # Case of a preposition. Attempt to get from thematic roles else: icmpl_role = ResourcePool().thematic_roles.get_cmplt_role_for_preposition(verb, ic.prep[0], True) # If no thematic role exist AND the verb is not a state # verb, use the generic role 'involves' and try to qualify it properly. # E.g: 'move the ball next to the table' -> # action type Move, ..., action involves id1, id1 isNextTo table if not icmpl_role: try: if verb not in ResourcePool().state: icmpl_qualification = ResourcePool().preposition_rdf_object_property[ic.prep[0]][0] else: icmpl_role = " %s " % ResourcePool().preposition_rdf_object_property[ic.prep[0]][0] except IndexError: if ic.prep: icmpl_qualification = "is" + ic.prep[0].capitalize() #Creating statements # Case of negation if self._process_on_negative: negative_ic_noun_id = generate_id(with_question_mark=False) self._statements.append(sit_id + icmpl_role + negative_ic_noun_id) self._statements.append(negative_ic_noun_id + ' owl:differentFrom ' + ic_noun_id) # Case of affirmation else: if icmpl_qualification: qualification_id = generate_id(with_question_mark=False) self._statements += \ ["%s involves %s" % (sit_id, qualification_id), "%s %s %s" % (qualification_id, icmpl_qualification, ic_noun_id)] else: self._statements.append(sit_id + icmpl_role + ic_noun_id) i_stmt_builder.process_nominal_group(ic_noun, ic_noun_id, None, False) self._statements.extend(i_stmt_builder._statements) self._unclarified_ids.extend(i_stmt_builder._unclarified_ids) self.lear_more_concept.extend(i_stmt_builder.lear_more_concept)
def process_verb(self, verbal_group, subject_id, subject_quantifier, second_verb_sit_id): for verb in verbal_group.vrb_main: # Case 1: the state verb 'to be'/ to become""" #Case 2: actions or stative verbs with a specified 'goal' or 'thematic' role: # see '../../share/dialog/thematic_roles' #Case 3: actions verbs with 'passive behaviour' like 'see' #Case 4: special case for 'know' #Case 5: other action or stative verbs # Modal or phrasal verbs. E.g: can+do, look+for , ... # verb = must+do modal = '' if '+' in verb: # Case of Modals [modal, verb] = verb.split('+') if modal in ResourcePool().modal: pass #self._statements.append(sit_id + " rdf:type " + verb.capitalize()) #self._statements.append(subject_id+ " " + modal +"Performs " + sit_id) #Case 1: if verb in ResourcePool().state: if isinstance(subject_id, basestring): subjects_id = [subject_id] else: subjects_id = subject_id agentslist = ResourcePool().ontology_server.listAgents() if agentslist is None: agentslist = [ResourcePool().default_model, self._current_speaker] for subject_id in subjects_id: if subject_id in agentslist: sit_id = generate_id(with_question_mark=False) self._statements.append(subject_id + " experiences " + sit_id) else: #TODO: Will keep only the last one is several ids sit_id = subject_id else: # First, create a situation ID that represent the # semantic situation carried by the verbal phrase #Case : the verbal group that is being processed is the second verbs . i.e : it is held in the field sentence.sv.sv_sec if second_verb_sit_id: sit_id = second_verb_sit_id # Case of question elif self._process_on_question: sit_id = '?event' #case of negation : Creating a fake ID that is to find in the ontology for later removal # Setting up the field process_statement_to_remove to True elif self._process_on_negative: sit_id = generate_id(with_question_mark=True) self._unclarified_ids.append(sit_id) self.process_statements_to_remove = True # General case: we generate an ID else: sit_id = generate_id(with_question_mark=not self._process_on_resolved_sentence) # Then, process the type of verb #Case 2: if verb in ResourcePool().goal_verbs: self._statements.append(subject_id + " desires " + sit_id) if verbal_group.sv_sec: self.process_vrb_sec(verbal_group, subject_id, subject_quantifier, sit_id) #Case 3: action verbs wit passive behaviour elif verb.lower() in ResourcePool().action_verb_with_passive_behaviour.keys(): sit_id = subject_id # Case 4: 'know' elif verb.lower() == 'know': pass #Case 5: other verbs -> reification else: self._statements.append(sit_id + " rdf:type " + verb.capitalize()) self._statements.append(sit_id + " performedBy " + subject_id) if not self._process_on_question and \ not self._process_on_negative \ and self._process_on_resolved_sentence: # If I'm not processing a question, add a label # to this action self._statements.append(sit_id + " rdfs:label \"" + \ verb.capitalize() + " action #" + sit_id + "\"") # Store the situation id self.situation_id = sit_id #Imperative specification, add the goal verb 'desire' if self._process_on_imperative: self._statements.append(self._current_speaker + " desires " + sit_id) #Direct object if verbal_group.d_obj: self.process_direct_object(verbal_group.d_obj, verb, sit_id, subject_quantifier) #Indirect Complement if verbal_group.i_cmpl: self.process_indirect_complement(verbal_group.i_cmpl, verb, sit_id) # Adverbs modifiying the manner of an action verb if verbal_group.vrb_adv: self.process_action_verb_adverb(verbal_group.vrb_adv, verb, sit_id) #verb tense if verbal_group.vrb_tense: self.process_verb_tense(verbal_group, verb, sit_id)
def clarify(self, description, ignoreFeatureL=None): if not ignoreFeatureL: ignoreFeatureL = [] objL = self.get_all_objects_with_desc(description) if len(objL) == 0: logger.debug(colored_print('Nothing found!', "magenta")) else: logger.debug( colored_print('Found these possible concepts ID: ', "magenta") + colored_print(str(objL), 'blue')) if not self.oro: #No ontology server return 'UNKNOWN_CONCEPT_' + generate_id(with_question_mark=False) if not objL: questions = SentenceFactory().create_i_dont_understand() raise UnsufficientInputError({ 'status': 'FAILURE', 'question': questions }) #return "I don't understand" else: # Check if the speaker sees only some of the object. # If he sees none of them, discriminate on the whole set. # Else, discriminate only on visible objects. agent = description[0][0] logger.debug("Checking which of these objects are visible for " + agent) visible_objects = self.visible_subset(agent, objL) if visible_objects: objL = visible_objects logger.debug( colored_print('Only ', "magenta") + colored_print(str(objL), 'blue') + colored_print(" are visible by " + agent, "magenta")) else: logger.debug( colored_print('None are visible by ' + agent, "magenta")) if len(objL) == 1: return objL[0] if len(objL) == 2 and self.oro.check( ['%s owl:sameAs %s' % (objL[0], objL[1])]): return objL[0] agent, descriptor = self.get_descriptor(description, ignoreFeatureL) object = self.get_type_description(description) if descriptor: sentence_builder = SentenceFactory() question = None values = self.get_values_for_descriptor( agent, descriptor, objL) if not object: object = 'object' if descriptor == 'hasColor' or descriptor == 'mainColorOfObject': questions = sentence_builder.create_w_question_choice( object, 'color', values) elif descriptor == 'hasShape': questions = sentence_builder.create_w_question_choice( object, 'shape', values) elif descriptor == 'hasSize': questions = sentence_builder.create_w_question_choice( object, 'size', values) elif descriptor == 'isOn': questions = sentence_builder.create_w_question_location( object, 'on', values) elif descriptor == 'isIn': questions = sentence_builder.create_w_question_location( object, 'in', values) elif descriptor == 'isNextTo': questions = sentence_builder.create_w_question_location( object, 'next to', values) elif descriptor == 'isAt': questions = sentence_builder.create_w_question_location( object, 'at', values) elif descriptor == 'isLocated': questions = sentence_builder.create_w_question_location_PT( values, agent) elif descriptor == 'rdf:type': questions = sentence_builder.create_w_question_choice( object, 'type', values) else: questions = sentence_builder.create_w_question_generic_descriptor( object, descriptor, values) raise UnsufficientInputError({ 'status': 'SUCCESS', 'question': questions }) #return questions else: questions = [ Sentence(IMPERATIVE, '', [], [ VerbalGroup(['give'], [], 'present simple', [ NominalGroup([], ['information'], [['more', []]], [], []) ], [ IndirectComplement( [], [NominalGroup([], ['me'], [], [], [])]), IndirectComplement( ['about'], [NominalGroup(['the'], [object], [], [], [])]) ], [], [], VerbalGroup.affirmative, []) ]) ] raise UnsufficientInputError({ 'status': 'SUCCESS', 'question': questions })
def clarify(self, description, ignoreFeatureL=None): if not ignoreFeatureL: ignoreFeatureL = [] objL = self.get_all_objects_with_desc(description) if len(objL) == 0: logger.debug(colored_print('Nothing found!', "magenta")) else: logger.debug( colored_print('Found these possible concepts ID: ', "magenta") + colored_print(str(objL), 'blue')) if not self.oro: #No ontology server return 'UNKNOWN_CONCEPT_' + generate_id(with_question_mark=False) if not objL: questions = SentenceFactory().create_i_dont_understand() raise UnsufficientInputError({'status': 'FAILURE', 'question': questions}) #return "I don't understand" else: # Check if the speaker sees only some of the object. # If he sees none of them, discriminate on the whole set. # Else, discriminate only on visible objects. agent = description[0][0] logger.debug("Checking which of these objects are visible for " + agent) visible_objects = self.visible_subset(agent, objL) if visible_objects: objL = visible_objects logger.debug(colored_print('Only ', "magenta") + colored_print(str(objL), 'blue') + colored_print(" are visible by " + agent, "magenta")) else: logger.debug(colored_print('None are visible by ' + agent, "magenta")) if len(objL) == 1: return objL[0] if len(objL) == 2 and self.oro.check(['%s owl:sameAs %s' % (objL[0], objL[1])]): return objL[0] agent, descriptor = self.get_descriptor(description, ignoreFeatureL) object = self.get_type_description(description) if descriptor: sentence_builder = SentenceFactory() question = None values = self.get_values_for_descriptor(agent, descriptor, objL) if not object: object = 'object' if descriptor == 'hasColor' or descriptor == 'mainColorOfObject': questions = sentence_builder.create_w_question_choice(object, 'color', values) elif descriptor == 'hasShape': questions = sentence_builder.create_w_question_choice(object, 'shape', values) elif descriptor == 'hasSize': questions = sentence_builder.create_w_question_choice(object, 'size', values) elif descriptor == 'isOn': questions = sentence_builder.create_w_question_location(object, 'on', values) elif descriptor == 'isIn': questions = sentence_builder.create_w_question_location(object, 'in', values) elif descriptor == 'isNextTo': questions = sentence_builder.create_w_question_location(object, 'next to', values) elif descriptor == 'isAt': questions = sentence_builder.create_w_question_location(object, 'at', values) elif descriptor == 'isLocated': questions = sentence_builder.create_w_question_location_PT(values, agent) elif descriptor == 'rdf:type': questions = sentence_builder.create_w_question_choice(object, 'type', values) else: questions = sentence_builder.create_w_question_generic_descriptor(object, descriptor, values) raise UnsufficientInputError({'status': 'SUCCESS', 'question': questions}) #return questions else: questions = [Sentence(IMPERATIVE, '', [], [Verbal_Group(['give'], [], 'present simple', [Nominal_Group([], ['information'], [['more', []]], [], [])], [Indirect_Complement([], [Nominal_Group([], ['me'], [], [], [])]), Indirect_Complement(['about'], [ Nominal_Group(['the'], [object], [], [], [])])], [], [], Verbal_Group.affirmative, [])])] raise UnsufficientInputError({'status': 'SUCCESS', 'question': questions})