Example usage for org.apache.lucene.search.uhighlight Passage getStartOffset

List of usage examples for org.apache.lucene.search.uhighlight Passage getStartOffset

Introduction

In this page you can find the example usage for org.apache.lucene.search.uhighlight Passage getStartOffset.

Prototype

public int getStartOffset() 

Source Link

Document

Start offset of this passage.

Usage

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 w w w.  j av a 2 s.  com
 * @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.opensolaris.opengrok.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  w  w  w  . j av  a 2s.  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());
        elideLine(lhi, line);
    }

    return res;
}