Example 3: inference!

Rules of inference can be added to the Wisdom by simply writing them in the text

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
wisdom.add('David is happy. If a person is happy, he smiles.')
answer   = wisdom.ask('Who smiles?')

print answer.comment()
for item in answer.elements():
    print item.comment()

and the output would be:

the answer is a list

person: David
David is happy.+ If a person is happy, he smiles.

Some rules are implemented by default. For example you can try the following code:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
wisdom.add('Munich is in Bavaria. Bavaria is in Europe')
answer   = wisdom.ask('is Munich in Europe?')

print answer.comment()
for item in answer.elements():
    print item.comment()

with output

Yes

Munich is in Bavaria.+ Bavaria is in Europe.+if A is in B and B is
in C, then A is in C.

In the current alpha version only a one-level inference is allowed. A full inference tree will be developed for the next versions.

Common Wisdom

A set of standard rules is implemented in the file NLUlite/wisdoms/common.wisdom. This file can be loaded and used in the following way

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
wisdom.load('~/NLUlite/wisdoms/common.wisdom')
wisdom.add('David sells a car to Maria')
answer   = wisdom.ask('Who buys what from whom?')

print answer.comment()
for item in answer.elements():
    print item.comment()

ending with the output

the answer is a list

person: Maria
what: a car
whom: David
David sells a car to Maria.+If someone#1 sells something#2 to
someone#3 then someone#3 buys something#2 from someone#1.

Advanced

All the following rule declarations are equivalent:

* If a person is happy, he smiles.
* If a person is happy he smiles.
* If a person is happy then he smiles.
* A person smiles if he is happy.

At the moment, only rules of the type “if A,B,C then D” are supported (Horn clauses). One could write:

If a person goes to the South Pole and he does not bring a jacket he
freezes.

More complex rules might involve two or more actors, in which case it is difficult for the framework to understand who is “he” or “she” or “whom”. In order to help, a system of tags can be used by adding “#” after the word. For example:

If someone#1 breaks into someone#2's house, someone#1 is a thief.

This tag is only valid within the same sentence. In the following example:

If someone#1 breaks into someone#2's house, someone#1 is a thief. If
someone#1 is happy, someone#1 smiles.

the two words tagged with “#1” in the two rules are not related.