User avatar
Leslie
Lead Software Architect
Posts: 353
Joined: Fri Sep 14, 2012 12:20 pm
Contact: Website

Your first Natural Language Application

Thu Oct 02, 2014 5:26 pm

We will now see how you can integrate Syn Engine to your application.  Before you start make sure you have Visual Studio ( for Windows ) or MonoDevelop ( if on Linux ).

We are going to assume that you are working on Visual Studio ( pardon me Linux users ) and this tutorial will help you understand how to integrate the Engine in your desktop application. Though Syn Engine has different versions for different platforms we will use Syn Engine 2.0 for .Net Framework 4.0.

We will create a very simple application that can Add and Subtract numbers and also check if a number is greater than the specified number using just textual input.

To better understand what we are going to create you can even Download a Simple Demo Application
  • Create a simple Windows Form Application project and name it SimpleDemo
  • Now rename the Form to SynForm ( for the sake of this tutorial )
  • Next add reference to Syn Engine class library and make sure you have a license key to initialize the Engine. If not then Get it Here
  • Time to import some important namespaces add the following to the first section of your Code.

CSharp Code

using Syn.Engine;
using Syn.Engine.Data;
using Syn.Engine.Events;
Next inside the SynForm class declaration add the following line.

CSharp Code

//Engine object to be initialized by the user.
SynEngine Engine;
  • Add 2 Buttons to the form. Rename button1 to evaluateButton and rename button2 to initializeButton
  • Next add 2 TextBoxes and rename textBox1 to inputBox and rename textBox2 to outputBox
  • Now double click the initializeButton add the code below. Thats all you got to do to add a License Key.

CSharp Code

string licenseKeyFromFile = File.ReadAllText("License.key");
Engine = new SynEngine(licenseKeyFromFile);
Engine.Initialize();

Engine.MainCaller.ResponseReceived += MainCaller_ResponseReceive
The last line in the above code is to add a handler to the ResponseReceived event. This event is raised when a response is generated for a command that was sent to the Engine. We haven't created the MainCaller_ResponseReceive function yet. So add the following code inside the SynForm class.

CSharp Code

private void MainCaller_ResponseReceived(object sender, ResponseReceivedEventArgs e)
{
if (InvokeRequired)
{
this.Invoke(new Action<object, ResponseReceivedEventArgs>(MainCaller_ResponseReceived), new object[] { sender, e });
return;
}
//If invoke isnt required then add response to outputBox
else
{
outputBox.Text = outputBox.Text + e.Response.Message + Environment.NewLine;
}
}
  • The InvokeRequired approach is to manage Cross Thread activity.
  • Now double click the evaluateButton and add the following code.

CSharp Code

Engine.MainCaller.Message = inputBox.Text.ToLower();
Engine.CreateResponse(Engine.MainCaller);
  • Syn Engine has a default internal Caller. A Caller is an entity that sends messages to the Engine. A Caller has an unique ID. The MainCaller is typically the main User. In the above code the Message for the Main Caller is set to the text in the inputBox  and then the Caller ( with the message ) is sent to the Engine as an argument for the CreateResponse function.
  • Next we create 3 SynData classes.  SynData is a Computable Data class that is used as an arguments for functions. For our tutorial we will create 3 non-computable SynData types as they are going to be used as Triggers for our Add,Subtract and IsGreaterThan function. Add the following code inside SynForm Class.

CSharp Code

public class MathAddTerm : SynData
{
public MathAddTerm()
{
//The word "add" will result in the creation of this Data type during runtime.
this.Data.AddPattern("add");
}
}
public class MathSubtractTerm : SynData
{
//The word "subtract" will result in the creation of this Data type during runtime.
public MathSubtractTerm()
{
this.Data.AddPattern("subtract");
}
}
public class MathGreaterThanTerm: SynData
{
//The sentence "greater than" will result in the creation of this Data type during runtime.
public MathGreaterThanTerm()
{
this.Data.AddPattern("greater than");
}
}
  • Alright so the Data types are ready. But what will they do ? Well as you can see in the above code we have added the "add" and "subtract" pattern to them. This means whenever the user inputs Add or Subtract the above SynData types should be added to the Caller's Context. A Caller's Context is where most of the Runtime-generated Computable Data are held. The Engine uses the Context to determine what is the current state of the Caller. Now we will have to tell the Engine that these 2 Data types do exist and therefore lets go back to the initializeButton and append the following code

CSharp Code

Engine.AddAutoParser(new MathAddTerm());
Engine.AddAutoParser(new MathSubtractTerm());
Engine.AddAutoParser(new MathGreaterThanTerm());
  • Now that our Add and Subtract patterns have been added to the Engine. Its time to create the signature for these functions. Functions in Syn Engine need to be Grouped inside what is called a SynGear. A Gear is a container for function signatures and declarations. Lets create a Gear with 3 functions namely Add,Subtract and IsGreaterThan. For the Engine to detect the functions we will make sure they are declared with the Public access modifier as Private functions do not interact with the Engine and are completely ignored at run-time.

CSharp Code

public class MathGear : SynGear
{
//Function with signaure "Add [number] [number]" Example Sentence "Add 1 and 2"
public IntegerData AddNumber(MathAddTerm cmd, IntegerData firstNumber, IntegerData secondNumber)
{
IntegerData toReturn = new IntegerData();
toReturn.Value = firstNumber.Value + secondNumber.Value;
toReturn.Data.Response.Message = "The result of addition is " + toReturn.Value.ToString();
return toReturn;
}
//Function with signaure "Subtract [number] [number]" Example Sentence "Subtract 1 and 2"
public IntegerData SubtractNumber(MathSubtractTerm cmd, IntegerData firstNumber, IntegerData secondNumber)
{
IntegerData toReturn = new IntegerData();
toReturn.Value = firstNumber.Value - secondNumber.Value;
toReturn.Data.Response.Message = "The result of subtraction is " + toReturn.Value.ToString();
return toReturn;
}
//Function with signaure "Greater Than [number] [number]" Example Sentence "5445 Is Greater Than 563"
//This function is also used to determine a Predicate.
public BooleanData IsGreaterThan(MathGreaterThanTerm cmd, IntegerData firstNumber, IntegerData secondNumber)
{
BooleanData toReturn = new BooleanData();
toReturn.Value = firstNumber.Value > secondNumber.Value;
if (toReturn.Value == true)
{
toReturn.Data.Response.Message = "Yes " + firstNumber.Value.ToString() + " is greater than " + secondNumber.Value.ToString();
}
else
{
toReturn.Data.Response.Message = "No " + firstNumber.Value.ToString() + " is Not greater than " + secondNumber.Value.ToString();
}

return toReturn;
}
}
  • The above is a SynGear class containing 3 functions. Note that the first parameter of the functions take the SynData types that we created. These are by convention called "Terms" and are required to Trigger a function. Every function takes 2 IntegerData as parameters. We do not have to create this type as its already defined in the Syn.Engine.Data namespace and can be used throughout application development. Lets add this Gear to the Engine so that the Engine will be able to use the Gear to generate and execute functions we created within the Gear. Append the following code to the initializeButton's clicked event

CSharp Code

Engine.AddGear(new MathGear());
So thats it. Lets recap ( if you don't mind).  We first created SynEngine object and initialized it by providing the License Key. Then we added handler to the ResponseRecieved event to catch any response generated by the Engine. We created 3 Data types that tell the Engine which all functions we are referring to. Later we created a Gear with 3 functions Add,Subtract and IsGreaterThan and used our declared Data types to Trigger those functions.

Testing The Application

Its time to test the application for this we will use the following commands to test if our functions are doing what they are supposed to do
  • Add 1 and 5
  • Subtract 8 and 3
  • 6 Is Greater Than 8
Lets try something else.
  • Add 1 4 and 6
  • Subtract 5 8 7
  • Add 1 and 5 and then Subtract 3
  • Subtract 5 and 6 and then Add 6 to it
  • If 5 Is Greater Than 4 then Add 5 and 6
  • If 5 Is Greater Than 6 then Subtract 4 and 3
Note: You didn't create any signature for the above commands but they work because Syn Engine is also a Natural Language Compiler and can intelligently sequence and execute functions (many but not all).

Return to “Archived (Old Posts)”

Who is online

Users browsing this forum: No registered users and 2 guests