Function for bigrams matching
I have got a following function:
public static ArrayList<String> matchLists(ArrayList<String> ar1, ArrayList<String> ar2, ArrayList<String> ar3) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < ar1.size(); i++) {
for (int j = 0; j < ar2.size(); j++) {
for (int k = 0; k < ar3.size(); k++) {
String[] s1 = ar1.get(i).split("\\s");
String[] s2 = ar2.get(j).split("\\s");
String[] s3 = ar3.get(k).split("\\s");
if (s1[1].equals(s2[0]) && s2[1].equals(s3[0])) {
result.add(s1[0] + " " + s2[0] + " " + s3[0] + " " + s3[1]);
}
}
}
}
return result;
}
It takes as input 3 ArrayLists of bigrams and compares them. If the second word from the first array list matches the word from the second array list then they create a sentence.
Example:
1st Array List: he ate
2nd Array List: ate two
3rd Array List: two apples
creates a sentence he ate two apples. But this function is开发者_JAVA百科 only limited to 3 ArrayLists. I would like to make it more robust so It would accept a 2d ArrayList of strings where each single ArrayList would be a collection of bigrams and would be checking all the possible matchings from the available bigrams. Can somebody help me with that?
A 2D ArrayList
of strings would be ArrayList<ArrayList<String>>
. Then you just need to change your loops to loop through all of the other ArrayLists
. I'm not going to tell you how to do this (as this does look very much like homework) but you should be able to figure it out quite easily :)
精彩评论