5

I have a javascript chat bot where a person can type into an input box any question they like and hope to get an accurate answer. I can do this but I know I'm going about this all wrong because I don't know what position the number will appear in the sentence. If a person types in exactly:

what's the square root of 5 this works fine.

If he types in things like this it doesn't.

what is the square root of the 5

the square root of 5 is what

do you know what the square root of 5 is

etc

I need to be able to determine where the number appears in the sentence then do the calculation from there. Note the line below is part of a bigger working chatbot. In the line below I'm just trying to be able to answer any square root question regardless of where the number appears in the sentence. I also know there are many pitfalls with an open ended input box where a person can type anything such as spelling errors etc. This is just for entertainment not a serious scientific project. :)

if(
    (word[0]=="what's") &&
    (word[1]=="the") &&
    (word[2]=="square") &&
    (word[3]=="root") &&
    (word [4]=="of") &&
    (input.search(/\d{1,10}/)!=-1) &&
    (num_of_words==6)
){        
    var root= word[5];
    if(root<0){ 
        document.result.result.value = "The square root of a negative number is not possible.";
    }else{
         word[5] = Math.sqrt(root);
         word[5] = Math.round(word[5]*100)/100 
         document.result.result.value = "The square root of "+ root +" is "+ word[5] +"."; 
    }
    return true;
}

Just to be clear the bot is written using "If statemments" for a reason. If the input in this case doesn't include the words "what" and "square root" and "some number" the line doesn't trigger and is answered further down by the bot with a generic "I don't know type of response". So I'm hoping any answer will fit the format I am using. Be kind, I'm new here. I like making bots but I'm not much of a programmer. Thanks.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
mister movie
  • 103
  • 1
  • 10

2 Answers2

2

You can do this using a regular expression.

"What is the sqrt of 5?".match(/\d+/g);
["5"]

The output is an array containing all of the numbers found in the string. If you have more than one, like "Is the square root of 100 10?" then it will be

"Is the square root of 100 10?".match(/\d+/g);
["100", "10"]

so you can pick what number you want to use in your script.

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • `"Is the meaning of life 42?"` --> `6.48` – Derek 朕會功夫 Jun 07 '12 at 23:57
  • That code will only give you the number, as the original question asked. It seems like `mister movie` already has a way of handling the _kind_ of question asked. – sachleen Jun 08 '12 at 00:04
  • I think this point might not be clear. The "If Statement" screens out all the unnecessary information. If the information isn't present then if statement never triggers. So I know there is going to be a number in the if statement. I was hoping something like var root = whatever that number is. Then perfom a simple calculation on that number such as sum = Math.sqrt(root); Looking at the original post I know word[5] is the number. However if phrased different the number maybe word[3] word[4] etc. Is there a way to search the input. Find where the number falls and calculator accordingly. – mister movie Jun 08 '12 at 16:39
  • Yes, my code does exactly that. Do `var numbersInString = "What is the sqrt of 5?".match(/\d+/g);` and you get an array of numbers found in the string. then `numbersInString[0]` gives you the first number. – sachleen Jun 08 '12 at 16:46
  • Thanks, I see what you are saying now. – mister movie Jun 08 '12 at 19:40
1

You will need to use regular expressions, if you do not know what they are you should look them up as it would take too long to explain them in this response. Here is a useful website for regular expressions in JavaScript https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions.

Assuming that you know regular expressions you should first search for numbers and store all the numbers that you find, if no numbers are found print out an error message. As an added note, you may what to consider searching for mathematical constants such as pi or e. This should work

nums = someString./\d+|pi|e/gi

The next part is going to be hard but to boil it down to is core concept, you need to look for key words such as 'square root', 'times', or 'plus'. You should do this word by word going left to right. For example if a user inputs

What is 5 plus 3 minus 8?

you should detect the plus before the minus, while if this is inputted

What is 5 minus 3 plus 8?

You should detect the minus before the plus.

For operations that uses two numbers you need to take the first two numbers that you found and do the operation and replace the two numbers with the result. I am trying to use reverse polish notation if you do not quite understand what I am trying to do, look it up if do not know what it is.

I hope I understood your question correctly and provided some help to coming to a solution because what you asked is very hard but seems like fun. Good luck. Also as a warning I am not considering order of operations in my response.

island_hopper
  • 285
  • 1
  • 2
  • 8