Now that we know how to launch our program, it's time to add a touch of intelligence
getUp
Let us begin by making our program understand the meaning of each word. To do that, we
will use the getUp function. This function applied to an individual returns us all the
classes of which our individual inherits.
This function, therefore, applies the getUp method to each word found in the "words" vector and
it returns a vector for which each cell corresponds to the vector returned by getUp.
We can represent this part of the ontology as follow:
Delete the same
We will now create a function that will simply remove classes from which all words inherit.
If we look at the previous example, we can see that Alice, Bob, and Kevin all inherit active and
human classes. That is to say that the agent and human classes can not allow us to determine
the intruder and that we can therefore remove them from our reasoning.
We will not analyze this code together because it is not the center of this tutorial.
Compile this and run the program, then in another terminal re-query the programs on the words:
bob, alice, and kevin.
This time, you should have this:
[SAY]on bob alice kevin
bob is a : man
alice is a : woman
kevin is a : man
[SAY]Let's play!
Find the intruder
Now that we have removed the classes from which all individuals inherit, we are not going
to look for the individual who is the only one to inherit from a class but rather the class
of which all but one inherit.
That's exactly what the next function will do.
Let's start by defining a structure that will represent an intruder with his name and the
reason he is the intruder.
structintruder_t
{
std::stringname_;
std::stringwhy_;
};
We will finally code algorithms to find intruders. Indeed, in the function, it is sometimes
possible to find several intruders for different reasons.
Once again, we will not analyze the following algorithm because it does not use ontologenius directly.
To still be able to analyze it for yourself, here are some key elements:
We go through all the words of each heritage vector. For each of these words, we seek to
know if it exists in all other vectors except one.
The intruder_index variable represents the index of the vector in which the search word
does not exist. This variable is set to -1 as long as we have not found or found more than
one vector that does not have the word.
In the case where we found an intruder, we add it to our intruder vector, only if it does
not already exist for the same reason.
say("the intruder is " + intruder.name_ + " because it is the only one that is not: " + intruder.why_);
Compile this and run the program, then in another terminal re-query the programs on the words:
bob, alice, and kevin.
This time, you should have this:
[SAY]on bob alice kevin
[SAY]the intruder is alice because it is the only one that is not: man
[SAY]Let's play!
IT WORKS!
Now is the time to have a little fun. Test for example with the words banana, lemon, apple,
and carrot or only with the words banana, lemon, and carrot.
You should have this:
[SAY]on banana lemon apple carrot
[SAY]the intruder is carrot because it is the only one that is not: fruit
[SAY]Let's play!
[SAY]on banana lemon carrot
[SAY]the intruder is carrot because it is the only one that is not: yellow
[SAY]the intruder is carrot because it is the only one that is not: fruit
[SAY]Let's play!
Do not hesitate to look at all the available words and see how our algorithm reacts.