So far, we have only used individuals in one word that did not allow us to see
if we were manipulating identifiers or words in natural language. But if we want
to be able to work with different languages, we will have to make the link between
a word and its computer identifier.
So you can test the program with the following words: "green cup", "blue cup" and "green book".
You have the following result which shows that no intruder has been found.
[SAY]on green cup blue cup green book
[SAY]Let's play!
However, by redoing the test with the "green_cup", "blue_cup" and "green_book"
identifiers, you have this:
[SAY]on green_cup blue_cup green_book
[SAY]the intruder is green_book because it is the only one that is not: cup
[SAY]the intruder is blue_cup because it is the only one that is not: green
[SAY]Let's play!
You understand that we have so far worked with identifiers and not with words in natural language.
Multilingual translation can not be done simply by changing the language of work.
To make the transition from natural language to identifiers and vice versa, we will use the two new
functions: find() and getName()
find
The find function is used to retrieve all concepts of a given type whose name in natural language is
the word passed in the parameter. In this way, if several words are said (and therefore are written) in
the same way we will be able to recover them all. In this tutorial, this case is not present in
order not to add complexity. That's why we will each time recover the first element of the list.
We will therefore modify our callback function to realize the natural language transition
to identifiers.
defwordCallback(msg):
global_start
global_names
global_onto
ifmsg.data == 'start':
_start = True
elifnotmsg.data in_names:
ids = _onto.individuals.find(msg.data)
iflen(ids) == 0:
say('this word is unknown' )
else:
_names.insert(1, ids[0])
The find function is applied to individuals because we want individuals to input.
ids = _onto.individuals.find(msg.data)
We can test our program again with the words : "green cup", "blue cup" and "green book". We thus
obtain the same exit as with the old use of the identifiers:
[SAY]on green_cup blue_cup green_book
[SAY]the intruder is green_book because it is the only one that is not: cup
[SAY]the intruder is blue_cup because it is the only one that is not: green
[SAY]Let's play!
getName
Our display still uses identifiers. We will change this thanks to the function getName which
will return a natural language word relative to the identifier passed in parameter.
The only thing we have to change is the display line as follows:
say('the intruder is ' + _onto.individuals.getName(intruder[0]) + \
' because it is the only one that is not: ' + \
_onto.classes.getName(intruder[1]));
The result of our program becomes:
[SAY]on green_cup blue_cup green_book
[SAY]the intruder is green book because it is the only one that is not: cup
[SAY]the intruder is blue cup because it is the only one that is not: green
[SAY]Let's play!
Multilingual
We will now modify our launch file to put the working language in french.
Well, that's it, our program will now work in French. You can test it with the words
"tasse verte", "tasse bleu" et "livre vert".
The result of our program is:
[SAY]on tasse verte tasse bleu livre vert
[SAY]the intruder is livre vert because it is the only one that is not: tasse
[SAY]the intruder is tasse bleu because it is the only one that is not: vert
[SAY]Let's play!
The advantage of this method is that internally the program uses identifiers that are
much faster to find and thus only the interface uses the natural language.
Go further
To go further, you can add the ability to change the working language with the setLang() function.
_onto.actions.setLang('en')
You can also change the display texts according to the language used by retrieving the
language currently used with the getLang() function.
lang = _onto.actions.getLang()
To obtain the identifier of a word in natural language, we used the function find, but Ontologenius provides other functions to obtain an identifier from natural language.
You can try the functions proposed by class OntologyClient.