CorrectionHighlighter { public static String highlightCorrections(String originalQuery, String correctedQuery, String highlightingTagRemoved, String highlightingTagReplaced, boolean ignoreRemovedQuotes) { if (originalQuery == null || correctedQuery == null || originalQuery.trim().equals("") || correctedQuery.trim().equals("")) { return correctedQuery; } List<String> originalQueryTokens = new ArrayList<String>(); List<String> correctedQueryTokens = new ArrayList<String>(); ReSearcherUtils.tokenizeQueryString(originalQuery, originalQueryTokens); ReSearcherUtils.tokenizeQueryString(correctedQuery, correctedQueryTokens); int j = 0; String highlightedQuery = ""; for (int i = 0; i < correctedQueryTokens.size(); i++) { String nextTokenOrig = originalQueryTokens.get(j); String nextTokenCorr = correctedQueryTokens.get(i); while (nextTokenOrig.equals("\"") && ignoreRemovedQuotes) { j++; nextTokenOrig = originalQueryTokens.get(j); } if (nextTokenCorr.equalsIgnoreCase(nextTokenOrig)) { highlightedQuery += nextTokenCorr + " "; } else { int k = j + 1; boolean wordFound = false; for (; k < originalQueryTokens.size(); k++) { if (originalQueryTokens.get(k).equalsIgnoreCase(nextTokenCorr)) { wordFound = true; break; } } if (wordFound) { boolean tagOpened = false; boolean tagClosed = false; if (highlightingTagRemoved != null) { for (int l = j; l < k; l++) { if (originalQueryTokens.get(l).equals("\"") && ignoreRemovedQuotes == true) { ; } else { if (tagOpened == false) { highlightedQuery += "<" + highlightingTagRemoved + ">"; tagOpened = true; } highlightedQuery += originalQueryTokens.get(l); if (l == k - 1) { highlightedQuery += "</" + highlightingTagRemoved + "> "; tagClosed = true; } else { if (originalQueryTokens.get(l).equals("\"") && ignoreRemovedQuotes == false) { ; } else { highlightedQuery += " "; } } } } if (tagOpened == true && tagClosed == false) { highlightedQuery = highlightedQuery.substring(0, highlightedQuery.length() - 1); highlightedQuery += "</" + highlightingTagRemoved + "> "; } } highlightedQuery += nextTokenCorr + " "; j = k; } else { if (highlightingTagReplaced != null) { highlightedQuery += "<" + highlightingTagReplaced + ">" + nextTokenCorr + "</" + highlightingTagReplaced + "> "; } } } j++; } if (highlightingTagRemoved != null && (j < originalQueryTokens.size())) { boolean tagOpened = false; boolean tagClosed = false; for (int k = j; k < originalQueryTokens.size(); k++) { if (originalQueryTokens.get(k).equals("\"") && ignoreRemovedQuotes == true) { ; } else { if (tagOpened == false) { highlightedQuery += "<" + highlightingTagRemoved + ">"; tagOpened = true; } highlightedQuery += originalQueryTokens.get(k); if (k == originalQueryTokens.size() - 1) { highlightedQuery += "</" + highlightingTagRemoved + "> "; tagClosed = true; } else { highlightedQuery += " "; } } } if (tagOpened == true && tagClosed == false) { highlightedQuery = highlightedQuery.substring(0, highlightedQuery.length() - 1); highlightedQuery += "</" + highlightingTagRemoved + "> "; } } return highlightedQuery.trim(); }  static String highlightCorrections(String originalQuery, String correctedQuery, String highlightingTagRemoved, String highlightingTagReplaced, boolean ignoreRemovedQuotes);  }
@Test public void testHighlightCorrections() { assertEquals("some query", CorrectionHighlighter.highlightCorrections("some query", "some query", "i", "b", true)); assertEquals("some <i>test</i> query", CorrectionHighlighter.highlightCorrections("some test query", "some query", "i", "b", true)); assertEquals("some test <i>query</i>", CorrectionHighlighter.highlightCorrections("some test query", "some test", "i", "b", true)); assertEquals("<i>some</i> test query", CorrectionHighlighter.highlightCorrections("some test query", "test query", "i", "b", true)); assertEquals("some <b>text</b> query", CorrectionHighlighter.highlightCorrections("some test query", "some text query", "i", "b", true)); assertEquals("some test <b>quest</b>", CorrectionHighlighter.highlightCorrections("some test query", "some test quest", "i", "b", true)); assertEquals("<b>same</b> test query", CorrectionHighlighter.highlightCorrections("some test query", "same test query", "i", "b", true)); assertEquals("some <i>test query</i>", CorrectionHighlighter.highlightCorrections("some test query", "some", "i", "b", true)); assertEquals("some <i>test query</i> bla", CorrectionHighlighter.highlightCorrections("some test query bla", "some bla", "i", "b", true)); assertEquals("some test query bla", CorrectionHighlighter.highlightCorrections("some \"test query\" bla", "some test query bla", "i", "b", true)); assertEquals("some test query bla", CorrectionHighlighter.highlightCorrections("some test \"query\" bla", "some test query bla", "i", "b", true)); assertEquals("some test query bla", CorrectionHighlighter.highlightCorrections("some test query \"bla\"", "some test query bla", "i", "b", true)); assertEquals("some test query bla", CorrectionHighlighter.highlightCorrections("\"some\" test query bla", "some test query bla", "i", "b", true)); assertEquals("some test query <b>blog</b>", CorrectionHighlighter.highlightCorrections("some \"test query\" bla", "some test query blog", "i", "b", true)); assertEquals("some <i>test</i> query <b>blog</b>", CorrectionHighlighter.highlightCorrections("some \"test query\" bla", "some query blog", "i", "b", true)); assertEquals("some <i>\"test</i> query <i>\"</i> bla", CorrectionHighlighter.highlightCorrections("some \"test query\" bla", "some query bla", "i", "b", false)); assertEquals("some <i>\"test</i> query <i>\"</i> blog <b>blog</b>", CorrectionHighlighter.highlightCorrections("some \"test query\" blog bla", "some query blog blog", "i", "b", false)); }