List of usage examples for org.apache.lucene.search.uhighlight Passage getNumMatches
public int getNumMatches()
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testLineMatchFormatted() { final String WORD = "gravida"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);//from w w w . j a va 2 s . c om Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.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("format() result", res); 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("format() result", res); 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); }
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testLinesSpanningMatchFormatted() { Passage p = new Passage(); p.setStartOffset(0);//from w ww.j a v a2 s . c om p.setEndOffset(DOC2.length()); p.addMatch(0, p.getEndOffset(), new BytesRef(DOC2), 1); assertEquals("getNumMatches()", 1, p.getNumMatches()); /** * We're using the entire document, but see how it behaves with * contextCount==1 */ ContextArgs args = new ContextArgs((short) 1, (short) 10); ContextFormatter fmt = new ContextFormatter(args); fmt.setUrl("http://example.com"); Object res = fmt.format(new Passage[] { p }, DOC2); assertNotNull("format() result", res); final String DOC2CTX = "<a class=\"s\" href=\"http://example.com#1\"><span class=\"l\">" + "1</span> <b>abc</b></a><br/>" + "<a class=\"s\" href=\"http://example.com#2\"><span class=\"l\">" + "2</span> <b>def</b></a><br/>" + "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">" + "3</span> <b>ghi</b></a><br/>"; String ctx = res.toString(); assertLinesEqual("format().toString()", DOC2CTX, ctx); }
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testDualElidedMatchFormatted() { final String WORD = "dignissim"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);//from ww w . j a va 2s .c o m Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.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("format() result", res); final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span class=\"l\">" + "6</span> …putate ipsum sed laoreet. Nam maximus libero" + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus…</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("format() result", res); 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> …putate ipsum sed laoreet. Nam maximus libero" + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus…</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("format() result", res); 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> …putate ipsum sed laoreet. Nam maximus libero" + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus…</a><br/>" + "<a href=\"http://example.com/more\">[all …]</a><br/>"; ctx = res.toString(); assertLinesEqual("format().toString()", DOCCTX_2M, ctx); }
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testLeftElidedMatchFormatted() { final String WORD = "ultricies"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);//w ww . j a va2s . com Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.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("format() result", res); final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span " + "class=\"l\">6</span> …um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean " + "dignissim ipsum eu rhoncus <b>ultricies</b>.</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("format() result", res); 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> …um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean " + "dignissim ipsum eu rhoncus <b>ultricies</b>.</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("format() result", res); 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> …um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean " + "dignissim ipsum eu rhoncus <b>ultricies</b>.</a>" + "<br/><a href=\"http://example.com/more\">[all " + "…]</a><br/>"; ctx = res.toString(); assertLinesEqual("format().toString()", DOCCTX_2M, ctx); }
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testRightElidedMatchFormatted() { final String WORD = "Maecenas"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);//from www . ja va2 s . c o m Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.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("format() result", res); 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("format() result", res); 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("format() result", res); 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); }
From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java
License:Open Source License
@Test public void testBoundsProblemFormatted() { final String PHRASE = "efficitur vitae"; int phOff = DOC.indexOf(PHRASE); assertTrue(PHRASE, phOff >= 0);//from www.j a va 2 s . c o m // Create a slightly-longer word of all '*'. final int LF_CHAR_COUNT = 1; final String STARS = String.join("", Collections.nCopies(PHRASE.length() + LF_CHAR_COUNT, "*")); Passage p = new Passage(); p.setStartOffset(phOff); p.setEndOffset(phOff + STARS.length()); p.addMatch(phOff, p.getEndOffset(), new BytesRef(STARS), 1); assertEquals("getNumMatches()", 1, p.getNumMatches()); // 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("format() result", res); final String DOC_CTX_0 = "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">" + "3</span> Mauris diam nisl, tincidunt nec gravida sit" + " amet, <b>efficitur vitae</b></a><br/>\n"; String ctx = res.toString(); assertLinesEqual("format().toString()", DOC_CTX_0, ctx); }
From source file:org.opengrok.indexer.search.context.PassageConverter.java
License:Open Source License
/** * Converts the specified passages into a sorted map of * {@link LineHighlight} instances keyed by line offsets. * @param passages a defined instance//from www .j a v a 2 s . c o m * @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.findLineOffset(start); if (m < 0) { continue; } int n = splitter.findLineOffset(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.findLineOffset(mstart); int mend = passage.getMatchEnds()[i]; int nn = splitter.findLineOffset(mend - 1); if (mstart < mend && mm >= m && mm <= n && nn >= m && nn <= n) { if (mm == nn) { int lbeg = splitter.getPosition(mm); int lstart = mstart - lbeg; int lend = mend - lbeg; LineHighlight lhigh = res.get(mm); lhigh.addMarkup(PhraseHighlight.create(lstart, lend)); } else { int lbeg = splitter.getPosition(mm); int loff = mstart - lbeg; LineHighlight lhigh = res.get(mm); lhigh.addMarkup(PhraseHighlight.createStarter(loff)); lbeg = splitter.getPosition(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; }
From source file:org.opengrok.indexer.search.context.PassageConverterTest.java
License:Open Source License
@Test public void testOneWord() { final String WORD = "gravida"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);/*from w w w . j ava 2 s . com*/ Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.getNumMatches()); PassageConverter cvt = getConverter((short) 0); SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter); assertEquals("linemap size()", 1, linemap.size()); int lineno = linemap.firstKey(); assertEquals("lineno", 2, lineno); LineHighlight lhi = linemap.get(lineno); assertNotNull("get LineHighlight", lhi); assertEquals("getLelide()", 0, lhi.getLelide()); assertEquals("getRelide()", 0, lhi.getRelide()); assertEquals("countMarkups()", 1, lhi.countMarkups()); PhraseHighlight phi = lhi.getMarkup(0); assertNotNull("get PhraseHighlight", phi); assertEquals("getLineStart()", 32, phi.getLineStart()); assertEquals("getLineEnd()", 32 + WORD.length(), phi.getLineEnd()); }
From source file:org.opengrok.indexer.search.context.PassageConverterTest.java
License:Open Source License
@Test public void testOneWordElided() { final String WORD = "dignissim"; int woff = DOC.indexOf(WORD); assertTrue(WORD, woff >= 0);// ww w. j a va2 s.c o m Passage p = new Passage(); p.setStartOffset(woff); p.setEndOffset(woff + WORD.length()); p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1); assertEquals("getNumMatches()", 1, p.getNumMatches()); PassageConverter cvt = getConverter((short) 0); SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter); assertEquals("linemap size()", 1, linemap.size()); int lineno = linemap.firstKey(); assertEquals("lineno", 5, lineno); LineHighlight lhi = linemap.get(lineno); assertNotNull("get LineHighlight", lhi); assertEquals("getLelide()", 41, lhi.getLelide()); assertEquals("getRelide()", 139, lhi.getRelide()); assertEquals("context width minus 2", 139 - 41, cvt.getArgs().getContextWidth() - 2); assertEquals("countMarkups()", 1, lhi.countMarkups()); PhraseHighlight phi = lhi.getMarkup(0); assertNotNull("get PhraseHighlight", phi); assertEquals("getLineStart()", 113, phi.getLineStart()); assertEquals("getLineEnd()", 113 + WORD.length(), phi.getLineEnd()); }
From source file:org.opengrok.indexer.search.context.PassageConverterTest.java
License:Open Source License
@Test public void testTwoWordsElided() { final String WORD1 = "Maecenas"; int woff1 = DOC.indexOf(WORD1); assertTrue(WORD1, woff1 >= 0);// w w w . j a v a2 s . c o m final String WORD2 = "rhoncus"; int woff2 = DOC.indexOf(WORD2); assertTrue(WORD2, woff2 >= 0); Passage p = new Passage(); p.setStartOffset(woff1); p.setEndOffset(woff2 + WORD2.length()); p.addMatch(woff1, woff1 + WORD1.length(), new BytesRef(WORD1), 1); p.addMatch(woff2, woff2 + WORD2.length(), new BytesRef(WORD2), 1); assertEquals("getNumMatches()", 2, p.getNumMatches()); PassageConverter cvt = getConverter((short) 0); SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter); assertEquals("linemap size()", 1, linemap.size()); int lineno = linemap.firstKey(); assertEquals("lineno", 5, lineno); LineHighlight lhi = linemap.get(lineno); assertNotNull("get LineHighlight", lhi); assertEquals("getLelide()", 4, lhi.getLelide()); assertEquals("getRelide()", 102, lhi.getRelide()); assertEquals("context width minus 2", 139 - 41, cvt.getArgs().getContextWidth() - 2); assertEquals("countMarkups()", 2, lhi.countMarkups()); PhraseHighlight phi = lhi.getMarkup(0); assertNotNull("get PhraseHighlight", phi); assertEquals("0:getLineStart()", 4, phi.getLineStart()); assertEquals("0:getLineEnd()", 4 + WORD1.length(), phi.getLineEnd()); phi = lhi.getMarkup(1); assertNotNull("get PhraseHighlight", phi); assertEquals("1:getLineStart()", 132, phi.getLineStart()); assertEquals("1:getLineEnd()", 132 + WORD2.length(), phi.getLineEnd()); }