Search in sources :

Example 76 with BreakIterator

use of java.text.BreakIterator in project iNGAGE by davis123123.

the class RecentCommentsAdapter method truncateText.

private String truncateText(String text, int maxLen) {
    if (text != null && text.length() > maxLen) {
        BreakIterator bi = BreakIterator.getWordInstance();
        bi.setText(text);
        if (bi.isBoundary(maxLen - 1)) {
            text = text.substring(0, maxLen - 2);
        } else {
            int preceding = bi.preceding(maxLen - 1);
            text = text.substring(0, preceding - 1);
        }
        text = text.concat("...");
        return text;
    } else {
        return text;
    }
}
Also used : BreakIterator(java.text.BreakIterator)

Example 77 with BreakIterator

use of java.text.BreakIterator in project jdk8u_jdk by JetBrains.

the class GlyphView method getBreakSpot.

/**
     * Returns a location to break at in the passed in region, or
     * BreakIterator.DONE if there isn't a good location to break at
     * in the specified region.
     */
private int getBreakSpot(int p0, int p1) {
    if (breakSpots == null) {
        // Re-calculate breakpoints for the whole view
        int start = getStartOffset();
        int end = getEndOffset();
        int[] bs = new int[end + 1 - start];
        int ix = 0;
        // Breaker should work on the parent element because there may be
        // a valid breakpoint at the end edge of the view (space, etc.)
        Element parent = getElement().getParentElement();
        int pstart = (parent == null ? start : parent.getStartOffset());
        int pend = (parent == null ? end : parent.getEndOffset());
        Segment s = getText(pstart, pend);
        s.first();
        BreakIterator breaker = getBreaker();
        breaker.setText(s);
        // Backward search should start from end+1 unless there's NO end+1
        int startFrom = end + (pend > end ? 1 : 0);
        for (; ; ) {
            startFrom = breaker.preceding(s.offset + (startFrom - pstart)) + (pstart - s.offset);
            if (startFrom > start) {
                // The break spot is within the view
                bs[ix++] = startFrom;
            } else {
                break;
            }
        }
        SegmentCache.releaseSharedSegment(s);
        breakSpots = new int[ix];
        System.arraycopy(bs, 0, breakSpots, 0, ix);
    }
    int breakSpot = BreakIterator.DONE;
    for (int i = 0; i < breakSpots.length; i++) {
        int bsp = breakSpots[i];
        if (bsp <= p1) {
            if (bsp > p0) {
                breakSpot = bsp;
            }
            break;
        }
    }
    return breakSpot;
}
Also used : BreakIterator(java.text.BreakIterator)

Example 78 with BreakIterator

use of java.text.BreakIterator in project webanno by webanno.

the class ImportExportServiceImpl method splitSentences.

public static void splitSentences(JCas aJCas) {
    BreakIterator bi = BreakIterator.getSentenceInstance(Locale.US);
    bi.setText(aJCas.getDocumentText());
    int last = bi.first();
    int cur = bi.next();
    while (cur != BreakIterator.DONE) {
        int[] span = new int[] { last, cur };
        trim(aJCas.getDocumentText(), span);
        if (!isEmpty(span[0], span[1])) {
            Sentence seg = new Sentence(aJCas, span[0], span[1]);
            seg.addToIndexes(aJCas);
        }
        last = cur;
        cur = bi.next();
    }
}
Also used : Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) BreakIterator(java.text.BreakIterator)

Example 79 with BreakIterator

use of java.text.BreakIterator in project webanno by webanno.

the class ImportExportServiceImpl method tokenize.

public static void tokenize(JCas aJCas) {
    BreakIterator bi = BreakIterator.getWordInstance(Locale.US);
    for (Sentence s : select(aJCas, Sentence.class)) {
        bi.setText(s.getCoveredText());
        int last = bi.first();
        int cur = bi.next();
        while (cur != BreakIterator.DONE) {
            int[] span = new int[] { last, cur };
            trim(s.getCoveredText(), span);
            if (!isEmpty(span[0], span[1])) {
                Token seg = new Token(aJCas, span[0] + s.getBegin(), span[1] + s.getBegin());
                seg.addToIndexes(aJCas);
            }
            last = cur;
            cur = bi.next();
        }
    }
}
Also used : Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) BreakIterator(java.text.BreakIterator)

Example 80 with BreakIterator

use of java.text.BreakIterator in project cytoscape-impl by cytoscape.

the class FormulaBuilderDialog method wrapLabelText.

private void wrapLabelText(final JLabel label, final String text) {
    if (label.getPreferredSize() == null)
        return;
    final int maxWidth = label.getPreferredSize().width;
    final BreakIterator boundary = BreakIterator.getWordInstance();
    boundary.setText(text);
    final FontMetrics fm = label.getFontMetrics(label.getFont());
    final StringBuilder trial = new StringBuilder();
    final StringBuilder real = new StringBuilder("<html>");
    int start = boundary.first();
    for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
        final String word = text.substring(start, end);
        trial.append(word);
        int trialWidth = SwingUtilities.computeStringWidth(fm, trial.toString());
        if (trialWidth > maxWidth) {
            trial.setLength(0);
            trial.append(word);
            real.append("<br>");
        }
        real.append(word);
    }
    real.append("</html>");
    label.setText(real.toString());
}
Also used : FontMetrics(java.awt.FontMetrics) BreakIterator(java.text.BreakIterator)

Aggregations

BreakIterator (java.text.BreakIterator)120 ArrayList (java.util.ArrayList)17 Locale (java.util.Locale)9 Paint (android.graphics.Paint)4 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)3 BytesRef (org.apache.lucene.util.BytesRef)3 SuppressLint (android.annotation.SuppressLint)2 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)2 Collection (java.util.Collection)2 BadLocationException (javax.swing.text.BadLocationException)2 Document (javax.swing.text.Document)2 Element (javax.swing.text.Element)2 Segment (javax.swing.text.Segment)2 Snippet (org.apache.lucene.search.highlight.Snippet)2 Intent (android.content.Intent)1 RectF (android.graphics.RectF)1 TextPaint (android.text.TextPaint)1 TagElement (com.google.devtools.j2objc.ast.TagElement)1 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)1 AbstractNLPDecoder (edu.emory.mathcs.nlp.decode.AbstractNLPDecoder)1