I am interested in seeing how people would refactor this code to make it more readable, remove local variables, etc. The code checks a string against a number of arrays. The first (0) field of each array is the root word and the rest of the words are its synonyms. The idea is to replace any word in the user-provided string with the root word or provide the user-supplied word if none were found.
import java.util.Arrays;
public class Synonymiser {
private final static int ROOT_WORD = 0;
private String[][] mSynonyms;
public Synonymiser(String[]... synonyms) {
super();
this.mSynonyms = synonyms;
}
public String normaliseString(String inStr){
String[] inArr = inStr.split(" ");
boolean matchFound;
for (int i = 0; i < inArr.length; i++){
matchFound=false;
for (String[] synonymArr : mSynonyms){
for(int j = 0 ; j < synonymArr.length; j++){
if (inArr[i].equalsIgnoreCase(synonymArr[j])){
inArr[i] = synonymArr[ROOT_WORD];
matchFound = true;
}
if (matchFound) break;
}
if (matchFound) break;
}
}
return Arrays.toString(inArr);
}
public static void main(String [] args){
String[] ar1 = new String[] {"one", "two", "three", "four", "five"};
String[] ar2 = new String[] {"six", "seven", "eight"};
Synonymiser s = new Synonymiser(ar1, ar2);
System.out.println(s.normaliseString("one two seven bulb"));
}
}
for (int j = 0;
loop can be replaced with afor (String possibleSynonym : synonymArr)
\$\endgroup\$