Example usage for java.text BreakIterator following

List of usage examples for java.text BreakIterator following

Introduction

In this page you can find the example usage for java.text BreakIterator following.

Prototype

public abstract int following(int offset);

Source Link

Document

Returns the first boundary following the specified character offset.

Usage

From source file:com.arthackday.killerapp.util.Util.java

public static String truncate(String text, int charLimit) {
    if (text.length() > charLimit) {
        BreakIterator bi = BreakIterator.getWordInstance();
        bi.setText(text);//from www.  java 2 s . c  om
        int cutOff = bi.following(charLimit);
        text = text.substring(0, cutOff) + " ...";
    }
    return text;
}

From source file:org.eclipse.fx.core.text.TextUtil.java

/**
 * Find the end offset of the word/*ww w  . j av a  2s.c  om*/
 *
 * @param content
 *            the content
 * @param offset
 *            the offset to start the search from
 * @param pointAsBoundary
 *            should the '.' treated as word boundary
 * @return the end offset or {@link BreakIterator#DONE}
 */
public static int findWordEndOffset(IterableCharSequence content, int offset, boolean pointAsBoundary) {
    BreakIterator wordInstance = BreakIterator.getWordInstance();
    wordInstance.setText(content.getIterator());
    int rv = wordInstance.following(offset);
    if (rv != BreakIterator.DONE && pointAsBoundary) {
        String s = content.subSequence(offset, rv).toString();
        int idx = s.indexOf('.');
        if (idx >= 0) {
            rv = offset + idx;
        }

        if (rv == offset) {
            rv = offset + 1;
        }
    }
    return rv;
}

From source file:org.eclipse.fx.core.text.TextUtil.java

/**
 * Find the bounds of the word//from   w ww .  ja  va  2s. c o m
 *
 * @param content
 *            the content
 * @param offset
 *            the offset
 * @param pointAsBoundary
 *            should the '.' treated as word boundary
 * @return a tuple of value representing start and end
 */
public static IntTuple findWordBounds(IterableCharSequence content, int offset, boolean pointAsBoundary) {
    BreakIterator wordInstance = BreakIterator.getWordInstance();
    wordInstance.setText(content.getIterator());
    int previous = wordInstance.preceding(offset);
    int next = wordInstance.following(offset);

    if (pointAsBoundary && previous != BreakIterator.DONE && next != BreakIterator.DONE) {
        String preMatch = content.subSequence(previous, offset).toString();
        String postMatch = content.subSequence(offset, next).toString();

        int idx = preMatch.lastIndexOf('.');
        if (idx > 0) {
            previous += idx + 1;
        }

        idx = postMatch.indexOf('.');
        if (idx > 0) {
            next = offset + idx;
        }
    }

    return new IntTuple(previous, next);
}

From source file:net.bible.service.device.speak.SpeakTextProvider.java

private StartPos getForwardTextStartPos(String text, float fraction) {
    StartPos retVal = new StartPos();

    int allTextLength = text.length();
    int nextTextOffset = (int) (Math.min(1, fraction) * allTextLength);

    BreakIterator breakIterator = BreakIterator.getSentenceInstance();
    breakIterator.setText(text);/*from w  w  w  .  j  a  v  a2  s .  c  o  m*/
    int startPos = 0;
    try {
        // this can rarely throw an Exception
        startPos = breakIterator.following(nextTextOffset);
    } catch (Exception e) {
        Log.e(TAG, "Error finding next sentence start", e);
    }
    retVal.found = startPos >= 0;

    if (retVal.found) {
        // nudge the startPos past the beginning of sentence so this sentence start is found when searching for previous block in getNextSentence
        retVal.startPosition = startPos < text.length() - 1 - 1 ? startPos + 1 : startPos;

        // because we don't return an exact fraction, but go to the beginning of a sentence, we need to update the fractionAlreadySpoken  
        retVal.actualFractionOfWhole = ((float) retVal.startPosition) / allTextLength;

        retVal.text = text.substring(retVal.startPosition);
    }
    return retVal;
}

From source file:com.bellman.bible.service.device.speak.SpeakTextProvider.java

private StartPos getForwardTextStartPos(String text, float fraction) {
    StartPos retVal = new StartPos();

    int allTextLength = text.length();
    int nextTextOffset = (int) (Math.min(1, fraction) * allTextLength);

    BreakIterator breakIterator = BreakIterator.getSentenceInstance();
    breakIterator.setText(text);/*from ww  w.ja  va 2s  . com*/
    int startPos = 0;
    try {
        // this can rarely throw an Exception
        startPos = breakIterator.following(nextTextOffset);
    } catch (Exception e) {
        Log.e(TAG, "Error finding next sentence start", e);
    }
    retVal.found = startPos >= 0;

    if (retVal.found) {
        // nudge the startPos past the beginning of sentence so this sentence start is found when searching for previous block in getNextSentence
        retVal.startPosition = startPos < text.length() - 1 - 1 ? startPos + 1 : startPos;

        // because we don't return an exact fraction, but go to the beginning of a sentence, we need to update the fractionAlreadySpoken
        retVal.actualFractionOfWhole = ((float) retVal.startPosition) / allTextLength;

        retVal.text = text.substring(retVal.startPosition);
    }
    return retVal;
}