Example 1: asking questions

Let us start with a simple code for NLUlite:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy()
wisdom   = Wisdom(server)
wisdom.add('The chancellor is going to Berlin in October 2010')
answer  = wisdom.ask('Who goes to Germany after 2009')

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

the output is

The chancellor is going to Berlin.
person: the chancellor

In this example the first line connects to a running server:

server   = ServerProxy()

The previous line is equivalent to writing

server   = ServerProxy('localhost', 4001)

which connects to a server that is running on “localhost” on the default port 4001. A different port can be set when starting the server from the command line.

The second line initializes a Wisdom object, using the server proxy of the first line:

wisdom   = Wisdom(server)

The Wisdom class is where the text information is stored. All the data, rules, descriptions are provided to the class by using the method Wisdom.add(), as in the third line:

wisdom.add('The chancellor is going to Berlin on October 2010')

After this, the wisdom can be asked a question:

answer  = wisdom.ask('Who goes to Germany after 2009')

The Answer class contains all the information in the reply. The line:

print answer.comment()

prints a short comment to the answer. In the alpha version, this comment is limited to “Yes”, “No”, “the answer is not found in the data”.

Among other things, the answer contains a list of replies matching the question asked; in this example the list contains only one element. In general, the list of replies can be written out by using a cycle:

for item in answer.elements():

The following line prints a text that gives a short comment about the answer item:

print item.comment()

A more advanced control over the answer output is explained in the advanced example sections.