Check a list of words against a String

Started by Saub, Thu 01/12/2016 22:39:50

Previous topic - Next topic

Saub

Please can someone suggest best way to check if the string contains one of many words, and return the position?

I can use IndexOf to check if a user input string contains specific words, but it's quite cumbersome to set up multiple IndexOf and If's for each word I want to recognise.

I'm thinking if I could somehow have a numbered list of words, I could have a loop to check through until a match is found.  I don't think I can use the parser, as I want to also check for combinations of words in different categories eg. Subject - Verb - Noun

The script would search the input text to return the index of first word found from the Subject list, first from the verb, first from the noun.

Any suggestions gratefully recieved, thanks!


Snarky

Quote from: Saub on Thu 01/12/2016 22:39:50
Please can someone suggest best way to check if the string contains one of many words, and return the position?

I can use IndexOf to check if a user input string contains specific words, but it's quite cumbersome to set up multiple IndexOf and If's for each word I want to recognise.

I'm thinking if I could somehow have a numbered list of words, I could have a loop to check through until a match is found.

Yes, do that:

Code: ags
#define MAX_NOUNS 50
int nounCount=0;
String noun[MAX_NOUNS];

// Add a noun to the dictionary. Return false if dictionary full.
bool addNoun(String newNoun)
{
  if(nounCount<MAX_NOUNS)
  {
    noun[nounCount++] = newNoun;
    return true;
  }
  else
    return false;
}

int foundNounId=-1;
// Returns the index of the first noun in the string. Also sets foundNounId to identify that noun
int findNoun(String s)
{
  int n=s.Length;
  foundNounId=-1;
  int i=0;
  while(i<nounCount)
  {
    int nounIndex = s.IndexOf(noun[i]);
    if(noundIndex>=0 && nounIndex < n)
    {
      n = nounIndex;
      foundNounId = i;
    }
    i++;
  }
  if(foundNounId == -1)
    return -1;
  else
    return n;
}

// Gets the noun that was found with the last call to findNoun()
String getLastFoundNoun()
{
  if(foundNounId == -1)
    return null;
  else
    return noun[foundNounId];
}


And similarly for the other word classes.

Snarky

(One limitation of this approach is that simple IndexOf() will find matches inside words. So "scratch" will match to "rat," for example, and "stiffly" to "fly". There are ways to fix that, but more robust parsing is quite difficult in general.)

Saub

Thank you very much!  I'll give this a bash when I have a chance later.

Vincent

You could even try the module made by Monkey0506 "StringPlus" because it has some really great functions on there. :)

Crimson Wizard

Quote from: Vincent on Fri 02/12/2016 15:28:51
You could even try the module made by Monkey0506 "StringPlus" because it has some really great functions on there. :)
Ah, I was trying to remember how that module was called.
Here it is:
http://www.adventuregamestudio.co.uk/forums/index.php?topic=20950.0
The last release is old, but knowing how slow AGS script changes, it might work well today.

Vincent

Quote from: Crimson Wizard on Fri 02/12/2016 15:51:12
Ah, I was trying to remember how that module was called.
Ahah this is funny because I was looking for the module link and I could not find it. :)
Nowaday it's still working just fine. :)

SMF spam blocked by CleanTalk