Wisdom class

/static/WisdomDiagram.png

The Wisdom class is the most relevant class of NLUlite. All the data is provided to this class, and all the questions are managed by it. Your code can have many Wisdom objects, and each one is a separate knowledge base. The Wisdom class holds the information until it is destroyed (for example at the end of a script) or until an explicit Wisdom.clear command is called.

Besides the examples of the previous sections, a few more options are available.

Description

A text can be introduced by a description about the content of the text. This description is invisible to the parser, and can be a comment or a link that reminds you where the text is taken from. These descriptions must be included between:

[% ... %]

as in the example:

from NLUlite import ServerProxy, Wisdom

server  = ServerProxy('localhost', 4001)
w       = Wisdom(server)

w.add('[% Taken from wikipedia %] The chancellor goes to Berlin.')
answer = w.ask('Who goes to Germany?')

print answer.comment()
for item in answer.elements():
    print item.comment()
    print '[%' + item.description + '%]'

All the text contained between “[% ... %]” is interpreted as a description referring to all the text that follows.

Save/load

Parsing a long text can take some time. If you are going to use the same text more than once, it is a good idea to save the Wisdom to a file. This file contains all the information in the Wisdom in the internal representation used by NLUlite.

The following example writes the file “test.wisdom”:

from NLUlite import ServerProxy, Wisdom

server  = ServerProxy('localhost', 4001)
wisdom  = Wisdom(server)
wisdom.add('The chancellor is going to Berlin')
wisdom.save('test.wisdom')

The same file can then be loaded using the Wisdom.load() method:

from NLUlite import ServerProxy, Wisdom

server  = ServerProxy('localhost', 4001)
wisdom  = Wisdom(server)
wisdom.load('test.wisdom')

answer = wisdom.ask('Who goes to germany')

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

with an output that answers the question using the data previously saved.:

the answer is a list

person: the chancellor
The chancellor is going to Berlin.

Save as RDF

Saving in the TriG-RDF format is as simple as saving in the “wisdom” format:

from NLUlite import ServerProxy, Wisdom

server  = ServerProxy('localhost', 4001)
wisdom  = Wisdom(server)
wisdom.add('The chancellor is going to Berlin')
wisdom.save_rdf('test.rdf')

Exporting to RDF is still under development. At the moment, it is not possible to load from RDF files.

Clear

If you wish to clear the content of a Wisdom object, just call the Wisdom.clear() method:

wisdom  = Wisdom(server)
## lots of code here
wisdom.clear()
## Good as new!

Export

One problem with the Wisdom class is that it is confined to a specific session of the script. After the script finishes the content of a Wisdom is erased.

As long as the server is running, you can export the Wisdom to the server, by using the Wisdom.export_to_server() method. The content of your Wisdom is then stored into the server for as long as you like, and it can be retrieved by a Wisdom in another script.

Exporting a Wisdom to the server is easy:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
wisdom.add('The chancellor is going to Berlin.')
wisdom.export_to_server('name_ID')

This code exports a Wisdom under the name “name_ID”. In another session you can then get the same Wisdom by using the method Wisdom.import_from_server:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
wisdom.import_from_server('name_ID')
answer  = wisdom.ask('Who goes to Germany')

for item in answer.elements():
    print item.text
    for pair in item.pairs:
        print pair.query + ": " + pair.reply

The published wisdom can be erased from the server by using ServerProxy.erase_exported:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy('localhost', 4001)
wisdom   = Wisdom(server)
server.erase_exported('name_ID')

The list of all the published Wisdoms can be retrieved by using the ServerProxy.list_exported():

from NLUlite import ServerProxy, Wisdom

server = ServerProxy('localhost', 4001)
wisdom = Wisdom(server)
plist  = server.list_exported()
for item in plist:
    print item

The methods Wisdom.export_to_server() and ServerProxy.erase_exported() have additional arguments

Wisdom.export_to_server(name_ID, password, timer)
Parameters:
  • name_ID – String object, the name of the exported Wisdom
  • password – String object, the password needed to erase the Wisdom (default = ‘’)
  • timer – integer, the number of seconds after which the Wisdom expires (default = -1, the password never expires)
ServerProxy.erase_exported(name_ID, password)
Parameters:
  • name_ID – String object, the name of the exported Wisdom
  • password – String object, the password needed to erase the Wisdom (default = ‘’)