Advanced example 2: the answer elements for inference

In the previous example

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()

The result of an inference is printed out as:

the answer is a list

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

Notice that the result of the inference cannot be found in any part of the text, but it is a combination of various parts of the text.

If you need more control you should not use item.comment. Instead, you should parse the item.rules and write

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():
    for pair in item.pairs:
        print pair.query + ': ' + pair.reply
    for rule in item.rules:
        print rule.text

with output:

the answer is a list
person: David
David is happy.
If a person is happy, he smiles.