Knowledge Graph Handler

KG handler is the main class to encapsulate the methods to access the Knowledge Graph of the chatbot.

class knowledge_graph.KGHandler(base_url, path)

KGHandler is the main class of this project. It is used to connect to the knowledge base, an retrieve information for intents.

These are the main methods of this class:

knowledge_graph.KGHandler._build_uri(self, uri, resource=True)

This method builds an URI from a complete URL or just the URI name. This method also checks if the uri is well structured.

  • uri – the URI or full URL
  • resource – If the URI is from the ontology or a resource

The namespace of the full URI

knowledge_graph.KGHandler.get_intent_requirements(self, intent)

This method returns all required entities of an intent.

Parameters:intent – The intent from where required entities will ne retrieved.
Returns:a list of the required entities.

This method is used to remove the URL part of the URI to return the clean property.

Parameters:uri – The URI to be cleaned
Returns:The clean property from the URI
knowledge_graph.KGHandler.build_answer_prop_val(self, qres, ans_prop)

This method builds a part of the answer, from the result of a SPARQL query. :param qres: The result from a SPARQL query.

Parameters:ans_prop – The property intended to an answer.
Returns:A dict containing a property and a value from the query result.
knowledge_graph.KGHandler.get_answer_properties(self, intent)

This method retrieves the answer properties from an answer class using a SPARQL query. The answer has a series of properties that may or may not be present, depending on the way the answer is configured. If there are more than one properties of an answer, the will be stored in an array.

Parameters:intent – The intent from where the answer will be retrieved
Returns:returns the different properties found in the answer object.
knowledge_graph.KGHandler.get_intent_answer(self, intent, entities_aux)

This method returns the full answer from a intent. Using and other method (get_answer_properties) to retrieve the different properties of an Answer Object. And other method (get_answer_parts) to build the key value of properties of an Entity.

  • intent – The intent from where the answer will be retrieved
  • entities_aux – A list of entities, that can be collected in different ways from other components.

The full answer in a dictionary displayed in this way: { ‘answer’: [{ ‘property’: ‘teacherName’, ‘value’: [‘Diana Lucía Espinoza Torres’, ‘Glenda Edith Ponce Espinosa’]}], ‘template’: ‘El docente encargado del curso es {%teacherName%}’ }

knowledge_graph.KGHandler.get_answer_parts(self, ans_prop, refers_to, ans_from, entity, entities)

This method searches for the real values of the answer of the intent. Depending on the different properties found in the answer object. The answer can be from a direct object, or an indirect object or, from a related object.

  • ans_prop – A property to retrieve from an object
  • refers_to – A related object to obtain a value using ans_prop
  • ans_from – An object to obtain a value using ans_prop
  • entity – A direct entity to be used in the answer
  • entities – A list of auxiliary entities

The a pair of property an value that conforms an answer.

knowledge_graph.KGHandler.get_intent_options(self, intent)

This method returns the options related to an Intent To complete the different entities needed to satisfy an intent.

Parameters:intent – An intent from where options will be retrieved.
Returns:a dict containing, the resolve question, and the different options.


handle custom options

knowledge_graph.KGHandler.get_entity_options(self, entity)

This method returns options to complete an entity.

Parameters:entity – The entity type from where options will be retrieved
Returns:returns an entity and their options.


handle custom options

knowledge_graph.KGHandler.find_agent_intents(self, agent)

This method returns information about the chatbot, like name, description and intents.

param agent:A valid agent name
knowledge_graph.KGHandler.get_resolution_question(self, intent, entity)

This method finds the resolution question of an intent and entity :param intent: A valid intent name

Parameters:entity – A valid entity name
Returns:A dict with the intent, entity and the resolution question