use of org.apache.lucene.search.uhighlight.Passage in project lucene-solr by apache.
the class TestUnifiedHighlighterExtensibility method testFieldHiglighterExtensibility.
@Test
public void testFieldHiglighterExtensibility() {
final String fieldName = "fieldName";
FieldHighlighter fieldHighlighter = new FieldHighlighter(fieldName, null, null, null, 1, 1, null) {
@Override
protected Passage[] highlightOffsetsEnums(List<OffsetsEnum> offsetsEnums) throws IOException {
return super.highlightOffsetsEnums(offsetsEnums);
}
};
assertEquals(fieldHighlighter.getField(), fieldName);
}
use of org.apache.lucene.search.uhighlight.Passage in project OpenGrok by OpenGrok.
the class PassageConverterTest method testOneWordElided.
@Test
public void testOneWordElided() {
final String WORD = "dignissim";
int woff = DOC.indexOf(WORD);
assertTrue(woff >= 0, WORD);
Passage p = new Passage();
p.setStartOffset(woff);
p.setEndOffset(woff + WORD.length());
p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
assertEquals(1, p.getNumMatches(), "getNumMatches()");
PassageConverter cvt = getConverter((short) 0);
SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter);
assertEquals(1, linemap.size(), "linemap size()");
int lineno = linemap.firstKey();
assertEquals(5, lineno, "lineno");
LineHighlight lhi = linemap.get(lineno);
assertNotNull(lhi, "get LineHighlight");
assertEquals(41, lhi.getLelide(), "getLelide()");
assertEquals(139, lhi.getRelide(), "getRelide()");
assertEquals(139 - 41, cvt.getArgs().getContextWidth() - 2, "context width minus 2");
assertEquals(1, lhi.countMarkups(), "countMarkups()");
PhraseHighlight phi = lhi.getMarkup(0);
assertNotNull(phi, "get PhraseHighlight");
assertEquals(113, phi.getLineStart(), "getLineStart()");
assertEquals(113 + WORD.length(), phi.getLineEnd(), "getLineEnd()");
}
use of org.apache.lucene.search.uhighlight.Passage in project OpenGrok by OpenGrok.
the class ContextFormatterTest method testLineMatchFormatted.
@Test
public void testLineMatchFormatted() {
final String WORD = "gravida";
int woff = DOC.indexOf(WORD);
assertTrue(woff >= 0, WORD);
Passage p = new Passage();
p.setStartOffset(woff);
p.setEndOffset(woff + WORD.length());
p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
assertEquals(1, p.getNumMatches(), "getNumMatches()");
// First, test with contextCount==0
ContextArgs args = new ContextArgs((short) 0, (short) 10);
ContextFormatter fmt = new ContextFormatter(args);
fmt.setUrl("http://example.com");
Object res = fmt.format(new Passage[] { p }, DOC);
assertNotNull(res, "format() result");
final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">" + "3</span> Mauris diam nisl, tincidunt nec <b>gravida</b> sit" + " amet, efficitur vitae</a><br/>\n";
String ctx = res.toString();
assertLinesEqual("format().toString()", DOCCTX_0, ctx);
// Second, test with contextCount==1
args = new ContextArgs((short) 1, (short) 10);
fmt = new ContextFormatter(args);
fmt.setUrl("http://example.com");
res = fmt.format(new Passage[] { p }, DOC);
assertNotNull(res, "format() result");
final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#2\"><span class=\"l\">" + "2</span> Mauris vel tortor vel nisl efficitur fermentum nec vel" + " erat.</a><br/>" + "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">" + "3</span> Mauris diam nisl, tincidunt nec <b>gravida</b> sit" + " amet, efficitur vitae</a><br/>" + "<a class=\"s\" href=\"http://example.com#4\"><span class=\"l\">" + "4</span> est. Sed aliquam non mi vel mattis:</a><br/>";
ctx = res.toString();
assertLinesEqual("format().toString()", DOCCTX_1, ctx);
}
use of org.apache.lucene.search.uhighlight.Passage in project OpenGrok by OpenGrok.
the class ContextFormatterTest method testRightElidedMatchFormatted.
@Test
public void testRightElidedMatchFormatted() {
final String WORD = "Maecenas";
int woff = DOC.indexOf(WORD);
assertTrue(woff >= 0, WORD);
Passage p = new Passage();
p.setStartOffset(woff);
p.setEndOffset(woff + WORD.length());
p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
assertEquals(1, p.getNumMatches(), "getNumMatches()");
// First, test with contextCount==0
ContextArgs args = new ContextArgs((short) 0, (short) 10);
ContextFormatter fmt = new ContextFormatter(args);
fmt.setUrl("http://example.com");
Object res = fmt.format(new Passage[] { p }, DOC);
assertNotNull(res, "format() result");
final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span " + "class=\"l\">6</span> ----<b>Maecenas</b> vitae " + "lacus velit varius vulputate ipsum sed laoreet. " + "Nam maximus libero non ornare eg…</a><br/>";
String ctx = res.toString();
assertLinesEqual("format().toString()", DOCCTX_0, ctx);
// Second, test with contextCount==1
args = new ContextArgs((short) 1, (short) 10);
fmt = new ContextFormatter(args);
fmt.setUrl("http://example.com");
res = fmt.format(new Passage[] { p }, DOC);
assertNotNull(res, "format() result");
final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#5\"><span " + "class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span " + "class=\"l\">6</span> ----<b>Maecenas</b> vitae " + "lacus velit varius vulputate ipsum sed laoreet. " + "Nam maximus libero non ornare eg…</a><br/>" + "<a class=\"s\" href=\"http://example.com#7\"><span " + "class=\"l\">7</span> </a><br/>";
ctx = res.toString();
assertLinesEqual("format().toString()", DOCCTX_1, ctx);
// Third, test with contextCount==1 and a line limit
args = new ContextArgs((short) 1, (short) 10);
fmt = new ContextFormatter(args);
fmt.setUrl("http://example.com");
fmt.setMoreLimit(2);
fmt.setMoreUrl("http://example.com/more");
res = fmt.format(new Passage[] { p }, DOC);
assertNotNull(res, "format() result");
final String DOCCTX_2M = "<a class=\"s\" href=\"http://example.com#5\"><span " + "class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span " + "class=\"l\">6</span> ----<b>Maecenas</b> vitae " + "lacus velit varius vulputate ipsum sed laoreet. " + "Nam maximus libero non ornare eg…</a><br/>" + "<a href=\"http://example.com/more\">[all " + "…]</a><br/>\n";
ctx = res.toString();
assertLinesEqual("format().toString()", DOCCTX_2M, ctx);
}
use of org.apache.lucene.search.uhighlight.Passage in project OpenGrok by OpenGrok.
the class PassageConverter method convert.
/**
* Converts the specified passages into a sorted map of
* {@link LineHighlight} instances keyed by line offsets.
* @param passages a defined instance
* @param splitter a defined instance
* @return a defined instance
*/
public SortedMap<Integer, LineHighlight> convert(Passage[] passages, SourceSplitter splitter) {
SortedMap<Integer, LineHighlight> res = new TreeMap<>();
for (Passage passage : passages) {
int start = passage.getStartOffset();
int end = passage.getEndOffset();
if (start >= end) {
continue;
}
int m = splitter.findLineIndex(start);
if (m < 0) {
continue;
}
int n = splitter.findLineIndex(end - 1);
if (n < 0) {
continue;
}
m = Math.max(0, m - args.getContextSurround());
n = Math.min(splitter.count() - 1, n + args.getContextSurround());
// Ensure an entry in `res' for every passage line.
for (int i = m; i <= n; ++i) {
if (!res.containsKey(i)) {
res.put(i, new LineHighlight(i));
}
}
// Create LineHighlight entries for passage matches.
for (int i = 0; i < passage.getNumMatches(); ++i) {
int mstart = passage.getMatchStarts()[i];
int mm = splitter.findLineIndex(mstart);
int mend = passage.getMatchEnds()[i];
int nn = splitter.findLineIndex(mend - 1);
if (mstart < mend && mm >= m && mm <= n && nn >= m && nn <= n) {
if (mm == nn) {
int lbeg = splitter.getOffset(mm);
int lstart = mstart - lbeg;
int lend = mend - lbeg;
LineHighlight lhigh = res.get(mm);
lhigh.addMarkup(PhraseHighlight.create(lstart, lend));
} else {
int lbeg = splitter.getOffset(mm);
int loff = mstart - lbeg;
LineHighlight lhigh = res.get(mm);
lhigh.addMarkup(PhraseHighlight.createStarter(loff));
lbeg = splitter.getOffset(nn);
loff = mend - lbeg;
lhigh = res.get(nn);
lhigh.addMarkup(PhraseHighlight.createEnder(loff));
/*
* Designate any intermediate lines as
* wholly-highlighted
*/
for (int j = mm + 1; j <= nn - 1; ++j) {
lhigh = res.get(j);
lhigh.addMarkup(PhraseHighlight.createEntire());
}
}
}
}
}
/*
* Condense PhraseHighlight instances within lines, and elide as
* necessary to the reportable length.
*/
for (LineHighlight lhi : res.values()) {
lhi.condenseMarkups();
String line = splitter.getLine(lhi.getLineno());
Matcher eolMatcher = StringUtils.STANDARD_EOL.matcher(line);
if (eolMatcher.find()) {
line = line.substring(0, eolMatcher.start());
}
elideLine(lhi, line);
}
return res;
}
Aggregations