简体   繁体   中英

How to check if a string contains words

I'm not really advanced at coding/javascript so I hired someone to code a function that should check SMS messages (within a web service called Twilio) and determine if it contains positive or negative words.

He wrote the code below. However, the problem is that this code only detects a sub-string and not a word.

In this case it doesn't make a distinction between the word "No" and "Nowhere" or "Yes" and "Yesterday". So I need it to only check for the words and not just the letters.

Can anyone help me out?

Here's the code:

 var event = { Body: 'this is a test with some words' }; var str = event.Body.toLowerCase(); var positive_pattern = ['yes', 'yeah', 'sure', 'of course', 'yup', 'i do', 'i have', 'certainly', 'yep', 'you bet']; var negative_pattern = ['no', 'nope', 'stop']; function contains(target, pattern) { var value = 0; pattern.forEach(function(word) { value = value + target.includes(word); }); return (value > 0); } var positive = contains(str, positive_pattern); var negative = contains(str, negative_pattern); console.log({ positive, negative }); callback = (_, msg) => console.log(msg); if (positive === true && negative === false) callback(null, 'positive'); else if (positive === false && negative === true) callback(null, 'negative'); else if (positive === true && negative === true) callback(null, 'negative'); else if (positive === false && negative === false) callback(null, 'unknown');

You can use the regular expression word boundary \b .

function contains(target, pattern) {
  var value = 0;
  pattern.forEach(function(word) {
    value = value + new RegExp("\\b" + word + "\\b")).test(target);
  });
  return (value > 0);
}

Since you only require one of the words to match, you can use Array#some .

function contains(target, pattern) {
  return pattern.some(word=>new RegExp("\\b" + word + "\\b")).test(target));
}

If the words might contain regular expression metacharacters, you can split the target on whitespace characters and check if the resulting array contains each word.

function contains(target, pattern){
    const parts = target.split(/\s+/);
    return pattern.some(word=>parts.includes(word));
}

We need to make sure the whole word is matched, which we can do by splitting the target string up by whitespace.

 var positive_pattern = ['yes', 'yeah', 'sure', 'of course', 'yup', 'i do', 'i have', 'certainly', 'yep', 'you bet']; var negative_pattern = ['no', 'nope', 'stop']; function contains(target, patterns) { // at least one match? return patterns.filter( word => target.split(/\s+/).includes(word) ).length > 0; } function Test(callback, str) { var positive = contains(str, positive_pattern); var negative = contains(str, negative_pattern); if (positive === true && negative === false) callback(null, 'positive'); else if (positive === false && negative === true) callback(null, 'negative'); else if (positive === true && negative === true) callback(null, 'negative'); else if (positive === false && negative === false) callback(null, 'unknown'); } const testCallback = (_, msg) => console.log(msg); const tests = [ 'yesterday nowhere', // unknown 'yes', // positive 'yes no yeah', // negative (mixed) 'nope' // negative ]; for (const testString of tests) Test(testCallback, testString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM