Merge lines that share a word-link

Tags: , ,



so I’m having a small problem in java. I have something like

"Victor Fleming" 
"Gone With" 
"With The" 
"The Wind."  

So what the sentence should actually look like is

"Victor Fleming" 
"Gone with the wind."

Therefore I’m looking to form a single sentence, by words that are adjacent and the same. If no adjacent same word is detected then the sentence will be separated as in “Victor Fleming” case where Fleming is not the same with Gone, so a new sentence is starting. What I’ve written so far:

List<String> separatedText = new ArrayList<>();
int i = 0;
while (i < mergedTextByHeightColor.size()) {
    if ((i < (mergedTextByHeightColor.size() - 3)) && !(mergedTextByHeightColor.get(i + 1).equals(mergedTextByHeightColor.get(i + 2)))) {
        separatedText.add(mergedTextByHeightColor.get(i) + " " + mergedTextByHeightColor.get(i + 1));
        i = i + 2;
    }
    String concatStr = "";
    while ((i < (mergedTextByHeightColor.size() - 3)) && (mergedTextByHeightColor.get(i + 1).equals(mergedTextByHeightColor.get(i + 2)))) {
        if (concatStr.contains(mergedTextByHeightColor.get(i))) {
            concatStr = mergedTextByHeightColor.get(i + 1) + " " + mergedTextByHeightColor.get(i + 3);
        } else {
            concatStr = mergedTextByHeightColor.get(i) + " " + mergedTextByHeightColor.get(i + 1) + " " + mergedTextByHeightColor.get(i + 3);
        }
        i = i + 3;
    }
    separatedText.add(concatStr);
}

Answer

We can store the sentences in a String array, then loop through each one.

Inside the loop, we check whether the last word of the last item (by splitting it into an array with .split(" "), then getting the last element) is equal to the first word of the current item. If it is, we first remove the first word of the current item, then append it to a StringBuilder.

If it isn’t, then we append the StringBuilder’s value to the list, append the current element, and move on.

String[] sentences = {"Victor Fleming", "Gone With", "With The", "The Wind."};
List<String> newsentences = new ArrayList<>();
StringBuilder str = new StringBuilder();
for(int i = 0; i < sentences.length; i++) {
    String cur = sentences[i];
    if(i != 0) {
        String[] a = sentences[i-1].split(" ");
        String[] b = cur.split(" ");
        String last = a[a.length-1];
        String first = b[0];
        if(last.equalsIgnoreCase(first)) {
            str.append(cur.substring(first.length()));
        }else {
            newsentences.add(str.toString());
            str = new StringBuilder();
            str.append(cur);
        }
    }else {
        str.append(cur);
    }
}
newsentences.add(str.toString());
System.out.println(Arrays.toString(newsentences.toArray()));

Output:

[Victor Fleming, Gone With The Wind.]


Source: stackoverflow