DoubleP90
DoubleP90

Reputation: 1249

Most efficient way of comparing long arrays of strings

I'm using the speech recognizer to get a voice input from the user, it returns an array of 5 strings which I pass to this method

    public int analyzeTag(ArrayList<String> voiceResults,Editor editor, Context context){
            for (String match : voiceResults) {
                Log.d(TAG, match);
                if (match.equalsIgnoreCase(context.getResources().getString(R.string.first_tag))){
                    editor.append(context.getResources().getString(R.string.first_tag));
                    return 1;
                }
                else if (match.equalsIgnoreCase(context.getResources().getString(R.string.second_tag))){
                    editor.append(context.getResources().getString(R.string.second_tag));
                    return 1;
                }
                //etc....(huge list of tags)
                //Some tags might also have acceptable variations, example:
                else if (match.equalsIgnoreCase("img") || match.equalsIgnoreCase("image")
                {
                     editor.append("img"); //the string to append is always taken from the first variation
                }

            }
            return 0;
        }

This method compares the results with a list of tags, the tag list will be pretty big with hundreds of tags so I would like to find the most efficient way to do this operation.

I need help with:

1.Is my way of comparing results the most efficient? Is there a better way? (from the user experience perspective, I don't want users waiting a long time to get a result). The voice input will be a big part of my app so this method will be called quite often

2.I have a long list of tags, obviously the if(), elseIf() route is gonna be quite repetitive, is there a way to iterate this? Considering the fact that some tags might have variations (even more than 1)and that the variation 1 ("img") will be the same for everyone, but other variations will be locale/language sensitive example: "image" for english users "immagini" for italian users etc. Text appended to the editor will be always taken from the first variation

Upvotes: 1

Views: 98

Answers (1)

ChrisCarneiro
ChrisCarneiro

Reputation: 352

How about puting tags in a StringArray and then iterate though the array ?

  String[] tags = context.getResources().getStringArray(R.array.tags);
    for (String match : voiceResults) {
        for (int index = 0; index < tags.length; index++ ) {            
            if (match.equalsIgnoreCase(tags[index]) {
                editor.append(tags[index]);
            }
        }
    }

Here's the doc on StringArray

Upvotes: 2

Related Questions