I have an arraylist of different phrases such as, “protein”, ” protein kinase”, “functional”, “functional protein”, “sox5”, “il-6”, Now, if I give a sentence as input, “functional protein kinase and il-6 and sox5”, it must provide output as, “{functional protein} kinase and {il-6} and {sox5}”. Every word in the sentence must be compared with the phrases.
The code that I have done returns me starting and ending indexes of different strings which are compared with the arraylist of different phrases. I need to filter out only the indexes which is largest and without any collisions.
For e.g.
Input:
[0, 7][8, 22][8, 15] [36, 43] [23, 43] [20, 30]
Required output:
[0, 7] [8, 22] [23, 43]
Cases:
- Between [8, 22] and [8, 15], [8, 22] is the largest because 22-8 = 14 > 15-8 = 7, so [8,22] must be selected.
- Between [36, 43], [23, 43] and [20, 30], 36 lies in the range [23, 43] and 30 also lies in the range [23, 43] which is collision but among these collisions, [23, 43] is
largest and must be selected.
What should I do in order to get the required output? (comparing criteria)
I have done,
ArrayList<ArrayList<Integer>> ListOfList = new ArrayList<ArrayList<Integer>>();
for(int a = 0; a<ListOfList.size();a++)
{
if(a == ListOfList.size()-1) break;
for(int b = a+1; b<ListOfList.size();b++)
{
if((ListOfList.get(a).get(0) == ListOfList.get(b).get(0)) && (ListOfList.get(a).get(1) < ListOfList.get(b).get(1)))
{
startOffset = ListOfList.get(b).get(0);
endOffset = ListOfList.get(b).get(1);
}
else
{
startOffset = ListOfList.get(a).get(0);
endOffset = ListOfList.get(a).get(1);
}
}
}
From what I understand your solution would be to group all pairs with intersecting indexes, and then just find the one with max length in each group. Here’s some boilerplate code. Let me know if you need more clarification:
static class Pair {
public int start, end;