Example 4: Read a website

You can read the content of a web page just by typing the url:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy()
wisdom   = Wisdom(server)
wisdom.add_url('http://simple.wikipedia.org/wiki/Snakes')
answer  = wisdom.ask('what do the snakes need')

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

Notice that it can take a few seconds to read a webpage. It is therefore convenient to split the code in two parts: a part that fetches the website and saves it locally:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy()
wisdom   = Wisdom(server)
wisdom.add_url('http://simple.wikipedia.org/wiki/Snakes')
wisdom.save('fetched.wisdom')
print 'The article was saved in "fetched.wisdom".'

and a part that loads the fetched wisdom and ask the question:

from NLUlite import ServerProxy, Wisdom

server   = ServerProxy()
wisdom   = Wisdom(server)
wisdom.load('fetched.wisdom')
answer  = wisdom.ask('what do the snakes need')

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

with output:

the answer is a list

what: a heat of the sun controls a body temperature
Like all reptiles, snakes need the heat of the sun to control their body temperature.

In this way, you can ask different questions without waiting each time for the server to read the page.