简体   繁体   English

在Jquery中查找关键短语的简单方法?

[英]Simple Way of Looking for Key Phrases in Jquery?

I have set up a method of retrieving messages from a real time chat client. 我已经建立了一种从实时聊天客户端检索消息的方法。 I want to take these messages (a lot of text) and find out if any of these messages contain key phrases. 我想获取这些消息(大量文本),并找出这些消息中是否包含关键短语。 If they contain a key phrase I will simply add 1 to a count variable. 如果它们包含关键字,我将简单地在count变量上加1。

Currently I am doing it like this ("good", "yes", and "nice" are the key phrases) 目前,我正在这样做(“好”,“是”和“不错”是关键短语)

for (var i = 0; i < data.msgs.length; i++)
{
    str = data.msgs[i].msg.toLowerCase();
    if (str.search("good") != -1 || str.search("yes") != -1 || str.search("nice") != -1)
    {
         count = count + 1;
    }
}

This method works, but it isn't very pretty. 此方法有效,但不是很漂亮。 I plan on adding around 100 different key phrases and it is easy to see that this will become silly. 我计划添加大约100个不同的关键短语,很容易看出这将变得很愚蠢。 Also, adding new key phrases is annoying and cumbersome. 另外,添加新的关键短语是烦人且麻烦的。

I'm wondering if there is a better way of making some kind of database of key phrases from which I can search for matches. 我想知道是否有更好的方法来建立某种关键短语数据库,以便从中搜索匹配项。 Preferably one which allows for easy adding and deleting of phrases. 优选地,其允许容易地添加和删除短语。

Thanks. 谢谢。

var keys = ['good', 'yes', 'nice'];
for (var i = 0; i < data.msgs.length; i++)
{
    str = data.msgs[i].msg.toLowerCase();
    for(var j=0; j < keys.length; j++){
         if(str.indexOf(keys[j]) !== -1){
            count++;
            //could also return here if you want to break the function after a find
          }
    }
}

You could also load your "keys" from a JSON file and parse it. 您还可以从JSON文件加载“密钥”并进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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