<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Neural Network Design blog &#187; tips and tricks</title>
	<atom:link href="http://janbogaerts.name/index.php/category/tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://janbogaerts.name</link>
	<description>My take on neural networks, AI and more</description>
	<lastBuildDate>Thu, 15 Dec 2011 18:43:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Scraping the Internet</title>
		<link>http://janbogaerts.name/index.php/2011/11/13/scraping-the-internet/</link>
		<comments>http://janbogaerts.name/index.php/2011/11/13/scraping-the-internet/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 12:48:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[chatbot designer]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Screencasts]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[scraping]]></category>
		<category><![CDATA[screencasts]]></category>

		<guid isPermaLink="false">http://janbogaerts.name/index.php/2011/11/13/scraping-the-internet/</guid>
		<description><![CDATA[Getting information from the internet into a chatbot’s output can be very useful at some times. Not only to show continuously changing values, like weather information, but it can potentially also be used for learning, although the latter is obviously a little trickier. Retrieving, or scraping info from the internet can be done remarkably easy [...]]]></description>
			<content:encoded><![CDATA[<p>Getting information from the internet into a chatbot’s output can be very useful at some times. Not only to show continuously changing values, like weather information, but it can potentially also be used for learning, although the latter is obviously a little trickier.</p>
<p>Retrieving, or scraping info from the internet can be done remarkably easy with the chatbot designer. Here’s a screencast of a bot that retrieves weather information from the <a href="http://googleweather.riaforge.org/" target="_blank">google weather api</a>.</p>
<div class="lyte hidef" id="WYL_ppfuKxQkSJo" style="width:640px;height:360px;"><noscript><a href="http://youtu.be/ppfuKxQkSJo"><img src="http://img.youtube.com/vi/ppfuKxQkSJo/0.jpg" alt="" width="640" height="360" /><br />Watch this video on YouTube.</a></noscript><script type="text/javascript"><!-- 
 (function(){var d=document;if(d.addEventListener){d.addEventListener('DOMContentLoaded', insert, false)}else{window.onload=insert} function insert(){if(!d.getElementById('lytescr')){lytescr=d.createElement('script');lytescr.async=true;lytescr.id='lytescr';lytescr.src='http://janbogaerts.name/wp-content/plugins/wp-youtube-lyte/lyte/lyte-min.js?wylver=0.9.2';h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(lytescr, h)}};}()) 
 --></script></div>
<p>In the video, a .net plug-in is used to retrieve information from the internet by means of XPaths. This plugin is included by default in the application. Note though that plug-ins are only supported in the pro version.  Basic users will be able to use these projects, but they can’t create or edit any patterns that rely on plug-ins. Also, plug-ins are loaded on a project by project basis. So if you want to use the scraping features in your own project, you will first need to make certain that the correct .net functions have been loaded. Once this has been set up though, all plug-ins will <a href="http://janbogaerts.name/wp-content/uploads/2011/11/Capture1.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; float: right; padding-top: 0px; border-width: 0px;" title="Capture" src="http://janbogaerts.name/wp-content/uploads/2011/11/Capture_thumb1.jpg" alt="Capture" width="396" height="212" align="right" border="0" /></a>automatically be loaded when the project is opened.</p>
<h3>Loading</h3>
<p>To load a plug-in, go to <em>view/communication channels/OS</em>. This will bring up a view like the one on the right. From here, you can load and unload dlls, classes and functions. First up is the dll. This can be loaded with one of the buttons on the toolbar. The first one gives access to the cache (dlls that have already been loaded). With the next button, you can select a file from disk. Note that, even though the ‘<em>CmdShell.dll’</em> file (which contains the scraping functions) is part of the installation, it isn’t guaranteed that it’s already loaded in cache, so you might have to select it from the ‘<em>program files/Chatbot designer pro/’</em> path. By the way, you can remove a dll by selecting it and pressing delete. Functions can be selected/deselected with the checkbox in front of the name. You can alternatively (de)select the entire class or lib at once. Notice the blue label behind each function name: this is the name that you can use in the patterns. You see, the do-patterns evaluator has no knowledge whatsoever of namespaces, classes or functions, it just knows a single name. This means that all function names should be unique across a single project. If you try to enter a duplicate name, a red box will be displayed round the newly mapped name.</p>
<p>There are quite a few functions available for scraping. Basically though, there are 3 groups: some functions to open/close web-pages, some functions to get data from those opened pages and finally the same functions that don’t require you to first open/close any files but which can do a scrape directly.</p>
<h3>Short scrapes</h3>
<p>Depending on how much data you need to retrieve, you can use one or the other technique. If there is only 1 xpath that you have to run on a page, then you can probably best use the short/direct functions that don’t require you to first open the web-page. Instead the address is supplied as an argument, together with the xpath. Here’s a list of the available quick scrapers:</p>
<table width="554" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="128"><strong>Name</strong></td>
<td valign="top" width="108"><strong>Arg 1</strong></td>
<td valign="top" width="58"><strong>Arg 2</strong></td>
<td valign="top" width="258"><strong>result</strong></td>
</tr>
<tr>
<td valign="top" width="129">ScrapeText</td>
<td valign="top" width="108">file or web path</td>
<td valign="top" width="58">XPath</td>
<td valign="top" width="258">0, 1 or more text values</td>
</tr>
<tr>
<td valign="top" width="129">ScrapeInt</td>
<td valign="top" width="108">file or web path</td>
<td valign="top" width="58">XPath</td>
<td valign="top" width="258">0, 1 or more int values</td>
</tr>
<tr>
<td valign="top" width="129">ScrapeDouble</td>
<td valign="top" width="108">file or web path</td>
<td valign="top" width="58">XPath</td>
<td valign="top" width="258">0, 1 or more floating point values</td>
</tr>
<tr>
<td valign="top" width="129">ScrapeDate</td>
<td valign="top" width="108">file or web path</td>
<td valign="top" width="58">XPath</td>
<td valign="top" width="258">0, 1 or more dates</td>
</tr>
</tbody>
</table>
<p>And a short usage example to get the temperature info from the google API for a city that’s defined in ‘<em>$place</em>’:</p>
<blockquote><p>$value = ScrapeText(&#8220;<a href="http://www.google.com/ig/api?weather=">http://www.google.com/ig/api?weather=</a>$place:interleaf(+)&#8221;, “<a href="mailto:“/xml_api_reply/weather/current_conditions/temp_c/@data">/xml_api_reply/weather/current_conditions/temp_c/@data</a>”)</p></blockquote>
<p>As you can see, the first argument specified the web-page to open. The second is an xpath to the data attribute of the ‘temp_c’ element. Note that we use ‘<em>:interleaf(+)</em>’  cause the google API expects city-names that contain multiple words to be separated with a ‘+’ like: New+York.</p>
<h3>More scraping</h3>
<p>The second scraping method is primarily useful if you need to run multiple xpaths on the same content. In this case, it’s far more economical to first retrieve the page, run all the queries on the cached file and finally, when done, release it again. This can be accomplished with the remaining scrape functions.</p>
<p>You open a file or webpage with either ‘OpenScraper’ or ‘OpenScraperHTML’. The first works on xml content, the second on html. That is, the second will convert html to xml so that the xpath can be run on it. Both return an integer that needs to be used in subsequent calls. Basically, the integer replaces the filename as a reference. It allows you to have multiple files open and to have the system run multi-threaded and let it serve multiple people at the same time.</p>
<p>The scraping functions themselves are almost identical as the quick versions, except that they take an integer as first argument instead of a path. Other then that, usage is exactly the same, with the same types: one for text, integers, doubles and dates.</p>
<p>Once you are done with the file, you have to call ‘CloseScraper’ with, as argument, the integer that was returned by ‘OpenScraper(HTML)’, so that resources can be cleaned up. This is important, if you forget to do this, the system will eventually buckle, crack and give up.<br />
In a normal usage situation, you would do a short salvo: open a page, do a few scrapes and close it again, all in 1 block, but this is not a requirement, you can keep the page open across multiple inputs. As long as you maintain a reference to the scraper (the integer) somewhere in memory so that you don’t loose track of it.</p>
<h3>Html scraping</h3>
<p>As already mentioned, html scraping is done by first converting the page into xml before the xpath is executed. This conversion can cause some ‘changes’ in the structure of the file. In other words, the path that you would calculate, based on the html file might not be correct for the xml version. This means that you best build your xpaths based on the xml version of the HTML pages.</p>
<p>The conversion routine that’s internally used by the chatbot designer is based on the <a href="http://developer.mindtouch.com/SgmlReader" target="_blank">SGMLReader</a> library. This provides a command-line tool to manually convert html to xml files. This can be very useful for building the correct query. I’ve included a direct download for the <a href="http://www.janbogaerts.name/files/sgmlreader.zip" target="_blank">command line html to xml conversion tool</a>. Here’s a short description on how to use it (taken from the original documentation):</p>
<blockquote><p>sgmlreader &lt;options&gt; [InputUri] [OutputFile]</p></blockquote>
<table width="811" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="113">-e &#8220;file&#8221;</td>
<td valign="top" width="696">Specifies a file to write error output to. The default is to generate no errors. The special name &#8220;$stderr&#8221; redirects errors to stderr output stream.</td>
</tr>
<tr>
<td valign="top" width="115">-proxy &#8220;server&#8221;</td>
<td valign="top" width="695">Specifies the proxy server to use to fetch DTD&#8217;s through the fire wall.</td>
</tr>
<tr>
<td valign="top" width="116">-html</td>
<td valign="top" width="694">Specifies that the input is HTML.</td>
</tr>
<tr>
<td valign="top" width="117">-dtd &#8220;uri&#8221;</td>
<td valign="top" width="693">Specifies some other SGML DTD.</td>
</tr>
<tr>
<td valign="top" width="118">-base</td>
<td valign="top" width="692">Add an HTML base tag to the output.</td>
</tr>
<tr>
<td valign="top" width="119">-pretty</td>
<td valign="top" width="691">Pretty print the output.</td>
</tr>
<tr>
<td valign="top" width="120">-encoding name</td>
<td valign="top" width="690">Specify an encoding for the output file (default UTF-8)</td>
</tr>
<tr>
<td valign="top" width="121">-noxml</td>
<td valign="top" width="689">Stops generation of XML declaration in output.</td>
</tr>
<tr>
<td valign="top" width="122">-doctype</td>
<td valign="top" width="688">Copy &lt;!DOCTYPE tag to the output.</td>
</tr>
<tr>
<td valign="top" width="123">InputUri</td>
<td valign="top" width="687">The input file name or URL. Default is stdin. If this is a local file name then it also supports wildcards.</td>
</tr>
<tr>
<td valign="top" width="124">OutputFile</td>
<td valign="top" width="686">The optional output file name. Default is stdout. If the InputUri contains wildcards then this just specifies the output file extension, the default being &#8220;.xml&#8221;.</td>
</tr>
</tbody>
</table>
<p><strong>Examples: </strong></p>
<blockquote><p>sgmlreader -html *.htm *.xml<br />
Converts all .htm files to corresponding .xml files using the built in HTML DTD.</p></blockquote>
<blockquote><p>sgmlreader -html http://www.msn.com -proxy myproxy:80 msn.xml<br />
Converts all the MSN home page to XML storing the result in the local file &#8220;msn.xml&#8221;.</p></blockquote>
<blockquote><p>sgmlreader -dtd ofx160.dtd test.ofx ofx.xml<br />
Converts the given OFX file to XML using the SGML DTD &#8220;ofx160.dtd&#8221; specified in the test.ofx file.</p></blockquote>
<h3>Building an XPath</h3>
<p>Once you have your xml file, getting the xpath to the element that you want can still be a little challenging. Html files simply aren’t designed with this type of usage in mind (and hey, if it can be easier for xml files, why not). Enter <a href="http://getfirebug.com/" target="_blank">FireBug</a>, an add-on for <a href="http://www.mozilla.org/" target="_blank">Firefox</a> that allows developers to get a closer look at the html…. Or xml. After you have installed firebug and loaded up the xml file into firefox, go to <em>tools/Web developer/Firebug/Open firebug</em> so that you can see the debug panel. In this panel, select the element that you which to query, open the context menu and select ‘<em>copy XPath</em>’. And that’s it, simply paste this path in the chatbot designer and your done.</p>
 <img src="http://janbogaerts.name/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=721" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://janbogaerts.name/index.php/2011/11/13/scraping-the-internet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Numbers, time, dates, durations</title>
		<link>http://janbogaerts.name/index.php/2011/11/12/numbers-time-dates-durations/</link>
		<comments>http://janbogaerts.name/index.php/2011/11/12/numbers-time-dates-durations/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 14:43:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[chatbot designer]]></category>
		<category><![CDATA[demos]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://janbogaerts.name/index.php/2011/11/12/numbers-time-dates-durations/</guid>
		<description><![CDATA[Besides text, a chatbot should also be able to handle other types of basic data, like numbers, date-time and time-spans. Numbers The engine of the chatbot designer app will automatically convert every part of the input text that it can, into an integer or double. Furthermore, with the aid of the thesaurus, you are also [...]]]></description>
			<content:encoded><![CDATA[<p>Besides text, a chatbot should also be able to handle other types of basic data, like numbers, date-time and time-spans.</p>
<h3>Numbers</h3>
<p>The engine of the chatbot designer app will automatically convert every part of the input text that it can, into an integer or double. Furthermore, with the aid of the thesaurus, you are also able to pick up words that represent numbers and convert them into their corresponding number values. This allows us to easily work with and manipulate these numbers. Here are a few input pattern examples:</p>
<blockquote><p>^n:number</p>
<p>^n:integer</p>
<p>^n:double</p>
<p>^n:noun.(language unit).numeral</p></blockquote>
<p>The first 3 examples are the simplest: you can read a double (floating number), an integer or if you don’t care which, you can simply use ‘number’. The last one is a bit more interesting. Here we use the thesaurus to catch words like ‘one’, ‘two’,… Of course, that’s very limiting, cause this way, you’d have to declare a thesaurus-entry for every single number in the universe, which is, well…, impossible. So better to write the last pattern like so:</p>
<blockquote><p>^n:noun.(language unit).numeral { [and] ^n:noun.(language unit).numeral}</p></blockquote>
<p>The {} operator is used to find a repeating block in the input. So this catches things like: <em>two thousand and five hundred</em>.  As you can see, we use the same variable name for the number in the front and repeating part. This means that most likely, the ‘n’ variable will contain a list of words instead of just 1. Also, we don’t collect the ‘and’ cause it would screw up the conversion procedure. A small warning about the {} operator though: <em><strong>The application can’t find duplicate patterns when the {} operator is used</strong>,</em> so watch out for this.</p>
<p>For completion, you could also include a pattern that handles mixed numbers and words. The conversion algorithm is perfectly capable of handling this mixed type of input.</p>
<h3>Working with numbers</h3>
<p>So, now that we can read in the numbers, lets look at how we can use them. let me just pick up where we ended: with word-numbers. Right now, the variable is still containing words (or a mix of words and numbers), but we actually need a single number, so this needs to be converted. Luckily, you don’t need to do this manually, but there’s already a function available for this. Here’s a usage example:</p>
<blockquote><p>$value = $n:ToNumber</p></blockquote>
<p><em>:ToNumber</em> will convert the content of the variable into a number (only integers are currently supported, a ‘.’ is not yet recognized). Each individual word is converted using the thesaurus and finally added/multiplied together to form a single number. Now, this conversion algorithm is language dependent and at the time of writing, there is only an implementation for English. If you need it for a different language, best to contact me.</p>
<p>Of course, the primary purpose for having real numbers, is so that we can do calculations with them. As you’ll soon see though, this is done a little bit different compared to a traditional programming language. That’s because the chatbot designer’s pattern definition language is text oriented and not math. This means that if you were to type something like $n + $w, it would not perform the calculation, but print/store the ‘+’ sign in between the 2 numbers. So, to perform arithmetic, you need to use functions. here are some examples:</p>
<blockquote><p>$result = $n:add</p>
<p>$result = $n:add($w)</p>
<p>$result = $n:subtract($w,$d,$f)</p>
<p>$result = $n:subtract($w,$d,$f:Add($h))</p>
<p>…</p></blockquote>
<p>The basic principle is always the same: since a variable can contain a list of items (a set), it doesn’t really need arguments to perform the arithmetic operation, as long as it has enough items in the list. Because of this, most mathematical operations can be performed on more then 2 items. So you don’t write <em>a+b+c</em>, but if all 3 are in the same variable, you simply write <em>:add</em>.</p>
<p>If the variable doesn’t contain enough values or you want to include additional values, you can add those as arguments. For most functions, this list is unlimited and can always be nested, so you can do mathematical operations inside others.</p>
<p>For completion, here are all the currently supported mathematical operators:</p>
<table width="564" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="143"><strong>Function</strong></td>
<td valign="top" width="309"><strong>Description</strong></td>
<td valign="top" width="110"><strong>Nr of arguments</strong></td>
</tr>
<tr>
<td valign="top" width="143">Add</td>
<td valign="top" width="309">Adds all the numbers</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Subtract</td>
<td valign="top" width="308">subtracts each item from the first</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Multiply</td>
<td valign="top" width="307">multiplies each item with the first</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Divide</td>
<td valign="top" width="307">divides each item with the first</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Modulus</td>
<td valign="top" width="307">returns the modulus of the 2 numbers</td>
<td valign="top" width="110">2 items</td>
</tr>
<tr>
<td valign="top" width="143">Complete</td>
<td valign="top" width="307">completes the sequence of the 3 numbers</td>
<td valign="top" width="110">3 items</td>
</tr>
<tr>
<td valign="top" width="143">Count</td>
<td valign="top" width="307">counts the nr of items in the list</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">StDev</td>
<td valign="top" width="307">calculates the standard deviation of the values</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Min</td>
<td valign="top" width="307">returns the smallest number in the list</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Max</td>
<td valign="top" width="307">returns the biggest number in the list</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Avg</td>
<td valign="top" width="307">gets the average of all the values.</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Complement</td>
<td valign="top" width="307">removes all the items in the argument from the input variable and returns the result (the difference between the 2).</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Reverse</td>
<td valign="top" width="307">Reversed the order of the items in the list (last becomes first)</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Distinct</td>
<td valign="top" width="307">Removes all duplicates. If there are any arguments, these are first added to the end of the list before duplicates are removed.</td>
<td valign="top" width="110">any length</td>
</tr>
<tr>
<td valign="top" width="143">Union</td>
<td valign="top" width="307">Adds the arguments to the end of the input items.</td>
<td valign="top" width="110">any length</td>
</tr>
</tbody>
</table>
<h3>Date and time</h3>
<p>Next on the menu are dates and times. These are very closely related to numbers. Internally, the system doesn’t see a difference between a date and a time, it’s always a DateTime. Depending how you use the value, you can regard it as a date, time or DateTime.</p>
<p>First of, there is a special variable to get to the current system date/time: <strong>$time</strong>. This returns a DateTime-cluster which contains a list of numbers that together form the date and time. The order is predetermined by the system to make it language independent. For ease of use, there are functions to get to each part of the date, so you don’t need to memorize each position in the format (though it helps for putting dates back together). Here’s a table of all the functions to get each part of the date list (+ the index position of the item in the list):</p>
<table width="553" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="49"><strong>Index</strong></td>
<td valign="top" width="90"><strong>Function</strong></td>
<td valign="top" width="412"><strong>Meaning</strong></td>
</tr>
<tr>
<td valign="top" width="49">0</td>
<td valign="top" width="90">Year</td>
<td valign="top" width="412">Gets the year number.</td>
</tr>
<tr>
<td valign="top" width="49">1</td>
<td valign="top" width="90">Month</td>
<td valign="top" width="412">Gets the month of the year.</td>
</tr>
<tr>
<td valign="top" width="49">2</td>
<td valign="top" width="90">Day</td>
<td valign="top" width="412">Gets the day of the month.</td>
</tr>
<tr>
<td valign="top" width="49">3</td>
<td valign="top" width="90">Hour</td>
<td valign="top" width="412">Gets the hour (24 hours in a day)</td>
</tr>
<tr>
<td valign="top" width="49">4</td>
<td valign="top" width="90">Minute</td>
<td valign="top" width="412">gets the minutes (60 per hour)</td>
</tr>
<tr>
<td valign="top" width="49">5</td>
<td valign="top" width="90">Second</td>
<td valign="top" width="412">gets the nr of seconds (60 per minute)</td>
</tr>
<tr>
<td valign="top" width="49">6</td>
<td valign="top" width="91">DayOfWeek</td>
<td valign="top" width="412">gets the nr day number of the week (0=Sunday, 1= Monday,…)</td>
</tr>
</tbody>
</table>
<p>And here are some output patterns to show how you can use them:</p>
<blockquote><p>$time:day/$time:month/$time:year</p>
<p>$time:hour : $time:minutes : $time:second</p>
<p>The current month is ^noun.month[$time:month-1]</p></blockquote>
<p>Note that I deliberately put spaces between the variables and the  time indicator (‘:’)  to avoid using the escape character (\). Also, in the last example, we get the name of the month from the thesaurus through the index number.  A thesaurus child at a specific index position can be accessed with the [] operator in the variable path. Because there’s no point of putting words between the brackets and this so often requires a small calculation (0 vs 1 based indexes), I have opted here for a more traditional calculus notation. Supported operators are: +-*/%.</p>
<p>Often it’s also useful to recompose a date, based on a series of numbers. For instance, to build the birthday of someone. As you probably already suspected, there are functions to do just that. Here are some examples:</p>
<blockquote><p><span style="background-color: #ffffff;">$date = $list:ToDate</span></p>
<p><span style="background-color: #ffffff;">$time = $list:ToTime</span></p>
<p><span style="background-color: #ffffff;">$date = $year:ToDate($month,$day)</span></p></blockquote>
<p>For building the date, the first value in the list has to be the year. This is the only required item, but more can follow: month, day, hour, minute, second. In that order.  To build a time value, the first item has to be the hour. Minutes and seconds are optional, but have to be provided in the previously mentioned order. If you want to omit the minutes (or any other) part , use 0 instead.</p>
<p>Why not simply store the date values as individual numbers in memory fields yourself, you might wonder instead of going through the hassle of converting a number into another number (which these conversion functions basically do). Well, that will become apparent in the next section.</p>
<h3>querying on date</h3>
<p>The date and time values are stored in such a way that a year, month, day, minute, second or week in a date is always represented by the same neuron. This means that you can easily find all the dates (and therefor the attached assets) that meet certain criteria (all in the month June for instance). This is very useful in queries like “Who else is born in the same year and month as I am?”. Getting the date objects is real easy. Here’s an example:</p>
<blockquote><p>$people= #user.birthday:year:GetDates:ValueFor(birthday)</p>
<p>$people = $people:Complement(#user)</p>
<p>$people = #($people).name</p>
<p><em>&#8211;or, for year and month:&#8211;</em></p>
<p>$people= #user.birthday:year:GetDates(#user.birthday:Month):ValueFor(birthday)</p>
<p>$people = $people:Complement(#user)</p>
<p>$people = #($people).name</p></blockquote>
<p>Note: For a full example, check the Date-Time demo found at <em>{documents}\nnd\demos\dateTime.dpl</em>.</p>
<p>In the first line, we get the year section of the user’s birthday. With the ‘:GetDates’ function, we retrieve all the date objects that the system has stored for the same year as the user’s birthday.  If you supply extra arguments, those values will also have to be included in the date, as is shown in the second example. With ‘:ValueFor’ we retrieve all the assets that link to the date. The ‘birthday’ argument, specifies that we only want the assets that link to the date value with the ‘birthday’ attribute. We could have omitted this, in which case we would have gotten all the assets, no matter how they are linked (it could be a ‘when’ reference for instance). Or, we could also have given more arguments, specifying a list of possible attribute values.</p>
<p>At this point, we have a list of assets that reference a date of the same year as the user’s birthday. There are still 2 things that need to be done: first we need to remove the ‘#user’ from the asset list, cause the ‘GetDates’ returns all objects, including the date object that represents the user’s birthday, but since the bot was asked ‘who else’, we need to remove the ‘#user’. This is done with the ‘:complement’ function. This is a mathematical function (borrowed from set-theories) and will return a list that contains all the items from the input with all the arguments removed.</p>
<p>We do the call to ‘:Complement’ on a different code line and not on the first one for a very specific reason: asset paths, like in the first assignment, always provide a single value as input to each function in the path, so even if ‘GetDates’ returns a list of items, ‘ValueFor’ will always work on a single value. At the end, the asset path joins all the results together. This, of course, is of no use for the <em>Complement</em> function, cause we need to remove the items from the list and not from a single value. Luckily, regular variable paths do provide a list of items as input values. That’s why we started a new assignment, using a variable path to calculate the complement. There are a couple of other functions (like Interleaf, Filter, Add, Multiply, Avg, Min, Max,…) which require a list of items as input. These should never be used in an asset path, but only in variable paths, for the reason explained above.</p>
<p>In the last line, we get the name value of all the assets that we found. This is again on a different line cause we need an asset path to get the ‘name’ of an asset. Regular variable paths don’t know how to do this. Hence the extra 3th line.  And thus, we get the names of the known objects that have a birthday in the same year as the user.</p>
<h3>Time ranges</h3>
<p>Besides the Date-Time type, there is also a Time-Span type. This specifies a length of time instead of a specific moment. You get a Time-Span by building it with a function, or as the result of some Date-Time calculations (see later). To build a time range (or time-span), you can do something like:</p>
<blockquote><p><span style="background-color: #ffffff;">$range = $list:ToTimeSpan</span></p>
<p><span style="background-color: #ffffff;">$range = $days:ToTimeSpan($Hours)</span></p></blockquote>
<p>Unlike a DateTime, a timespan contains the number of: days, hours, minutes and seconds (no year or month sections). so there are only 4 elements in the list. The first item in the list should always be the nr of days, possibly followed by the hours,… Missing data is set to 0. Like with :ToDate and :ToTime, you can also specify missing data as arguments. The same order applies.</p>
<p>For retrieving values in a TimeSpan, you can use the same functions as for the DateTime. They operate in exactly the same way, but the index position of the actual items in the list is different compared to a DateTime. here’s an overview:</p>
<table width="553" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="49"><strong>Index</strong></td>
<td valign="top" width="90"><strong>Function</strong></td>
<td valign="top" width="412"><strong>Meaning</strong></td>
</tr>
<tr>
<td valign="top" width="49">1</td>
<td valign="top" width="90">Day</td>
<td valign="top" width="412">Gets the total nr of days in the range.</td>
</tr>
<tr>
<td valign="top" width="49">2</td>
<td valign="top" width="90">Hour</td>
<td valign="top" width="412">Gets the remaining nr of hours.</td>
</tr>
<tr>
<td valign="top" width="49">3</td>
<td valign="top" width="90">Minute</td>
<td valign="top" width="412">gets the remaining nr of minutes.</td>
</tr>
<tr>
<td valign="top" width="49">4</td>
<td valign="top" width="90">Second</td>
<td valign="top" width="412">gets the remaining nr o seconds.</td>
</tr>
</tbody>
</table>
<p>At the time of writing, TimeSpans don’t yet support the same level of querying as the DateTime object does. If anyone has an urgent need for this, let me know, and I’ll see what I can do.</p>
<h3>Calculating with dates and times</h3>
<p>Like with regular numbers, you can also perform some calculations on dates, like subtracting one date from another. The syntax for arithmetic with dates/times is exactly the same as with numbers. Here’s an overview of what’s supported:</p>
<table width="564" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="143"><strong>Function</strong></td>
<td valign="top" width="309"><strong>combination</strong></td>
<td valign="top" width="110"><strong>results</strong></td>
</tr>
<tr>
<td valign="top" width="143">Add</td>
<td valign="top" width="309">Time + {range}</td>
<td valign="top" width="110">Time</td>
</tr>
<tr>
<td valign="top" width="143"></td>
<td valign="top" width="308">range + {range}</td>
<td valign="top" width="110">Range</td>
</tr>
<tr>
<td valign="top" width="143">Subtract</td>
<td valign="top" width="308">Time &#8211; {Time}</td>
<td valign="top" width="110">Range</td>
</tr>
<tr>
<td valign="top" width="143">Min</td>
<td valign="top" width="308">Time, Time,…</td>
<td valign="top" width="110">Time</td>
</tr>
<tr>
<td valign="top" width="143"></td>
<td valign="top" width="308">range, range,..</td>
<td valign="top" width="110">Range</td>
</tr>
<tr>
<td valign="top" width="143">Max</td>
<td valign="top" width="308">Time, Time,…</td>
<td valign="top" width="110">Time</td>
</tr>
<tr>
<td valign="top" width="143"></td>
<td valign="top" width="308">range, range,..</td>
<td valign="top" width="110">Range</td>
</tr>
<tr>
<td valign="top" width="143">Avg</td>
<td valign="top" width="308">Time, Time,…</td>
<td valign="top" width="110">Time</td>
</tr>
<tr>
<td valign="top" width="143"></td>
<td valign="top" width="308">range, range,..</td>
<td valign="top" width="110">Range</td>
</tr>
<tr>
<td valign="top" width="143">StDev</td>
<td valign="top" width="308">Time, Time,…</td>
<td valign="top" width="110">Time</td>
</tr>
<tr>
<td valign="top" width="143"></td>
<td valign="top" width="308">range, range,..</td>
<td valign="top" width="110">Range</td>
</tr>
</tbody>
</table>
<h3></h3>
<p>In general, the principle is always the same: the function input determines the type of the result, except for ‘Subtract’ which always returns a range and only works on Time values.</p>
<h3>Boolean operations</h3>
<p>As a final topic, perhaps a word about logical or Boolean operations on numbers, dates and ranges. Well, for the most part, these work as you would expect both for regular numbers and times/ranges. There’s just 1 small twist. The mathematical people out there will probably have noticed that set-theory is very present in this system: all variables can contain a list of items. This means that Boolean operators need to be prepared for this. Here’s an overview on how each operator handles lists:</p>
<ul>
<li>Equality (==): If the left part contains a list, the right side must have the exact same list: with the same nr of items, in the same order.</li>
<li>Difference (!=) If the left part contains a list, the right side is different if it contains a different amount of items, the sequence is different or one or more of the items differ.</li>
<li>Contains (contains): The right side of the operator should be part of the list found on the left side of the operation.</li>
<li>doesn’t contain (!Contains): The right side of the operator should not be part of the list found on the left side of the operation.</li>
<li>&gt;, &gt;=, &lt;, &lt;= (Bigger, bigger or equal, smaller, smaller or equal): If the left part contains a list, each item in the list will be evaluated to each item found on the right side. The operation succeeds if all items are compared and none failed.</li>
</ul>
<p>There is plenty more to say about this topic. But my time-range has run out, a time to move on has been reached. All that’s left for me to say is: Stay tuned and CY later.</p>
 <img src="http://janbogaerts.name/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=717" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://janbogaerts.name/index.php/2011/11/12/numbers-time-dates-durations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why? because! :Invert</title>
		<link>http://janbogaerts.name/index.php/2011/11/08/why-because-invert/</link>
		<comments>http://janbogaerts.name/index.php/2011/11/08/why-because-invert/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 15:03:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[demos]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Getting started]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[because]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[have]]></category>
		<category><![CDATA[like]]></category>
		<category><![CDATA[why]]></category>

		<guid isPermaLink="false">http://janbogaerts.name/index.php/2011/11/08/why-because-invert/</guid>
		<description><![CDATA[‘A because B’ and ‘Why C’ are 2 tricky statement types for a chatbot to handle correctly. Not only does the data need to be stored and retrieved correctly, but often, some words need to be replaced, like ‘I’ vs ‘you’. Here are some possible techniques you can use with the chatbot designer to handle [...]]]></description>
			<content:encoded><![CDATA[<p>‘<em>A because B’</em> and ‘<em>Why C</em>’ are 2 tricky statement types for a chatbot to handle correctly. Not only does the data need to be stored and retrieved correctly, but often, some words need to be replaced, like ‘I’ vs ‘you’. Here are some possible techniques you can use with the chatbot designer to handle such input.</p>
<h3>Because</h3>
<p>Let me first give an example of a ‘because’ statement that a user might say:</p>
<blockquote><p>I have a dog because I like animals.</p></blockquote>
<p>This statement is a bit of an oversimplification, perhaps just right for an example.   It doesn’t only specify why I have a dog, but also <em>that</em> I have a dog and that I like animals. All this data should be stored and retrievable so that the system can answer questions like:</p>
<blockquote><p>Why do I have a dog?</p>
<p>Do I have a dog?</p>
<p>Do I like animals?</p></blockquote>
<p>So, before we can store the data, we need to find an input pattern that’s able to handle ‘<em>I have a dog because I like animals</em>’. To start with, we could do something like:</p>
<blockquote><p><span style="background-color: #ffffff;">I have a $value because $reason [.]</span></p></blockquote>
<p>This can work for many possible input values, except that the ‘I&#8217; vs ‘you’ change is difficult to do since there is no knowledge of the context (‘you’ can change into ‘I’, ‘me’,… depending on the context), so this is the least usable pattern, but there are other possibilities.  For example, lets take:</p>
<blockquote><p>I have a $value because I like $object [.]</p>
<p>or better:</p>
<p>I have a ^value:noun because I like ^object:noun [.]</p></blockquote>
<p>With this input pattern, we are able to extract all the valid data so that it can be stored in memory and we also have static text for ‘I’, which can easily be transferred manually. A small improvement might be to replace ‘$value’ with ‘^value:noun’ so that only a proper noun can be captured instead of things like ‘I have a <em>pretty big keyboard on my lap sitting there</em> because I like typing’. The same goes for ‘$object’. As in the previous example, ‘typing’ could probably better be transformed to ‘type’ before you store it, so in the pattern you could replace ‘$object’ with ‘^object:verb’ or ‘^object:noun’,… Anyway, lets take a look at how the do-patterns would look like for storing this info:</p>
<blockquote><p>#user += $value</p>
<p>#user.like.($object) = yes</p>
<p>#user.($value):why = “because you like $object”</p></blockquote>
<p>In the first line, we create a ‘has’ data item for ‘$value’. The second line adds the ‘object’ to the ‘like’ list, with the value ‘yes’ so that it’s clear the user likes it. And the third line stores the reason why the user has a ‘value’. ‘:why’ is a function that provides access to the ‘reason’ data-tree. What you store in ‘:why’ is completely up to you. In this example, we store a text, between brackets, cause I wanted to preserve the spacing, but you could also treat it as a sub asset (or anything else, like a link to another rule), allowing you to write paths like <em>#user.($value):why.object</em>.</p>
<h3>Why</h3>
<p>Once you have stored  the information, you can retrieve it again if the user asks a <em>why</em> question. Here’s a possible input pattern that you could use for capturing this question:</p>
<blockquote><p>Why do I have a $value [?]</p>
<p>or better:</p>
<p>Why do I have a ^value:noun [?]</p></blockquote>
<p>And the output pattern could look something like:</p>
<blockquote><p>You have a $value #user.($value):why</p>
<p>or better:</p>
<p><strong>Bot says when</strong>:  #user.($value):why</p>
<p>You have a $value #user.($value):why</p>
<p><strong>else:</strong></p>
<p>I don’t know why you have a $value</p></blockquote>
<p>That was easy enough. The first line is the most basic approach, the only problem with it: if the bot doesn’t know, it returns ‘<em>You have a</em>’. Which is no good. Better to put it in a conditional and check if there is a ‘why’ value, if there is non, let the bot say it doesn’t know. To check if there is a ‘why’ value, simply put the path in the ‘when’ close. This will check if the result of the path has a value. To check if it doesn’t have a value, use the not (!) operator in front of the pattern, like so:</p>
<blockquote><p><strong>Bot says when</strong>: !#user.($value):why</p>
<p>I don’t know why you have a $value</p>
<p><strong>else:</strong></p>
<p>You have a $value #user.($value):why</p></blockquote>
<h3>Improvements</h3>
<p>Right now, we have a working system to handle because/why type of things, but it’s not very flexible, I mean, there are tons of reasons why you can have something, and not just because you <em>like</em> something else. Also, there are more people then just ‘I’, so lets improve the input patterns a bit and make things more flexible.</p>
<h4>Using the Thesaurus</h4>
<p>Lets start with ‘I’. As you had probably already guessed, the thesaurus variables are ideally suited for this. So, here’s an improved input pattern:</p>
<blockquote><p>^s:pronoun.subject (have|has) a ^value:noun because ^w:pronoun.subject like ^object:noun [.]</p></blockquote>
<p>Ok, that’s already a bit more complicated. The <em>^s </em>variable can capture any child of the pronoun ‘<em>subject</em>’.  If you check in <a href="http://www.janbogaerts.name/files/full.thesaurus.xml" target="_blank">the thesaurus</a>, under the pronoun POS, there are a bunch of greyed-out items, one of which is ‘subject’. These are ‘placeholders’, that is: these thesaurus items don’t actually contain a word, only a label. So our ^s variable can’t actually catch the word ‘subject’, but only it’s children: <em>I, you, he, she, they, we, it</em>.</p>
<p>The do-patterns also become a bit more complicated, since we will now have to calculate the inverse of the pronoun and we also have to extract the asset out of it. Luckily, there are some helper functions for this:</p>
<blockquote><p>#($s:ResolvePerson) += $value</p>
<p>#($w:ResolvePerson).like.($object) = yes</p>
<p>#($s:ResolvePerson).($value):why = “because &amp;w:InvertPerson ^verb.like:conjugate($w:ResolvePerson) $object”</p></blockquote>
<p>:ResolvePerson is able to extract the asset (or concrete representation) out of a word.  At the time of writing: I and you  are supported. Names, ‘He’, ‘she’, ‘it’,… need some further testing.<br />
:InvertPerson uses thesaurus links to jump from one person to it’s inverse, if there is one. So ‘you’ becomes ‘I if ‘you’ was used as a subject.<br />
:Conjugate will try to find the correct conjugation of a verb, based on the argument that is supplied. This has to be an asset, usually the asset representation of the sentence-subject. So if you want to conjugate for ‘I’, you pass in ‘#bot’. ‘You’ becomes ‘#user’, and so on.</p>
<h4>Split the patterns</h4>
<p>The second improvement that we can make to the pattern is a bit more radical and relies on a special feature of the pattern matcher. You see, the pattern matching process is not restricted to finding just 1 pattern in the input. It will try to find the longest possible sequence of patterns that it can. That is, if the same words can be caught with a single pattern, this pattern will get precedence over a sequence of patterns. But if the pattern matcher can do a longer match by using 2 patterns instead of 1, that will become the result. This allows us to split up the first part of the sentence: ‘<em>I have a $value’ </em>from ‘<em>I like $object</em>’. opening up a whole new range of possibilities, and more importantly: saving use lots of duplicate work. The do-patterns become a little different though. First the input-patterns:</p>
<blockquote><p><strong>Topic: HAVE</strong></p>
<p>^s:pronoun.subject (have|has) a ^value:noun</p>
<p>because ^s:pronoun.subject (have|has) a ^value:noun</p>
<p>&nbsp;</p>
<p><strong>Topic: LIKE</strong></p>
<p>^w:pronoun.subject (like|likes) ^object:noun [.]</p>
<p>because ^w:pronoun.subject (like|likes) ^object:noun [.]</p></blockquote>
<p>As you can see, not much has changed except that they are now 2 sets of patterns and each set has a version with and without ‘because’ in the front. We could also have written them as:</p>
<blockquote><p><strong>Topic: HAVE</strong></p>
<p>[because] ^s:pronoun.subject (have|has) a ^value:noun</p>
<p><strong>Topic: LIKE</strong></p>
<p>[because] ^w:pronoun.subject (like|likes) ^object:noun [.]</p></blockquote>
<p>But we aren’t. Instead, we keep each input-pattern in it’s own rule so that each can have it’s own set of do-patterns. This way, the ‘because’ version can be treated differently. Basically, what it comes down to is this: when there is no ‘because’, we simply store an extra memory field called ‘subj’ which allows us to recall the left part of the asset operation (excluding the attribute). When we have a because, we check if the ‘subj’ field is set and if so, we store the ‘:why’ in this field. Here are the do patterns:</p>
<blockquote><p><strong>Topic: HAVE</strong></p>
<p>#bot.who = $s:ResolvePerson</p>
<p>#bot.Inverted = &#8220;#bot.Inverted because $s:InvertPerson ^verb.have:conjugate(#bot.who) a $value&#8221;</p>
<p>#bot.attribute = $value</p>
<p>#bot.who += $value</p>
<p>#bot.value = #bot.who.($value)</p>
<p>#bot.Subj = #bot.Who</p>
<p>#bot.Subj.(#bot.attribute):why = &#8220;because $s:InvertPerson ^verb.have:conjugate(#bot.who) a $value&#8221;</p>
<p><strong>Topic: LIKE</strong></p>
<p>#bot.who = $s:ResolvePerson</p>
<p>#bot.Inverted = &#8220;#bot.Inverted because $s:InvertPerson ^verb.like:conjugate(#bot.who) $value&#8221;</p>
<p>#bot.value = yes</p>
<p>#bot.attribute = $value</p>
<p>#bot.who.like.($value) = yes</p>
<p>#bot.Subj = #bot.who.like</p>
<p>#bot.Subj.(#bot.attribute):why = &#8220;because $s:InvertPerson ^verb.like:conjugate(#bot.who) $value&#8221;</p></blockquote>
<p>The full code example can be found in <em>{documents}\NND\Demos\Why_Because2.dpl</em>. As you can see, it’s a bit more code then where we originally started. Though if you look a little closer, a lot of it is boiler-plate stuff: store who, attribute, value, subj, inverted.<br />
Key advantage here: with only a few patterns we can have any type of combination: have because like, like because have,  have because have, like because like, because have, because like, have, like, have because like because have,….</p>
<p>The basic set-up is always the same, we dissect the sentence into it’s parts so that they can be reused in other parts. here’s what we need:</p>
<ul>
<li>calculate ‘who’ (the asset form of the subject part in the sentence = I, you, he,…) so we can reuse it and don’t have to recalculate it each time.</li>
<li>store the the inverted sentence, for output generation (can always be useful)</li>
<li>the attribute and value (‘color’ is the attribute of ‘yellow’). In this example, strictly speaking not really required, but other parts of the concept rely on this data, so best to get used to it.</li>
<li>subj: sometimes, the ‘who’ isn’t enough to find out where some data needs to be stored. ‘like’ (and most other verbs) is a good example of this. So if we want to get to the correct data path later on, we need to store this new path, hence the existence of ‘subj’.</li>
<li>If ‘when’, ‘where’, ‘how’,… is also present in the sentence, these are all data parts that can be stored like attribute, value or more complex sub structures.</li>
</ul>
<p>There are a few extra do-patterns required to get this working correctly. The ‘who, inverted, value, attribute, subj’,… fields of the #bot are all temporarily, that is: they are supposed to be used as mid-term memory, for as long as the current input is being processed, so that the information can be passed along from one pattern to another. This means that we need to remove the data after the input has been processed so that it wont interfere with any of the next input. This can be done from the <em>Chatbot’s properties view </em>(select the menu item view/chatbot properties, next go to the ‘<em>Do after output</em>’ tab.). here’s how it would look like for this example (can be simplified, which we will do in the next example):</p>
<blockquote><p>#bot –= Inverted</p>
<p>#bot –= Who</p>
<p>#bot –= Subj</p>
<p>#bot –= attribute</p>
<p>#bot -= value</p></blockquote>
<p>Also, in this example, I only used thesaurus variables. You can achieve similar results with regular variables, but there has to be a small change in the pattern definition for it to work properly. Everything has to do with the fact that a regular variable can only determine it’s end by what is defined after the variable, and if the pattern definition ends with a regular variable, it will collect the remainder of the input and never jump to another pattern. So we need to put something behind the variable if the ‘because xxx’ needs to be handled correctly. This can be done by moving the ‘because’ from front to back like so:</p>
<blockquote><p><strong>Topic: HAVE</strong></p>
<p>I have a $value [.]</p>
<p>I have a $value because</p>
<p><strong>Topic: LIKE</strong></p>
<p>I like $value [.]</p>
<p>I like $value because</p></blockquote>
<p>The biggest disadvantage: you need extra patterns to handle a ‘because xxx’ (not shown or included in the demo), but on the plus side, the do-patterns become a little simpler using this type of pattern definition style. For a complete example, see: <em>{Documents}\NND\Why_Because.dpl</em>.</p>
<h4>Sub topics</h4>
<p>There’s one more trick we can use to make the patterns more flexible and which will also resolve a final problem caused by using multiple rules as we did in the previous step. You see, finding a list of unrelated rules is primarily done to recognize multiple sentences in the input and not for splitting up a single sentence. That’s because all the outputs from those rules are always automatically combined. So you can’t change the order or content. This can make output management a little tricky.</p>
<p>The solution comes in the form of sub-topics or sub-rules. With this technique, it’s possible to reference a topic or rule by it’s name from within a pattern in the same or other topic. This allows us to split the ‘because’ out into a third topic and then group them all back together into a single rule, which will be the final, single result. So, on the input side, it becomes more flexible, and thanks to a special variable ‘$output’ and a switch in the <em>chatbot properties</em>, we can also take control of the output side. more on that later, lets first start with the input patterns, how do you declare sub-topics?</p>
<blockquote><p><strong>Topic: HAVE</strong></p>
<p>^s:pronoun.subject (have|has) a ^value:noun</p>
<p><strong>Topic: LIKE</strong></p>
<p>^s:pronoun.subject (like|likes) ^value:noun [.]</p>
<p><strong>Topic: BECAUSE</strong></p>
<p>because</p>
<p><strong>Topic: BECAUSEHANDLER</strong></p>
<p>~have</p>
<p>~like</p>
<p>~becauseHandler ~because ~becauseHandler</p></blockquote>
<p>The first 2 input patterns should be familiar by now, nothing has changed since before, only the ‘because’ version has been eliminated. The first new topic is also nothing special, just a single word ‘because’.  The magic happens in the last topic, which groups all the other topics together.</p>
<p>~ is used to indicate a Topic reference. It’s always followed by the name of a topic and possibly a dot followed by the name of a rule. It’s meaning is simple: include all the patterns in the rule(s) of the specified topic at the location of the reference. Here’s an example for sub rules:</p>
<blockquote><p>~have.statement because ~like.statement //presumes that topics ‘have’ and ‘like’ have a rule labeled ‘statement’</p></blockquote>
<p>Now, if you look at the last topic: ‘BECAUSEHANDLER’ you’ll notice that it only contains references to other topics, it’s a root topic. In this example, it’s purpose is to provide a place to declare the output for every topic, so we can combine things correctly + it also stores the ‘why’ relationship. Also, if you look at the last pattern, it references it’s own topic, 2 times. This is recursion and allows us to recognize a sequence of ‘becauses’ like: <em>I have x because I like y because I have z,…</em>.</p>
<p>You might be wondering why it’s the <em>BECAUSEHANDLER</em> that stores the &#8216;:why’ link and not the ‘BECAUSE’ itself. That is because at the time of the ‘Because’ pattern, the reason is not yet known (this is defined in the next pattern, which hasn’t been processed yet), so it can’t link anything up yet. To overcome this, we make certain that there is an extra rule that gets executed after every other part of the sentence: the BECAUSEHANDLER.  In other words, the ‘becauseHandler’ is a way to perform some code after all the patterns have been processed.</p>
<p><em></em><em><span style="color: #8064a2;">By the way: topic names can be edited in the ‘project view’ (select the topic, press F2 or right mouse click/rename). A topic name should be unique within the project if you want to use it as a sub-topic, otherwise it’s not that important, but the UI will always warn about duplicate names and those topics will have a red icon instead of blue. The name of a rule is always visible in the ‘description view’s title when the rule is selected.  It can be changed in the topic editor: select the entire rule (don’t click on a pattern, but on the background of the rule). With F2, you get a dialog to change the name.</span></em></p>
<p>Let’s continue with the do patterns.  For ‘have’, ‘why’ and ‘like’, they remain very similar: the patterns are used to store the inverted sentence, who (asset), the attribute, the value and the ‘subject’. In this example though, instead of directly storing it under the ‘bot’, it is stored underneath ‘mem’ so that we can move the entire result set with 1 statement later on. So here’s a small example of the ‘have’ do-patterns:</p>
<blockquote><p>#bot.mem.attribute = $value</p>
<p>#bot.mem.who = $s:ResolvePerson</p>
<p>…</p></blockquote>
<p>And so on. The really interesting stuff happens in the ‘BECAUSE’ and ‘BECAUSEHANDLER’ topics. Note that this time, we have some do-stuff in the ‘calculate’ area and others in the ‘do’ section. The major difference here: ‘Calculate’ is done, just before any of the conditions are evaluated, so this allows us to do pre-calculations that can be used in those conditions.</p>
<blockquote><p><strong>Topic BECAUSE:</strong></p>
<p>#bot.because = #bot.mem</p>
<p>#bot -= mem</p>
<p><strong>Topic BECAUSEHANDLER:</strong></p>
<p><em><strong>Rule ~becauseHandler ~because ~becauseHandler</strong></em></p>
<p><em><span style="text-decoration: underline;">Calculate:</span></em></p>
<p>$result = &#8220;because #bot.mem.Inverted&#8221;</p>
<p>$path = #bot.because.Subj.(#bot.because.attribute)</p>
<p><em><span style="text-decoration: underline;">Output when</span></em>: #($path):why == $result</p>
<p>Yes, I now #bot.because.inverted because #bot.mem.inverted\.</p>
<p><em><span style="text-decoration: underline;">else</span></em></p>
<p>I see, #bot.because.inverted because #bot.mem.inverted\.</p>
<p><strong>       </strong><em><span style="text-decoration: underline;">Do</span></em></p>
<p>#($path):why = $result</p>
<p><strong><em>Rule ~have</em></strong></p>
<p>$output</p></blockquote>
<p>First off, BECAUSE: this moves the ‘mem’ field to ‘because’ and ‘mem’ is cleaned/deleted. Basically, we store the data of the previous sentence and prepare to collect the data for the next sentence. The ‘because’ field will later on be used to link to the newly collected ‘mem’ field. Note that this rule doesn’t generate any output.</p>
<p>Secondly comes the ‘BECAUSEHANDLER’. This builds up the result that needs to be stored in ‘:why’ and calculates the location where this info needs to be stored (in $path). Before actually committing the data to memory though, a check is done to see if it was already known. If so, a different response is given compared to when it is not yet known. In the latter case, the info is also stored.</p>
<p>For the other rules (~have and ~like), we simply declare the $output variable in the output section, indicating that we want to reproduce the output of previous topic.  Note that the use of this $output variable can be controlled in the <em>chatbot’s properties view</em>. When turned off, it will function as a regular variable, and the output of all the patterns will simply be combined. This allows you to select between a simple styled bot or something more advanced.</p>
<p>Finally, as already mentioned, we still need to do some clean-up after the input. Since we have grouped all the mid-term memory in 2 fields: ‘mem’ and ‘because’, cleanup becomes a little simpler (Note: sometimes the ‘because’ part doesn’t exist, but that’s ok, nothing will be removed in this case):</p>
<blockquote><p>#bot –= Mem</p>
<p>#bot -= because</p></blockquote>
<p>I guess that’s about it for now.</p>
<p>Well, we went from simple, straight forward, fire-cracker-leveled patterns to something that’s more akin to ‘rocket science’. The combination of memory, thesaurus and sub-topics might just turn out to be a very explosive mix. I for one, am very interested to see where all this will eventually lead too… Stay tuned.</p>
 <img src="http://janbogaerts.name/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=713" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://janbogaerts.name/index.php/2011/11/08/why-because-invert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using a bot&#8217;s memory</title>
		<link>http://janbogaerts.name/index.php/2011/10/28/using-a-bots-memory/</link>
		<comments>http://janbogaerts.name/index.php/2011/10/28/using-a-bots-memory/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 15:45:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[chatbot designer]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://janbogaerts.name/index.php/2011/10/28/using-a-bots-memory/</guid>
		<description><![CDATA[A bot has 2 types of memory at it’s disposal: short and long term memory, but with some tricks, a mid term memory function can also be simulated. Short term memory Or also called ‘volatile’ as it’s content is lost in time, can be accessed through the use of variables in the patterns.  Input patterns [...]]]></description>
			<content:encoded><![CDATA[<p>A bot has 2 types of memory at it’s disposal: short and long term memory, but with some tricks, a mid term memory function can also be simulated.</p>
<h3>Short term memory</h3>
<p>Or also called ‘volatile’ as it’s content is lost in time, can be accessed through the use of variables in the patterns.  Input patterns support 2 types of variables: regular variables, which can collect any type of content and thesaurus variables, which can only collect words that are equal to or are children of the thesaurus item referenced by the variable (in other words, they are filters). Asset variables, a third type of variable, is theoretically also possible, but not yet implemented. These are also filters, like thesaurus variables, except that they filter on concrete (asset) data instead of abstract (thesaurus) data.</p>
<p>The basic usage of this short term memory is simple: to provide a mechanism for collecting values of variable parts in the input patterns so that these values can later be used in the output and do-patterns for providing a response and feeding the long term memory.</p>
<h4>Regular variables</h4>
<p>As already mentioned, regular variables can’t filter on the values that they collect, but they are optionally able to limit the number of words that they collect, either as a specific number or a range. Here are some short <em>input-pattern</em> examples:</p>
<blockquote><p>I’m called $var[.]</p>
<p>I’m called $var:1[.]</p>
<p>I’m called $var:1-3[.]</p>
<p>I’m called $var:4:CollectSpaces[.]</p>
<p>Copy $from:collectSpaces to $to:collectspaces</p></blockquote>
<p>The $var constitutes the variable (‘$’ is the variable operator, followed by the name). Every word (except spaces, if ‘CollectSpaces’ is not specified) is collected by this variable until the pattern matcher finds a word in the input that follows the variable in the pattern or until the range is fully used. This variable can then be used in the output and do-patterns of the same rule (and also from other rules, if you are certain that the input-pattern is part of the result set, which can be checked upon, more on that  later).</p>
<h4>Thesaurus variables</h4>
<p>Thesaurus variables are a special type of input variable: they provide a mechanism for filtering possible input to a sub-branch of the thesaurus. If the input can’t be found in that branch, the pattern wont be activated. So, this is a filtering mechanism. The actual value that was found, can be accessed like any regular variable, through it’s name. There is no mechanism for providing length or range values though since they have no meaning here: it’s either an exact match with a thesaurus node or it isn’t. Here are some examples:</p>
<blockquote><p>I’m called ^var:noun [.]</p>
<p>I’m called ^var:noun.name [.]</p>
<p>I’m called ^var:noun.(first name) [.]</p>
<p>I’m ^var:number years old[.]</p></blockquote>
<p>A thesaurus variable always starts with  a ‘^’ followed by it’s name. The ‘:’ indicates the start of the thesaurus path and should always be followed with a POS (part of speech). These are the supported POS values:</p>
<p><strong><em>noun, verb, adjective (or adj), adverb (or adv), article (or art), pronoun (or pron), conjunction (or conj), interjection (or inter), preposition (or prep), number, integer (or int), double</em></strong>.</p>
<p>You can stop there, which would indicate that you want any word of the specified part of speech. You can also continue the path with a ‘.’ followed by a text value (put into brackets if it’s multiple words).  This allows you to further refine the thesaurus path. Note that you don’t need to start at the root of the thesaurus that you are using, just as long as you are comfortable that it will point to a unique word within the tree (otherwise you can have multiple matches,… which might also be desirable). Note that the last 3 POS values (number, int and double) can’t have any further path specifiers, they have to stop at the POS value.</p>
<h4>Collecting multiple values</h4>
<p>It’s possible to use the same variable name multiple times in the same input pattern. This allows you to collect a list of values for the same variable. Thesaurus and regular variables can be intermixed. Here are some examples:</p>
<blockquote><p>{$name ,} and ^name:noun.name are here  //catches something like: <em>Tom, Flint and Warner are here</em></p></blockquote>
<p>Note that there is a difference when a regular variable collects multiple words at a single location compared to when it collects single words at multiple locations in the pattern. When a single location collects multiple words, this group of words is combined into a compound word (as in ‘baby gear’), but when words are collected at multiple locations, a list is created. This list can later-on (in the long term memory) be labeled as AND, OR or LIST (unspecified).</p>
<h4>Using short term memory</h4>
<p>Up until now, we’ve only been talking about how to collect the values for the short term memory.  Of course, there’s no point in doing that unless you can actually do something with these values. That’s done in the output and do-patterns. As already mentioned, you access the content through the variable names. Here are some output-pattern examples:</p>
<blockquote><p>Ok, I see, your name is $var\.</p>
<p>So you are $var, nice to meet you!</p>
<p>So, you can $verb:Infinitive, can you?</p>
<p>I see, $name:interleaf(&#8220;\, &#8220;, &#8221; and &#8220;)</p></blockquote>
<p>At it’s most basic form you specify the ‘$’ operator followed by the name of the variable that you want to render. Note that you should always use the ‘$’ operator while rendering, even if the value was collected using a thesaurus variable ( ^ ). This is because the ^ operator is used to access the long-term, abstract memory (the thesaurus data itself).</p>
<p>Rendering the value as it was collected, is useful but often we want to do a little more, sometimes we need to do some kind of change or transformation to the values, like conjugating a verb, get the plural of a noun or find the attribute for the value (see later),…  This is done through functions that you define in the path. A function starts with a ‘:’ followed by the name of the function (a list of all the available functions will come shortly) and optionally a list of arguments for the function, specified between brackets and separated by a ‘,’. Note: if you use the ‘,’ sign as an argument value, it must always be escaped with a \ Also, if you need to preserve spaces, the argument should be placed between brackets (as in the last example).</p>
<h3>Long term memory</h3>
<p>The second major type of memory that’s available to the bot is used to store and retrieve values so that they can cross the boundary of the single-shot input/response system, in other words: <em>long term memory</em>. Currently, there are 2 types: <a href="http://janbogaerts.name/index.php/2010/07/12/objects-and-assets-abstract-and-concrete/" target="_blank">a thesaurus structure for storing abstract information and assets which maintain concrete knowledge</a>.  Typically, you use this data to compare against short-term variables, render previously stored data or store newly acquired knowledge.</p>
<h4>The thesaurus</h4>
<p>As already mentioned, thesaurus variables are used in the input-patterns so that the valid content for a variable can be filtered.  When the ‘^’ operator is used in output, conditional or do patterns however, it behaves a little bit different: it becomes a value generator instead of collector.  Consider the following output patterns:</p>
<blockquote><p>We are in ^noun.month[$time:month-1]</p>
<p>I like ^noun.food.(Italian food):random</p>
<p>I ^verb.be:conjugate(#bot) trying something complicated   //render: I am trying something complicate</p></blockquote>
<p>As you can see, a thesaurus output-path contains a mix of statics and functions which eventually result into 0, 1 or 2 values. Because they render values and don’t collect it, no name is required. You can use the [] operator to select a child at a specific index position, like in the first example, which is used the generate the name of the month instead of a number. Note that the index is 0 based. In case that a static path item contains multiple words (like ‘Italian food’), use () brackets to group them. Also, if there are no values found for the path, any spaces that follow it in the output are stripped.</p>
<p>You can also store new data in the thesaurus. This is done in the calculation or do-sections. There are basically 2 operations that you can do at 2 different levels: you can add or remove values either as thesaurus children or as conjugations/references. To explain the difference between children and conjugations or references, take the following examples and how they are stored:</p>
<table width="620" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="326">A house is a building</td>
<td valign="top" width="292">^noun.building += house</td>
</tr>
<tr>
<td valign="top" width="326">The plural of bird is birds</td>
<td valign="top" width="292">^noun.bird-&gt;plural = birds</td>
</tr>
<tr>
<td valign="top" width="326">The opposite of good is bad</td>
<td valign="top" width="292">^adj.good-&gt;opposite = bad</td>
</tr>
<tr>
<td valign="top" width="326">seagulls are a type of the singular of birds</td>
<td valign="top" width="292">^noun.birds-&gt;singular += seagull</td>
</tr>
<tr>
<td valign="top" width="326">The superlative of the opposite of good is worst</td>
<td valign="top" width="292">^adj.good-&gt;opposite-&gt;superlative = worst</td>
</tr>
</tbody>
</table>
<p>In the first example, we are declaring a child relationship: house is a building. If you have done any coding before, the syntax might be vaguely familiar: the left part of the statement contains the thesaurus path, the ‘+=’ operator to indicate that we want to create an ‘is child’ relationship, and on the right-side comes the value that needs to be stored. This could be a variable reference, an asset, another thesaurus path,….</p>
<p>The second and third examples look identical and for all intent and purpose, they are. The only difference is on the inside: in the first example ‘plural’ is a known conjugation form, ‘opposite’ is not. The statement used for storing this information, is a little bit different. First of, the thesaurus path ends with a ‘-&gt;’ followed by the name of the relationship that you would like to edit. Next, we use the ‘=’ assign operator instead of ‘+=’ to indicate that we want to change the relationship value.</p>
<p>The 2 last examples demonstrate what happens when you use the –&gt; operator together with the += assignment or when you use multiple –&gt; operators. When combining += with –&gt;, you will first calculate the full result of the left side.  So in our example, we first take the singular value of ‘birds’, then we add a child to this result, which is ‘bird’. A similar thing happens when you use multiple –&gt; operators: the value is calculated.</p>
<p>Except for the POS value at the start of the path, every other item in a thesaurus path can be a static, a variable reference, an asset path or another thesaurus path. This allows for tremendous flexibility in the way that you store data. We could generalize some of the previous statements like this:</p>
<blockquote><p>^noun.building += $value</p>
<p>^noun.($singular)-&gt;plural = $value</p>
<p>^adj.good-&gt;($relationship) = $value</p></blockquote>
<p>Removing values from the thesaurus is done using the ‘-=’ operator or by assigning to the ‘null’ value. Like with storing, all parts can be static or variable. This is probably best explained with some examples:</p>
<table width="368" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="186">A house is not a building</td>
<td valign="top" width="180">^noun.building -= house</td>
</tr>
<tr>
<td valign="top" width="186">Bird has no plural</td>
<td valign="top" width="180">^noun.bird-&gt;plural = null</td>
</tr>
<tr>
<td valign="top" width="186">A $value is not a $node [.]</td>
<td valign="top" width="180">^noun.($node) -= $value</td>
</tr>
</tbody>
</table>
<h4>Assets</h4>
<p>As already mentioned, assets could theoretically also be used in the input, but that’s not yet supported. If someone has a need for this, let me know, it’s not that tremendously difficult to add, it just creates a little more overhead.</p>
<p>Anyway, like thesaurus paths, asset paths can be used in output, do and conditional patterns. They are declared in much the same way as thesaurus paths by using the ‘.’ (dot) or ‘:’ (function) operators, except that they start with a # and ‘–&gt;’ (links) are not supported.  For the thesaurus path, the ‘.’ (dot) operator selected a child node, for assets, this selects an attribute value. Here are a few output examples:</p>
<blockquote><p>My name is #bot.name</p>
<p>your children are called #user.child.name:interleaf(“\, “, “ and “)</p>
<p>a book is made of $(#(^noun.book).component.name):interleaf(“\, “, “ and “)</p></blockquote>
<p><em>Bot</em> and <em>User</em> are hardcoded assets and refer to <em>me</em> and <em>you</em> respectively, from the bot’s point of view. In the third example, the first value in the asset path, is actually a thesaurus path. This results in concrete information about abstract data (a book is made of paper, ink, glue,…).</p>
<p>Also in the last example, the entire asset is the first value in a normal variable path, because an asset path will always calculate it’s result based on 1 value, if the previous path item resulted in multiple values (like ‘component), the next part of the path is calculated as if there was only 1 result (internally, a split is done), and only at the end of the path, all results are joined. This doesn’t work for ‘:interleaf’, it expects a list of values to combine. A variable path can do this, hence this construct.</p>
<p>To store asset data, the = (assign), += (assign add), != (assign not) and !+= (assign add not) operators are used. Removing data is done with the –= (assign remove) operator.   Take the following examples (input statement to the left, how to store/remove it to the right):</p>
<table width="431" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="214">My eyes are blue</td>
<td valign="top" width="215">#user.eye.color = blue</td>
</tr>
<tr>
<td valign="top" width="214">I have a dog.</td>
<td valign="top" width="215">#user += dog</td>
</tr>
<tr>
<td valign="top" width="214">My dog’s name is not doggy</td>
<td valign="top" width="215">#user.dog.name != doggy</td>
</tr>
<tr>
<td valign="top" width="214">I don’t have a tiger</td>
<td valign="top" width="215">#user !+= tiger</td>
</tr>
<tr>
<td valign="top" width="214">I have big blue eyes</td>
<td valign="top" width="215">#user.eye.color:extra.size = big</td>
</tr>
<tr>
<td valign="top" width="214">My eyes are also brown</td>
<td valign="top" width="215">#user.eye.color &amp;= brown</td>
</tr>
<tr>
<td valign="top" width="214">my eyes are brown or blue</td>
<td valign="top" width="215">#user.eye.color = brown<br />
#user.eye.color |= blue</td>
</tr>
<tr>
<td valign="top" width="214">my eyes are brown, blue</td>
<td valign="top" width="215">#user.eye.color = blue<br />
#user.eye.color ;= blue</td>
</tr>
<tr>
<td valign="top" width="214">Remove my dog</td>
<td valign="top" width="215">#user –= dog</td>
</tr>
<tr>
<td valign="top" width="214">remove my eye color</td>
<td valign="top" width="215">#user.eye –= color</td>
</tr>
</tbody>
</table>
<p>When you use the ‘=’ (assign) operator, you declare an ‘is’ relationship: ‘color’ becomes the attribute, ‘blue’ the value. Since ‘blue’ is not an asset, but just a word, we have a terminator: blue can’t have any more children.  But, there is a way to cross this border, by using the ‘:extra’ function, as in the 5th example.<br />
If instead, you want to declare that something <em>is not</em> y, you can use the not-assign operator (!=). This allows you to still store the information that something <em>is not</em>. Be careful though, there is a thin line between <em>being</em> and <em>not being</em>, if you don’t check on this in the conditions, you might say that something is, while it isn’t (sounds familiar?).</p>
<p>The ‘+=’ or assign-add operator is used to create ‘has’ relationships, like in the second example. The major difference with the first one is that ‘dog’ becomes the attribute and the value becomes a new asset that will represent the dog. There is also the not version:  !+= which is used to indicate that the asset doesn’t have something.</p>
<p>If you want to create a list of values, you can use either the ‘;=’, ‘|=’ or ‘&amp;=’ operators. The first one creates (or adds to) a generic list, the second is for OR lists and the last for AND lists. The generic list operator can add to any type of list without modifying it’s type. The |= and &amp;= operators will change a generic list to OR and AND respectively. When you try to add an item to an OR list with an AND operator, you create a new list object that contains the original OR list and the newly added item. the same goes for an OR operator with an AND list.</p>
<p>Finally, you can also actually remove an attribute value. This is done with the ‘-=’ operator. The  right-side should be the name of the attribute that you want to remove. The value that is removed get’s cleaned up automatically, so if the value was another asset which isn’t referenced anymore after the remove, the entire asset will be destroyed. (Removing items from a list has to be done with the ‘:Remove’ function.)</p>
<p>As already mentioned, every asset value can always use the ‘:extra’ function to get to a sub-asset. There are a few other functions worth mentioning which allow you to expand the dataset that the asset can store. These are used to declare things like when, where, why, how, amount,… Functions are:</p>
<table width="767" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="67">:why</td>
<td valign="top" width="415">provides access to the ‘reason’ path</td>
<td valign="top" width="283">#user.dog:why = “likes dogs”</td>
</tr>
<tr>
<td valign="top" width="67">:when</td>
<td valign="top" width="415">provides access to the ‘time’ path</td>
<td valign="top" width="283">#user.dog:when = “10 years ago”</td>
</tr>
<tr>
<td valign="top" width="67">:where</td>
<td valign="top" width="415">provides access to the ‘location’ path</td>
<td valign="top" width="283">#user:where.preposition = in<br />
#user:where.object = chair<br />
or #user:where = “in the chair”</td>
</tr>
<tr>
<td valign="top" width="67">:how</td>
<td valign="top" width="415">provides access to the ‘method’ path</td>
<td valign="top" width="283">#user.dog:how = received from some friends who had a bit of an accident</td>
</tr>
<tr>
<td valign="top" width="67">:amount</td>
<td valign="top" width="415">Allows you to specify that the same value should be counted multiple times. When the value is an asset, it indicates how many identical assets should be counted.</td>
<td valign="top" width="283">#User.hand:count = 2</td>
</tr>
<tr>
<td valign="top" width="67">:who</td>
<td valign="top" width="415">provides access to the ‘persons’ path</td>
<td valign="top" width="283">#user.see:who = man  //user sees a man</td>
</tr>
<tr>
<td valign="top" width="67">:what</td>
<td valign="top" width="415">provides access to the ‘objects’ path</td>
<td valign="top" width="283">#user.eat:what = food  //user eats food</td>
</tr>
<tr>
<td valign="top" width="67">:then</td>
<td valign="top" width="415">provides access to the causality path</td>
<td valign="top" width="283">#user.eat:then.who = #user<br />
#user.eat:then.attribute = state<br />
#user.eat:then.value != hungry<br />
or #user.eat:then = “I’m not hungry”</td>
</tr>
</tbody>
</table>
<h3>Mid term memory</h3>
<p>Some functions, like the ‘:attribute’ function (which is able to extract, for instance,  ‘color’ from ‘blue’, or ‘name’ from ‘Jan), make use of context, if it is declared. This context is usually a list of asset paths that point to some memory region of the bot. The idea is that, together with a response, you also generate the meaning of what was said and store this information in the asset that you declared as <em>context</em>. If you refresh this context on each run, you effectively have simulated mid term memory.</p>
<p>The basic setup for using mid term memory consist out of:</p>
<ul>
<li>a global context declaration so that the system knows where to go look for contextual info.</li>
<li>some global do-after-each-statement patterns. These are responsible for erasing the previously collected data and possibly creating an echo.</li>
<li>some global do-on-startup patterns which will remove any data from the previous run.</li>
<li>do-patterns on each rule to actually collect the knowledge about what is being said.</li>
</ul>
<blockquote><p>Context:</p>
<p>#bot.memory.subject</p>
<p>#bot.memory.attribute</p>
<p>#bot.memory.object</p>
<p>#bot.PrevMem.subject</p>
<p>#bot.PrevMem.attribute</p>
<p>#bot.PrevMem.object</p>
<p>&nbsp;</p>
<p>Do after output:</p>
<p>#bot.prevmem = #bot.memory</p>
<p>#bot –= memory</p>
<p>&nbsp;</p>
<p>do on startup:</p>
<p>#bot –= memory</p>
<p>#bot –= prevmem</p>
<p>&nbsp;</p>
<p>on pattern (example pattern = I like $value [.])</p>
<p>#bot.memory.subject = #user</p>
<p>#bot.memory.attribute = like</p>
<p>#bot.memory.object = $value</p>
<p>&nbsp;</p></blockquote>
<p>Mid term memory also becomes very useful once you start working with recursive sub rules/topics. This technique allows you to rebuild the extracted data.</p>
<h3>Patterns</h3>
<p>All the different types of patterns (input, output, conditional) could also be considered as a form of long term memory. Internally, they are stored in exactly the same manner as all the other data. As such, they can also be manipulated in a similar manner as the other long term data. Although, at the time of writing, there is still limited support for this.  More on that to come.</p>
 <img src="http://janbogaerts.name/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=696" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://janbogaerts.name/index.php/2011/10/28/using-a-bots-memory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Complete the sequence</title>
		<link>http://janbogaerts.name/index.php/2011/08/21/complete-the-sequence/</link>
		<comments>http://janbogaerts.name/index.php/2011/08/21/complete-the-sequence/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 18:51:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Screencasts]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[Updates]]></category>
		<category><![CDATA[code editor]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[screencasts]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://janbogaerts.name/index.php/2011/08/21/complete-the-sequence/</guid>
		<description><![CDATA[Check out this first ‘AI’ feature that can be done using only 1 rule and, if needed, some thesaurus lookups. I’ve been having a huge smile on my face all day For the interested, here’s a screenshot of the rule that enables this trick (click to enlarge): The important bit is the :complete after the [...]]]></description>
			<content:encoded><![CDATA[<p>Check out this first ‘AI’ feature that can be done using only 1 rule and, if needed, some thesaurus lookups.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:23fe3611-3a56-4d5f-a4c2-89cfdb3d7461" class="wlWriterEditableSmartContent">
<div id="3a266003-6e95-417d-bc23-e240a45fe015" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=fG9MfdKOhes&amp;hd=1&amp;fs=1" target="_new"><img src="http://janbogaerts.name/wp-content/uploads/2011/08/videoef38c5df47b83.jpg" style="border-style: none" galleryimg="no" onload="var downlevelDiv = document.getElementById('3a266003-6e95-417d-bc23-e240a45fe015'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;448\&quot; height=\&quot;252\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/fG9MfdKOhes?hl=en&amp;hd=1\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/fG9MfdKOhes?hl=en&amp;hd=1\&quot; type=\&quot;application/x-shockwave-flash\&quot; width=\&quot;448\&quot; height=\&quot;252\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
<p>I’ve been having a huge smile on my face all day <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-laughingoutloud" alt="Laughing out loud" src="http://janbogaerts.name/wp-content/uploads/2011/08/wlEmoticon-laughingoutloud.png" /></p>
<p>For the interested, here’s a screenshot of the rule that enables this trick (click to enlarge):</p>
<p><a href="http://janbogaerts.name/wp-content/uploads/2011/08/Capture.jpg"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Capture" border="0" alt="Capture" src="http://janbogaerts.name/wp-content/uploads/2011/08/Capture_thumb.jpg" width="553" height="89" /></a></p>
<p>The important bit is the <em>:complete</em> after the variable $ToComp which performs the calculation.</p>
<p>Here’s another screencast that shows what’s happening behind the scenes (basically, it’s a walkthrough of the neural code in the designer):</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:cc68f9f7-e5e1-4d8c-b663-69cff35ba00d" class="wlWriterEditableSmartContent">
<div id="947237f8-5554-411f-8eb9-c00912acab47" style="margin: 0px; padding: 0px; display: inline;">
<div><a href="http://www.youtube.com/watch?v=JKx3JWLj3S4&amp;hd=1&amp;fs=1" target="_new"><img src="http://janbogaerts.name/wp-content/uploads/2011/08/video49ca0469f5584.jpg" style="border-style: none" galleryimg="no" onload="var downlevelDiv = document.getElementById('947237f8-5554-411f-8eb9-c00912acab47'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;448\&quot; height=\&quot;252\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/JKx3JWLj3S4?hl=en&amp;hd=1\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;http://www.youtube.com/v/JKx3JWLj3S4?hl=en&amp;hd=1\&quot; type=\&quot;application/x-shockwave-flash\&quot; width=\&quot;448\&quot; height=\&quot;252\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div>
</div>
</div>
 <img src="http://janbogaerts.name/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=631" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://janbogaerts.name/index.php/2011/08/21/complete-the-sequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

