We recently had a great idea from an API user: on their contact page, they wanted to show the current time at each of their offices worldwide in real time.
As you may know, there are two ways to use our API:
- In Direct Answer mode, you submit a question in English we translate and understand and then give you an answer. This is by far the easiest way to get started.
- In True Knowledge Query mode, you submit your question in our semantic query language, and you will get the same answer.
Since the Query mode gives a detailed insight into how Direct Answer works, I will explain it in Part II of this post.
Before we start: If you are a PHP developer, we provide an officially supported library that makes interacting with our API straightforward using PHP classes. If you another language, our API is quite easy to call over HTTP. You can download the library from our developer home page.
Direct Answer
This mode is the easiest to work with. For example, if we are interested in the current time in London, you simply submit the question "What is the time in London?", and the API will return an XML response with the answer.
Using our PHP library, the code would be as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | define('API_DIRECTORY', 'WHERE_YOU_INSTALLED_LIBRARY');
define('API_ACCOUNT_ID', 'YOUR_API_USERNAME');
define('API_ACCOUNT_PW', 'YOUR_API_PASSWORD');
require_once(API_DIRECTORY.'/api.php');
$request = new KnowledgeEngineDirectAnswerRequest();
$request->setAccountId(API_ACCOUNT_ID);
$request->setAccountPassword(API_ACCOUNT_PW);
try
{
$query = "What is the time in London, England?";
$request->setQuery($query);
$response = $request->execute();
if(strlen($response->getErrorCode()) > 0)
{
echo $response->getErrorCode()." ".$response->getErrorMessage();
}
else
{
echo $response->getTextResult();
}
}
catch ( Exception $ex)
{
echo $ex->getMessage();
}
|
The code is self-explanatory: you define where the library is installed, set up the query and run it, check for errors, and if all is clear, the text result would contain your answer.
The answer would be what you see on our main website: What is the time in London, England?, which would say something like "March 31st 2010, 15:31:53 BST".
Disambiguation
When you ask about London, which city are you really talking about? Most people would think you are talking about London, England, but there is another London in Ontario, Canada. Likewise for Tokyo: there is the capital of Japan and there is a Tokyo, Papua New Guinea.
Our Direct Answer system automatically decides - disambiguates - which London or Tokyo you are referring to and returns the most expected answer. You can rephrase your question to ask about a different city; for example: What is the time in London Ontario?.
Another way to get a very specific unambiguous answer is to use the True Knowledge ID of the semantic object you are referring to. For London, UK, the True Knowledge ID is [london] and for London, Ontario, the ID is [london ontario]. You can rephrase your question to use the True Knowledge ID: what is the time in [london ontario]? and get the same result.
To get the True Knowledge ID, simply type the name of what you are referring to into our answer box and the answer will tell you the ID. For example: London Ontario.
Getting Started
With all this, you can now start implementing this on your own website:
And if you get stuck, please ask at our API Support Forums.
Comments