I am developing a chat bot with Node.Js. I am trying to find efficient way to parsing sentence.

what do you think about humanity
-> ba bla bla.

what do you think about cats
->I love cats

Right now I am using the intersection technique, but I don't think it's efficient:

var sentence = sentence.split(",")
var inters = intersectionOfArrays(sentence, ["what", "do", "you", "think", "about"])
if(inters.length >= 3) { //we have common words 
    keyword = extractionOfarrays(sentence,["what", "do", "you", "think", "about"]) // get question word (humanity, cats etc)
    if(keyword == "cats") 
       responseToServer("I love cats")
    if(keyword == "humanity") 
       responseToServer("bla bla bla")

Is this technique bad? I want to answer thousands of questions so I need to know what is the best method for doing some "AI" magic.


2 Answers 2


From a personal standpoint, I think this method is fine. You might want to re-work it a little. I'd recommend creating an array with possible intersections, and looping through that array and checking user input against those. For example:

var intersections = [
    ["what", "do", "you", "think", "about"],
    ["what", "is", "your"],

Finally, I'd recommend having something that allows for multiple keywords, rather than one. In addition to this, you may not want to be checking the length of intersections based on a constant, in this case, three.

Anyways, if there's anything else that you want me to cover, just mention it in the comments, and I'll see what I can do.

  • \$\begingroup\$ Hey, thanks for reply. I searched and found AIML, it's more suitable for me. (en.wikipedia.org/wiki/AIML). \$\endgroup\$
    – Lazy
    Commented Jul 5, 2015 at 16:51

A few things to point out:

["what", "do", "you", "think", "about"]

You should move this to an external array.

Rather than == matching responses in individual if-else loops: move the responses to a dictionary.

var responseDict = [
      "cats" : "I love cats"
    , "humanity": "bla bla bla"

Then, you can use the dictionary to build the response to fit into responseToServer().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.