Thursday, 8 August 2013

Faster String Matching/Iteration Method?

Faster String Matching/Iteration Method?

In the program I'm currently working on, there's one part that's taking a
bit long. Basically, I have a list of Strings and one target phrase. As an
example, let's say the target phrase is "inventory of finished goods".
Now, after filtering out the stop word (of), I want to extract all Strings
from the list that contains one of the three words: "inventory",
"finished", and "goods". Right now, I implemented the idea as follows:
String[] targetWords; // contains "inventory", "finished", and "goods"
ArrayList<String> extractedStrings = new ArrayList<String>();
for (int i = 0; i < listOfWords.size(); i++) {
String[] words = listOfWords.get(i).split(" ");
outerloop:
for (int j = 0; j < words.length; j++) {
for (int k = 0; k < targetWords.length; k++) {
if (words[j].equalsIgnoreCase(targetWords[k])) {
extractedStrings.add(listOfWords.get(i));
break outerloop;
}
}
}
}
The list contains over 100k words, and with this it takes rounghly .4 to
.8 seconds to complete the task for each target phrase. The things is, I
have a lot of these target phrases to process, and the seconds really add
up. Thus, I was wondering if anyone knew of a more efficient way to
complete this task? Thanks for the help in advance!

No comments:

Post a Comment