3.2. Ontology
How to use ontologies for matching in Natex.
Ontology
Let us create a dialogue flow to talk about animals:
transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[{dog, ape, rat}]': {
'`I love mammals!`': 'end'
},
'[{snake, lizard}]': {
'`Reptiles are slick, haha`': 'end'
},
'[{frog, salamander}]': {
'`Amphibians can be cute :)`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}For each type of animal, however, the list can be indefinitely long (e.g., there are over 5,400 mammal species). In this case, it is better to use an ontology (e.g., WordNet, FrameNet).
Let us create a JSON file, ontology_animal.json, containing an ontology of animals:
#2: the keyontologyis paired with a dictionary as a value.#3: the keyanimalrepresents the category, and its subcategories are indicated in the list.#4-6: each subcategory,mammal,reptile, andamphibian, has its own subcategory.#7: the ontology hierarchy:animal->mammal->dog.
Given the ontology, the above transitions can be rewritten as follow:
#4: matches the key "mammal" as well as its subcategories: "dog", "ape", and "rat".#5: matches the key "reptile" as well as its subcategories: "snake" and "lizard".#6: matches the key "amphibian" as well as its subcategories: "frog" and "salamander".
Unlike set matching, ontology matching handles plurals (e.g., "frogs").
Although there is no condition specified for the category dog that includes "golden retriever", there is a condition for its supercategory mammal (#4), to which it backs off.
Currently, ontology matching does not handle plurals for compound nouns (e.g., "golden retrievers"), which will be fixed in the following version.
Expression
It is possible that a category is mentioned in a non-canonical way; the above conditions do not match "puppy" because it is not introduced as a category in the ontology. In this case, we can specify the aliases as "expressions":
#10: the keyexpressionsis paired with a dictionary as a value.#4: allows matching "canine" and "puppy" for thedogcategory.
Once you load the updated JSON file, it now understands "puppy" as an expression of "dog":
It is possible to match "puppy" by adding the term as a category of "dog" (#7). However, it would not be a good practice as "puppy" should not be considered a subcategory of "dog".
Variable
Values matched by the ontology can also be stored in variables:
#4,7,10: the matched term gets stored in the variableFAVORITE_ANIMAL.#5,8,11: the system uses the value ofFAVORITE_ANIMALto generate the response.
Loading
The custom ontology must be loaded to the knowledge base of the dialogue flow before it runs:
#1: loads the ontology inontology_animal.jsonto the knowledge base ofdf.
Code Snippet
Last updated
Was this helpful?