by JonC » Wed Oct 05, 2011 2:27 pm
Okay, here are a few ideas.
We are giong to use multiple rule outputs for this I think, there is also a c# method.
Let's say you have a primary rule called "Can you do Maths".
The user asks that question, the 'bot replies:
"Yes, sure, give me a mathematical question."
There is then a child rule that will accept the question input (simplest thing might be to use "*" for the input which means "anything").
Now the user will input something: "1+2=", or maybe they say "1+2" or maybe they are a bit nervous and just type "1".
I am going to assume that you read my first reply in "Discussion" and have looked up info on MyMath and even found the vcm code for it (if not download Julia _csharp_supplement).
Let me also introduce you to a very useful bit of code (it's c# in fact) that we are going to use as a condition is some rule outputs...
[variablename].IndexOf("character string")
Now, if your user is "experienced" they will know that they can enter the whole sum in one go.
This means that the vars-variable [_input] (which is exactly what is says) will contain (in my example) 1+2=
Thus we search [_input] as follows:
vars["_input"].IndexOf("=") !=-1
What this means is I'm searching [_input] to see whether or not the input contains an "=". In fact what the line above means is "is not false" - i.e. we are checking to see if there is an equals anywhere in the output. We could test for the "=" at the end of the input, but I'll keep it simple for the moment.
If this condition is satisifed (i.e. the user has entered a complete sum) we get the 'bot to <send...> to a child rule that actions MyMaths and prints the result.
Thus one rule output might be <send GetMathsResult> with the condition as above.
What if the .IndexOf("=") search is false? i.e. there is no "=" in the input?
There are two basic possibilities:
1. The user simply omitted the "=" sign
2. The user gave a genuinely imcomplete "sum" - e.g. "1+"
Let's test for the incomplete sum in the same rule with another output.
In any arithmetic operation we will have the sequence number-operand-number (If you expand this to more esoteric maths you will have to look at this more closely). What this means is that the operand (here +,-,*,/) is not the final character of the [_input].
Thus we will assume that anything after a found operand is a number (at least at this stage).
Next let me introduce you to another useful bit of code:
variable.Length().
this is a way of measuring the length of a string (amongst other things!).
Now we can work out whether or not the operand is at the end of [_input] with the condition:
vars["_input"].IndexOf("+") == vars["_input"].Length().
(this is, obviously just one of the four conditions we would need to write. We can string conditions together using && for logical AND and || for logical OR.)
If this is true we know we have an incomplete sum and can <send...> to a rule to prompt for the other number.
In the same rule again, in another output we also test this:
vars["_input"].IndexOf("+") == -1 (again one of four conditions tested.)
If this is true we know that no operand has been supplied, so we assume that the only user input was a number and s/he is agonising about the rest of the question.
We can then then ask "which operand do you want to use" (etc) and send to another child rule that collects that and then (in turn) prompts for the next number etc.
Finally we have the possibility that the input is "1+2" and the only thing missing is the "=" sign.
At this point let me acquaint you with a useful way that the Verbot operates:
The Verbot considers a "conditionally true" output (or input) [i.e. an input/output with a condition that has been satisfied] to be "more true" than one with no condition at all.
We will use this fact for the last rule output. If none of the above conditions are true we assume (as said) that only the "=" is missing, so this output would be something like: "please use an '=' to end your input" <send GetMathsResult>.
______________________
I hope that gives you a start.
Please note - the above is not a complete solution I have concentrated on handling the [_input] rather than dealing with how to formulate the data for MyMaths.