Check for more than 1 word in a string

Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Hi,
I'm still here with a script question (don't hate me :LZSteary:).
I use $gameVariables.value(x).toLowerCase().includes("word1", "word2") in conditional branches to check a string previously stored in variable (x).
Now...It seems that the game check if in the string there is ONLY "word1" (And it is fine when I need to check i.e. $gameVariables.value(x).toLowerCase().includes("I don't know java")), but I need a way to check even for "word2" without nest other conditional branches.
I need a single script to do that.
Is it possible?
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,015
Reaction score
7,025
First Language
German
Primarily Uses
RMMV
in that case it should be

JavaScript:
$gameVariables.value(x).toLowerCase().includes("word1") && $gameVariables.value(x).toLowerCase().includes("word2")
(as one line)
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,141
Reaction score
12,414
First Language
English
Primarily Uses
RMMV
Note that if you are searching for "word1", it will return true if the variable holds "word1". It will also return true if the variable holds "word10", "word15", "word100", "word1111111", etc.

If that is going to be an issue, you should probably say what you're REALLY trying to achieve (why are you searching for one word and another word, what sets the variable, what could it possibly contain, etc) and we'll see if there's a more reliable way to accomplish it.
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
If you wanna check if any of the words are there, there's a small solution:
JavaScript:
RegExp(["insert", "words", "here"].join('|')).test($gameVariables.value(x).toLowerCase());

If you wanna check if all the words are there, and there's a ton of words to check, this is my l o n g solution:
JavaScript:
(function(){ words = ["insert", "all", "your", "cool", "words", "right", "here"]; for(i=0;i<words.length;i++){if(!$gameVariables.value(x).toLowerCase().includes(words[i])){break}};if(words.length===i)return!0})();

Both work in the conditional branch script box, Andar's too of course.
 
Last edited:

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Note that if you are searching for "word1", it will return true if the variable holds "word1". It will also return true if the variable holds "word10", "word15", "word100", "word1111111", etc.

If that is going to be an issue, you should probably say what you're REALLY trying to achieve (why are you searching for one word and another word, what sets the variable, what could it possibly contain, etc) and we'll see if there's a more reliable way to accomplish it.
Do you mean that if I set "word1" = "head" it will turn true even with "headache", right?
There is a way to search only for "head" avoiding ambiguity, then?
The 2nd solution posted by ozubon will do that?
I'm using a free input text plugin, so the variable can contain a whole phrase.

If you wanna check if any of the words are there, there's a small solution:
JavaScript:
RegExp(["insert", "words", "here"].join('|')).test($gameVariables.value(x).toLowerCase());

If you wanna check if all the words are there, and there's a ton of words to check, this is my l o n g solution:
JavaScript:
(function(){ words = ["insert", "all", "your", "cool", "words", "right", "here"]; for(i=0;i<words.length;i++){if(!$gameVariables.value(x).toLowerCase().includes(words[i])){break}};if(words.length===i)return!0})();

Both work in the conditional branch script box, Andar's too of course.
Just to see if I understand correctly; the first solution you posted turns true if it finds even just one of those words (i.e. "insert" alone will be enough),
while the second one turns true only if it will find ALL those words (that seems what I need).
Is that right?
Will the second solution avoid the ambiguities that Shaz emphasized?
Thank you all
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,015
Reaction score
7,025
First Language
German
Primarily Uses
RMMV
There is a way to search only for "head" avoiding ambiguity, then?
no, in neither case. Because the computer cannot read, he can only compare sequences.

and if a partial sequence is contained in a larger one, it will always be found - that is one of the reason why automatic spellcheckers and similar programs often fail. It has required decades and very high processing power to get text recognition software even halfway running, and you can't include that in a script or plugin for an RM.
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
@Mark91
Yes, exactly. But no unfortunately those two are not error proof. The word "the" would be found if the word "these" is in the string. I can come up with or find a solution to that problem but it might be a little bit more complicated than just using the conditional branch script box.

@Andar
No that's not true, there absolutely are easy ways to check for whole words instead of strings of characters, as long as the words are separated by spaces of course. Making "these" not return true for "the" isn't spellcheck levels of difficulty.
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,015
Reaction score
7,025
First Language
German
Primarily Uses
RMMV
@ozubon not without a lot of quirks and rules. Your "spaces" solution (searching for " the " instead of "the" would fail on interpunctation (comma, dot, exclamation mark, question mark etc) and the beginning of the string and so on.

Yes, it can be done - I never said it is impossible. But it will require a more complex processing of the sentences, exactly because there are too many ways how something could be interrupted or not. And that can't be done as easy as the OP assumed
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
I don't mean you search for " the " instead of "the" using includes(), although it's possible to write similarily simple code that finds the word "the" in a string without finding it in "these", "they", or "thesaurus". Interpunctation is not a problem either because that's a very limited group of characters to make rules for, especially in english. You most likely do not need a complex processing of sentences unless you're trying to achieve something very specific. Needing to pick out certain words from a string, for the purpose of a game isn't as complicated as you're making it seem.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,141
Reaction score
12,414
First Language
English
Primarily Uses
RMMV
I believe ozubon's first solution (or a variation of it) is what you're after, but can you answer a few more questions to be sure?

You say you're using a free text plugin, so the variable can contain a whole phrase. Which variable - the one entered by the user? In that case, do you want to know if the user entered a particular (whole) word as part of that phrase? Or do you want to compare the whole input from the user with a list of phrases?

If the user enters "I have a headache" and you are looking for the word head, you would want it to return false?
But if they entered "My head is aching" and you're looking for the word head, you would want it to return true?

In that case, the variable contains the string you're searching in, and you will hardcode the string you're searching for:

Code:
RegExp(" " + $gameVariables.value(x).toLowerCase() + " ").test(" head ");
The above will convert the user's input to lower case and add a space at either end, then search for the word head with spaces around it. So it will match Head at the start or end of the phrase, as well as in the middle, as long as it's not a part of a longer word.

There are other ways to do it, like convert the input text to an array using spaces as delimiters, then comparing each element to the word you're after.
 

TWings

The Dragon Whisperer
Veteran
Joined
Jul 26, 2017
Messages
385
Reaction score
646
First Language
French
Primarily Uses
RMMV
In that case, the variable contains the string you're searching in, and you will hardcode the string you're searching for:

Code:
RegExp(" " + $gameVariables.value(x).toLowerCase() + " ").test(" head ");
The above will convert the user's input to lower case and add a space at either end, then search for the word head with spaces around it. So it will match Head at the start or end of the phrase, as well as in the middle, as long as it's not a part of a longer word.

There are other ways to do it, like convert the input text to an array using spaces as delimiters, then comparing each element to the word you're after.
Assuming that $gameVariables.value(x) is containing a whole sentence, I believe it should be checked that way.
Code:
RegExp("\\b"+'head'+"\\b").test( $gameVariables.value(x).toLowerCase() );
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
I believe ozubon's first solution (or a variation of it) is what you're after, but can you answer a few more questions to be sure?

You say you're using a free text plugin, so the variable can contain a whole phrase. Which variable - the one entered by the user? In that case, do you want to know if the user entered a particular (whole) word as part of that phrase? Or do you want to compare the whole input from the user with a list of phrases?

If the user enters "I have a headache" and you are looking for the word head, you would want it to return false?
But if they entered "My head is aching" and you're looking for the word head, you would want it to return true?

In that case, the variable contains the string you're searching in, and you will hardcode the string you're searching for:

Code:
RegExp(" " + $gameVariables.value(x).toLowerCase() + " ").test(" head ");
The above will convert the user's input to lower case and add a space at either end, then search for the word head with spaces around it. So it will match Head at the start or end of the phrase, as well as in the middle, as long as it's not a part of a longer word.

There are other ways to do it, like convert the input text to an array using spaces as delimiters, then comparing each element to the word you're after.

I will try to give a more practical example.
Since variable (x) can contain a whole sentence, it can be useful for me to make the PC distinguish in affirmative / interrogative / negative sentences.
If it can be said that an interrogative sentence can be verified on the basis of the presence of a "?", and an affirmative sentence can be anything that is neither classified as a question nor a negative, the problem comes precisely in identifying a negative phrase.

In my language you can use "no" or "non" to deny, but these are too frequent particles.
I may have to say "nonno" (=grandfather) in an affirmative phrase, but that word contains both "no" and "non"... And a lot of words here can end with a "-no", even verbs.
Now... I, in fact, have tried to use " no " and, in some circumstances, it worked, but what if my phrase just begins with "No, blabla"? " no " will turn false (cuz there is no space at the beginning), and "no " will still turn true for all words ending with "-no".
So the best thing would be something that can check fot the presence of ONLY "no" in the string. Without anything before or after.
I tried also $gameVariables.value(x)===("no") but it is very restrictive, since the whole sentence must be = "no".

I don't mean you search for " the " instead of "the" using includes(), although it's possible to write similarily simple code that finds the word "the" in a string without finding it in "these", "they", or "thesaurus". Interpunctation is not a problem either because that's a very limited group of characters to make rules for, especially in english. You most likely do not need a complex processing of sentences unless you're trying to achieve something very specific. Needing to pick out certain words from a string, for the purpose of a game isn't as complicated as you're making it seem.
This seems exactly what I was searching for, but I have no idea the level of complexity it requires.
Do you have any idea?
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
I combined mine and TWings' code and voilà:
JavaScript:
(function(){ words = ["this", "should", "do", "it"]; for(i=0;i<words.length;i++){if(!RegExp("\\b"+words[i]+"\\b").test($gameVariables.value(x).toLowerCase())){break}};if(words.length===i)return!0})();
  • Returns true only if all the words are included
  • "No" won't be found in "non" and neither will be found in "nonno"
  • Punctuation won't matter, "hello" will be found in "hello!" and "hello," etc.
  • Works in the conditional branch script box like before
  • It's an even longer monstrosity this time!
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
I combined mine and TWings' code and voilà:
JavaScript:
(function(){ words = ["this", "should", "do", "it"]; for(i=0;i<words.length;i++){if(!RegExp("\\b"+words[i]+"\\b").test($gameVariables.value(x).toLowerCase())){break}};if(words.length===i)return!0})();
  • Returns true only if all the words are included
  • "No" won't be found in "non" and neither will be found in "nonno"
  • Punctuation won't matter, "hello" will be found in "hello!" and "hello," etc.
  • Works in the conditional branch script box like before
  • It's an even longer monstrosity this time!

I would only change the last point in:
  • It's an even nicer piece of script!
It works perfectly, thank you all!!!
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
14,646
Reaction score
8,077
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Playtesting for Battle Castle is done, demo time for me was roughly 6 hours and 20 minutes. I'm going to pass it on to a few friends first, and after getting some feedback will be posting an official topic.
There's another Humble Bundle available now for RPG developer assets, this time focused somewhat more towards pixel art style games.
Ive got a long lost half uncle. Thats real cool.
I forget what is bumping rule in this forum. Is anyone know? Thank you.
BCj
Ew, why does my blogpost on the rpgmaker.net site look like a garbled mess? Are html codes disabled or something?

Forum statistics

Threads
97,902
Messages
947,748
Members
129,145
Latest member
PokeeyXD
Top