In a previous post, we talked about how you can query the True Knowledge API using natural language to get the current time in cities around the world. This post builds on the background covered in Part I, where we dive into the True Knowledge query language.
Facts and Variables in True Knowledge Queries
Our query language is a lot like any other programming language: using language grammer, you describe the information you are after. At the heart of True Knowledge is what we call facts. Facts are relationships between two things. A fact is very simple structure, namely:
FactName: Something Relation AnotherThing
For example, the following fact tells True Knowledge that the string "London UK" can be used to describe the semantic object in our knowledge base [london]:
fact: ["London UK"] [can denote] [london]
The key point here is that we are describing a relationship between the string "London UK" and the object [london].
In our query language, you can use variables to fill in parts of the facts you are querying, much like any other programming language. We'll see how this works shortly. This foundation of facts and variables allows you to specifically describe the knowledge you want the API to return.
The Query
Using the "What is the time in Tokyo?" question discussed in Part I, the query would look like this:
1 2 3 4 5 | query tokyotime ["tokyo"] [can denote] city /r [current time] [applies to] now fact: tokyotime [is the local time in] city fact [applies at timepoint] now |
In the first line, we specify that we are creating a new query for a variable called tokyotime. The rest of the query will define how to calculate the value of the variable tokyotime. You can call it anything really, and you can ask for multiple variables by comma separating them on the first line.
In the second line, we specify another variable, we're calling city, and saying that this variable is actually an semantic object you can describe with the string "Tokyo". Also, we are specifying that we want the famous "Tokyo" not just any Tokyo using the /r modifier. This modifier means ignore the objects that are rarely referred to with just "Tokyo". In Part I, we talked about Tokyo in Papua New Guinea, and using /r ignores that. Thus in short, this line says find the semantic object of the famous city we call Tokyo and assign its semantic object to the variable city.
In the third line we set up another variable called now. This variable is set to the value of the semantic object [current time] which always has the correct current time.
The fourth and fifth lines are where we calculate the value the variable tokyotime. Firstly, in the fourth line, we create a fact that specifies that tokyotime is related to the city, namely, it is the local time in city. Secondly, we constrain this fact by saying it is the local time now not at some other point in time. This combination thus means tokyotime is the local time in the city right now.
So to summarize the whole query, we're asking for a variable called tokyotime that is the local time right now in the famous city "Tokyo".
Query in PHP
Using our PHP library from our developer home page, you can easily implement the above query in your websites. A fully working example is below.
Please note that you will need the latest version of our PHP library, released today, for the code below to work.
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 26 27 28 29 30 31 32 | <?php define('API_DIRECTORY', dirname(__FILE__) . '/..'); define('API_ACCOUNT_ID', ''); define('API_ACCOUNT_PW', ''); $query = 'query tokyotime ["tokyo"] [can denote] city /r [current time] [applies to] now fact: tokyotime [is the local time in] city fact [applies at timepoint] now'; require_once(API_DIRECTORY.'/api.php'); $request = new KnowledgeEnginequeryRequest(); $request->setAccountId(API_ACCOUNT_ID); $request->setAccountPassword(API_ACCOUNT_PW); $request->setTranslateAnswers(TRUE); $request->setQuery($query); $response = $request->execute(); if(strlen($response->getErrorCode()) > 0) { echo $response->getErrorCode()." ".$response->getErrorMessage(); } else { //echo $response->getXml(); //Helpful for debugging foreach($response->getResults() as $result) { echo $result->getVariable("tokyotime")->getUrs(); } } ?> |
You will need to specify your API account name and password on lines 3 and 4 before it runs correctly. The simply sets up the query and runs it. If there aren't any errors, we find the variable tokyotime and get its unique translation, which is a string that uniquely describes the variable's contents. That's the answer we're after, and in the example we just echo it.
Comments