detect if a combination of string objects from an array matches against any commands

倖福魔咒の 提交于 2019-12-13 06:02:37

问题


Please be patient and read my current scenario. My question is below.

My application takes in speech input and is successfully able to group words that match together to form either one word or a group of words - called phrases; be it a name, an action, a pet, or a time frame.

I have a master list of the phrases that are allowed and are stored in their respective arrays. So I have the following arrays validNamesArray, validActionsArray, validPetsArray, and a validTimeFramesArray.

A new array of phrases is returned each and every time the user stops speaking.

NSArray *phrasesBeingFedIn = @[@"CHARLIE", @"EAT", @"AT TEN O CLOCK", 
                                @"CAT", 
                                @"DOG", "URINATE", 
                                @"CHILDREN", @"ITS TIME TO", @"PLAY"];

Knowing that its ok to have the following combination to create a command:

COMMAND 1: NAME + ACTION + TIME FRAME
COMMAND 2: PET + ACTION 
COMMAND n: n + n, .. + n

//In the example above, only the groups of phrases 'Charlie eat at ten o clock' and 'dog urinate'
//would be valid commands, the phrase 'cat' would not qualify any of the commands
//and will therefor be ignored

Question

What is the best way for me to parse through the phrases being fed in and determine which combination phrases will satisfy my list of commands?

POSSIBLE solution I've come up with One way is to step through the array and have if and else statements that check the phrases ahead and see if they satisfy any valid command patterns from the list, however my solution is not dynamic, I would have to add a new set of if and else statements for every single new command permutation I create.

My solution is not efficient. Any ideas on how I could go about creating something like this that will work and is dynamic no matter if I add a new command sequence of phrase combination?


回答1:


I think what I would do is make an array for each category of speech (pet, command, etc). Those arrays would obviously have strings as elements. You could then test each word against each simple array using

[simpleWordListOfPets containsObject:word]

Which would return a BOOL result. You could do that in a case statement. The logic after that is up to you, but I would keep scanning the sentence using NSScanner until you have finished evaluating each section.

I've used some similar concepts to analyze a paragraph... it starts off like this:

while ([scanner scanUpToString:@"," intoString:&word]) {

        processedWordCount++;
        NSLog(@"%i total words processed", processedWordCount);

        // Does word exist in the simple list?
        if ([simpleWordList containsObject:word]) {
            //NSLog(@"Word already exists: %@", word);

You would continue it with whatever logic you wanted (and you would search for a space rather than a ",".



来源:https://stackoverflow.com/questions/21177259/detect-if-a-combination-of-string-objects-from-an-array-matches-against-any-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!