Feed and generalize (cpp)

Do the birds fly ?

Insert new knowledge

Before starting, here are some includes, an ontology manipulator pointer, a small function that removes the s of the plural of a word when it is present, and especially some regular expressions that I will explain to you.

#include <iostream>
#include <regex>
#include <string>
#include <algorithm>

#include "ros/ros.h"
#include "std_msgs/String.h"

#include "ontologenius/OntologyManipulator.h"

onto::OntologyManipulator* onto_;

std::regex is_a("(a\\s|the\\s)?(\\w+)\\s(is\\sa|is\\san|are)\\s(\\w+)");
std::regex property("(a\\s)?(\\w+)\\scan\\s(not\\s)?(\\w+)");

std::regex question("can\\s(a\\s)?(\\w+)\\s(\\w+)\\s?\\?");
std::regex what_is("what\\s(is|are)\\s(a\\s)?(\\w+)\\s?\\?");
std::regex is_it_a("(is\\sa|are)\\s(\\w+)\\s(is\\sa\\s|is\\san\\s|a\\s|an\\s)?(\\w+)\\s?\\?");

std::string singular(std::string word)
{
if(word.back() == 's')
word = word.substr(0, word.size() - 1);

return word;
}

We have described here five regexes that correspond to the five types of sentences that our chatbot will be able to understand. I'm not going to give you a lesson, but remember that an expression in parentheses is what we call a match. When evaluating a regex on a string of characters, if the string matches the regex, we will be able to recover each of these matches. Whenever you see (\\w+) it means that at this end you want to retrieve the set of characters until the next space: so a word.

For example, the regex is_a accepts all the sentences of the type "bob is a man", "a chicken is a bird" or "the birds are animals". This regex has four matches and the ones we are interested in are the second and fourth. In the previous examples, the second match should be 'bob', 'chicken' or 'birds' and the fourth 'man', 'bird' or 'animals'.

  • The property regexp will accept sentences of type: "a bird can fly" and "a bird can not swim"
  • The question regexp will accept sentences of type: "can a bird fly ?"
  • The what_is regexp will accept sentences of type: "what are kiwis ?" and "what is a bird ?"
  • The is_it_a regexp will accept sentences of type: "is a kiwi a bird ?" and "are birds animals ?"

We will now write the callback function of a ROS topic that will determine the type of sentence and perform the actions corresponding to each type of sentence.

void inputCallback(const std_msgs::String::ConstPtr& msg)
{
std::cout << msg->data << std::endl;
std::string response = "";

std::smatch match;
if(std::regex_match(msg->data, match, is_a))
onto_->feeder.addInheritage( singular(match[2].str()), singular(match[4].str()) );
else if(std::regex_match(msg->data, match, property))
{
if(match[3].str() == "")
{
onto_->feeder.removeProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "false");
onto_->feeder.addProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "true");
}
else
{
onto_->feeder.removeProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "true");
onto_->feeder.addProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "false");
}
}
else if(std::regex_match(msg->data, match, question))
{
std::vector<std::string> res = onto_->individuals.getOn(singular(match[2].str()), "can_" + match[3].str());
if(res.size() == 0)
res = onto_->classes.getOn(singular(match[2].str()), "can_" + match[3].str());

if(res.size())
response = (res[0] == "bool#true") ? "yes" : "no";
else
response = "I do not know";
}
else if(std::regex_match(msg->data, match, what_is))
{
std::vector<std::string> res = onto_->individuals.getUp( singular(match[3].str()), 1);
if(res.size() == 0)
res = onto_->classes.getUp( singular(match[3].str()), 1);

if(res.size() == 0)
response = "I do not know";
else
{
response = "it is a : ";
for(auto& word : res)
response += word + " ";
}
}
else if(std::regex_match(msg->data, match, is_it_a))
{
std::vector<std::string> res = onto_->individuals.getUp( singular(match[2].str()) );
if(res.size() == 0)
res = onto_->classes.getUp( singular(match[2].str()) );
if(std::find(res.begin(), res.end(), singular(match[4].str()) ) != res.end())
response = "yes";
else
response = "no";
}

if(response != "")
std::cout << "==> " << response << std::endl;
}

Let's break down the code:

if(std::regex_match(msg->data, match, is_a))

We test here if the sentence sent to the topic corresponds to the regexp is_a. If so, then the different matches will be put in the match variable. We will do the same for all the regexp.

onto_->feeder.addInheritage( singular(match[2].str()), singular(match[4].str()) );

If the sentence matches a heritage relationship, we add the inheritance relationship via the member feeder of the ontology manipulator.

Note that we do not directly pass the matches in the parameters but their singular version.

if(match[3].str() == "")
{
onto_->feeder.removeProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "false");
onto_->feeder.addProperty( singular(match[2].str()), "can_"+singular(match[4].str()) , "bool", "true");
}

The third match must contain "not" if the sentence is in its negative form and nothing if it is in its positive form.

If the sentence is in its positive form, we remove the data property "can_verb" with the boolean value false and add it with the value true. Although it seems strange, Ontologenius allows an individual to own the same property with different values. To be sure that this does not happen here because it does not make sense with booleans, we remove the value false before adding true. This is no problem removing a property that does not exist on an individual, it will have no effect. However, if we add to it the knowledge that a bird can not swim and that later we tell him who finally a bird can swim, we will be sure that the first knowledge will be removed.

We do the same for the negative form by removing the value true and adding the value false.

Note, however, that we are applying a data property of the form "can_verb" to an individual or class without ever having defined this property before. In fact, if Ontologenius does not find the said property it will create it alone by determining its type with respect to the data pointed. For all times after, the property will exist and will be reused.

std::vector<std::string> res = onto_->individuals.getOn(singular(match[2].str()), "can_" + match[3].str());
if(res.size() == 0)
res = onto_->classes.getOn(singular(match[2].str()), "can_" + match[3].str());

if(res.size())
response = (res[0] == "bool#true") ? "yes" : "no";
else
response = "I do not know";

We are trying here to answer a question such as "can a kiwi fly?" We want to know if the concept "kiwi" has the property "can_fly" with the value true or false. To get this information, we can use the getOn function seen in the previous tutorial. However, we do not know if the concept we are working on is of individual type or class type. The simplest way is to look at whether we have had an answer on the individuals and to test on the classes otherwise.

If we got an answer, it will be in the form "type#value".

std::vector<std::string> res = onto_->individuals.getUp( singular(match[3].str()), 1);
if(res.size() == 0)
res = onto_->classes.getUp( singular(match[3].str()), 1);

Here we try to answer a question such as "what is a kiwi?" You should directly think about using the getUp! However, this function will give us all the inheritance trees, which is not relevant to answer this question. This is why we add the second parameter to this function, defined here as 1. This value corresponds to the depth of exploration of the inheritance tree. By setting this parameter to 1, we ask to have only direct inheritances.

std::vector<std::string> res = onto_->individuals.getUp( singular(match[2].str()));
if(res.size() == 0)
res = onto_->classes.getUp( singular(match[2].str()) );
if(std::find(res.begin(), res.end(), singular(match[4].str()) ) != res.end())

Finally, we finish on a question of the type "is a kiwi is an animal?". To answer this, nothing more simple, we will recover all the heritage tree and we look if the concept of inheritance suposse is present in.

Finally, we can write our main function which creates the ontology manipulator, close the ontology, and subscribes to the topic "feed_and_generalize/in".

int main(int argc, char** argv)
{
ros::init(argc, argv, "feed_and_generalize");

ros::NodeHandle n;

onto::OntologyManipulator onto;
onto_ = onto;

onto.close();

sleep(1);

onto.feeder.addConcept("bird");

ros::Subscriber sub = n.subscribe("feed_and_generalize/in", 1000, inputCallback);

ros::spin();

return 0;
}

Before moving on, note that we add the concept "bird" before all. This is because to build an ontology, Ontologenius must always know at least one concept. For example, if we want to add the fact that a bird is an animal, Ontologenius must know either bird or animal and will automatically create the second if it does not exist yet.

onto.feeder.addConcept("bird");

Let's test!

Now that everything is ready, let's launch our program and in another terminal explain to it that a kiwi is a bird and that it can not fly:

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a kiwi is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a kiwi can not fly'"

We can already ask it some questions to evaluate what it knows thanks to this new information:

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'what is a kiwi?'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a kiwi fly ?'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'is a kiwi an animal?'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a bird fly ?'"

It should answer you (in the terminal where the program is launched) that a kiwi is a bird and it can not fly. You should also have that a kiwi is not an animal (since it does not even know what an animal is) and that it does not know if a bird can fly (since nothing has been said about it).

We can explain to it that a bird is an animal and ask it again if a kiwi is an animal:

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'birds are animals'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'is a kiwi an animal?'"

It is obvious but now our program responds to us that a kiwi is an animal as a kiwi is a bird and a bird is an animal.

So let's learn the concept of a penguin who is a bird that can not fly and then ask it again if a bird can fly:

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a penguin is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a penguin can not fly'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a bird fly ?'"

Surprisingly, our program now responds to the fact that birds can not fly, which is wrong, but above all, that we have never told it!

This is in fact due to the generalization mechanism. Since all the birds it knows can not fly, it has deduced by itself that all birds can not fly. To excuse this mistake, it must be said that we did not help it.

The basic principle is that Ontologenius realizes a generalization if 60% of the individuals inheriting from the same classes have the same property and that from two individuals.

Trying to remove this property with the call of the removeDataProperty function would have no effect since it would be deduced again. To make this property accurate, it should be explicitly told that birds can fly but we will not do that.

Before continuing, kill Ontologenius and so your program then restarts it and ask it again if the birds can fly.

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a bird fly ?'"

It should answer you again no. Yet you have stopped Ontologenius... This is where we understand its long-term memory principle as well as the usefulness of the internal file. When stopping Ontologenius, it stored all its knowledge in the internal file that is reloaded at the restart.

We will now teach it the principle of a parrot and a dove that are birds capable of flying and ask it again if the birds can fly.

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a parrot is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a parrot can fly'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a dove is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a dove can fly'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a bird fly ?'"

You had to suspect that the 60% condition is no longer true and our program finds itself in the inability to know whether birds can fly or not.

Let's learn five new birds that can all fly to reach the threshold of 60%.

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a heron is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a heron can fly'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a gull is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a gull can fly'"

By asking it one last time if the birds can fly you should finally have a good answer from it and this without ever having explicitly told it.

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a bird fly ?'"

As I did in the introduction of this tutorial, simply teach it that a phalarope is a bird and ask it if it can fly:

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'a phalarope is a bird'"
rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a phalarope fly ?'"

Since it has no information about this and because thinking that birds can fly in general, it estimates that the phalarope should be able to fly.

However, by asking if a kiwi can fly, you should always have the answer that not that you have explicitly given this knowledge.

rostopic pub -1 /feed_and_generalize/in std_msgs/String "data: 'can a kiwi fly ?'"

So we have seen here how to add new knowledge during the execution of the program and the impact of this through the mechanism of generalization.

If you wish, you can delete the internal file Ontologenius has created and try again by adding the following line just before closing the ontology in the main function.

onto.reasoners.deactivate("ReasonerGeneralize");

In fact, the generalization mechanism is a reasoning plugin that like all these mechanisms can be active or not through the reasoning manager.

However, if you do not delete the Ontologenius internal file, the generalization has already been done and will remain so. It will just not be able to achieve new generalizations.