Faizan
Faizan

Reputation: 1898

Javascript/jQuery check if string contains a word or occurrences of one of the optional words

I have a simple basic question and I am stuck about which approach should I take in this.

I am creating a chat bot. So each time a real human user types a message, I want detect which words the user typed in the message and then based on his/her words, the bot will reply particularly.

 // A string containing user message
        var UserMsg = data.message;
////Means if ANY ONE of this words are contained in user message then I will tell the bot to respond to the greeting message.
    if UserMsg.contains("Hi", "Hello", "Hey", "Morning"); 

    var welcomeArray = ['Hey!!', 'Hi', 'Hello :)']; 

 //gets a random welcome msg to respond
      var welcomeMsg = welcomeArray[Math.floor(Math.random() * welcomeArray.length)];


      if(data.message=='Hi') //if user says Hi or hello hey etc
       socket.emit('message', 'welcomeMsg'); //Bot will respond by one of the random greetings messages

E.g output of my code: Instance 1: User: Hi there! Bot: Hey!!

Instance 2: User: Morning Fellow Bot: Hello :)

I just want to detect the semantic meaning of the user message and then reply based on what user said from my arrays.

Like when user says , "Bye" or "Take Care" etc any one of these, my bot should understand user wants to go and then bot should say "Take care :)"

I am thinking to I need Reg ex to match such occurrences or String contain functions ? Any basic coding examples would be very appreciated.

OR Alternatively, Instead of making my own manual responses can I use some AIML like Eliza Bot DB in NodeJS, so that I can just include it in my code and send the user message to Eliza Bot and then reply back whatever Eliza Bot replies.

Upvotes: 2

Views: 1249

Answers (2)

Jamel de la Fuente
Jamel de la Fuente

Reputation: 69

You could use a regex for now. It seems not so complicated. For example you van use this:

//Use your array here with a regex with your own logic.
var myRegExp = /Hello/;

var userText = "Hello my friend";
if(myRegExp.test(userText))
{
    console.log("Found a Hello!")
}

This is a basic example which you can extend with your own logic or programming a dictionary of words to match. Hope this helps.

Upvotes: 1

JF-Mechs
JF-Mechs

Reputation: 1081

Have you tried using indexOf ? it works like .contains() if you're trying to search an specific text/string on a string or an array. It basically checks the index of the string you wanted to check. Then you just need to add some kind of condition to make the statement true.

By adding > -1

Logically speaking it would return a false statements if string is not found and true if string is found since it would return its index. and index starts at 0.

Try to check this out,

var data = {
   message1 : 'Hi there', message2 : 'Hello there', message3 : 'Hey bot',message4 : 'Good Morning'
}

var userMsg = data;

if (userMsg.message1.indexOf('Hi') > -1) {
   //Do something
}

if (userMsg.message2.indexOf('Hello') > -1) {
   //Do something
}
if (userMsg.message3.indexOf('Hey') > -1) {
   //Do something
}

if (userMsg.message4.indexOf('Morning') > -1) {
   //Do something
}

P.S. I couldn't write a comment since I dont have much reputation yet. :)

Upvotes: 2

Related Questions