Jump to content

Checking if string contains substring from list


Leo1452
 Share

You are about to reply to a thread that has been inactive for 355 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I'd have a listener on channel 0. For every message in nearby chat, it would check if that string contains a substring from a list in my script and perform some action if it finds one. To see if a message contains the word "happy," I'd use llSubStringIndex(llToLower(message), "happy") but how about a whole list of words I'm looking for?

Link to comment
Share on other sites

Briefly, parse the string to a list, then step through it with a for loop, taking each element of the list and using llListFindList to see if it's in your list of words. (I am assuming your list is going to be longer than any reasonable sentence coming in from local chat).

A refinement to speed up this stage would be to have a global variable recording the shortest and longest lengths of the words in your list and not bother checking any words in the chat log list that are either shorter or longer than the words in your list, which would get rid of common words such as "and", "the", "dammitTheTPFailedAgain", etc. Also, a bit of housekeeping to record which words from the chat list have already been processed would save checking repeated instances of them.

Edited by Profaitchikenz Haiku
Link to comment
Share on other sites

You have to loop through the entire list, or at least add a mess of extra filters to the if statements that check for each of your keywords.  It's a clunky process, likely to put measurable load on a region's servers if you are listening in the PUBLIC_CHANNEL.  Depending on what else is going on, you could be adding to chat lag and making yourself mildly unpopular.

Link to comment
Share on other sites

Ha, just pipped Rolig to the post, this has to be a first. I think my suggestion could avoid a heavy hit on chat lag, and as far as making onseself mildly unpopular, nobody seems to hate the creators of some of the heavily scripted RLVa devices around. 

Edited by Profaitchikenz Haiku
Link to comment
Share on other sites

A snippet to show what I had in mind

 

list myWords = ["foobar", "pooBear", "youThere", "whoWhere"];

integer minWord;    // shortest string length in myWords
integer maxWord;    // longest stringLength in myWords

// before calling this function you must have
// 1) set minWord and maxWord to the minimum and maximum word lengths in myWords
// 2) used llParseString2List with appropriate spacers and seperators to create  a list to pass to the function

checkChat(list checkThis)
{
    string thisWord;
    integer thisLen;
    list doneWords = [];
    integer ii;
    integer iiMax = llGetListLength(checkThis);
    
    for( ii = 0; ii < iiMax; ii++)
    {
        thisWord = llList2String(checkThis, ii);
        thisLen = llStringLength(thisWord);
        if( thisLen >= minWord && thisLen <= maxWord)
        {
            if( llListFindList(doneWords, [thisWord]) == -1)
            {
                doneWords += [thisWord];
                if( llListFindList(myWords, [thisWord]) > -1)
                {
                    // do stuff
                }
            }
        }
    }     
}

 

Edited by Profaitchikenz Haiku
Link to comment
Share on other sites

just a basic example from what the others have said...

list words = ["happy","sad","funky"]; // assumes all words are lower case
default
{
    state_entry()
    { llListen(0,"","","");
    }   
    listen( integer channel, string name, key id, string message )
    {
        list msg_list = llParseString2List(message,[""],[" "]);   
        integer x;
        for(; x < llGetListLength( msg_list); ++x)
        {   string word = llToLower( llList2String( msg_list, x) );
            if (~llListFindList( words,[word] ) ) 
            { llOwnerSay("found: " + word);
            } 
        } 
    }
}

 

Edited by Xiija
Link to comment
Share on other sites

2 hours ago, Leo1452 said:

To see if a message contains the word "happy," I'd use llSubStringIndex(llToLower(message), "happy") but how about a whole list of words I'm looking for?

Instead of the string "happy", you get a string from the list using llList2String. You will also need a loop to iterate through every entry of the list.

That's the core of what the above examples are showing you.

Link to comment
Share on other sites

in keeping with your original thought then we can operate on the message using llSubStringIndex. Example:
 

list words = ["happy", "sad", "content"];

message = llToLower(message);
for (i = 0; i < length_message; i++)
{
   word = llList2String(words, i);
   index = llSubStringIndex(sentence, word);
   if (~index) // is found
    do something with word at index in message  
}

 

Edited by Mollymews
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 355 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...