Example usage for java.text BreakIterator setText

List of usage examples for java.text BreakIterator setText

Introduction

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

Prototype

public abstract void setText(CharacterIterator newText);

Source Link

Document

Set a new text for scanning.

Usage

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

private StartPos getPrevTextStartPos(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);
    int startPos = 0;
    try {//from w ww. j  a  va  2s  . co m
        // this can rarely throw an Exception
        startPos = breakIterator.preceding(nextTextOffset);
    } catch (Exception e) {
        Log.e(TAG, "Error finding previous sentence start", e);
    }
    retVal.found = startPos >= 0;

    if (retVal.found) {
        retVal.startPosition = 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: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);
    int startPos = 0;
    try {// w  w  w .j a  v  a 2 s. co  m
        // 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 getPrevTextStartPos(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);
    int startPos = 0;
    try {//from ww  w  .  j ava 2  s. c o m
        // this can rarely throw an Exception
        startPos = breakIterator.preceding(nextTextOffset);
    } catch (Exception e) {
        Log.e(TAG, "Error finding previous sentence start", e);
    }
    retVal.found = startPos >= 0;

    if (retVal.found) {
        retVal.startPosition = 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);
    int startPos = 0;
    try {/*from  w w  w  .  jav a 2s.  c  o  m*/
        // 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:pl.edu.icm.coansys.kwdextraction.RakeExtractor.java

/**
 * Finding words or word sequences separated by stopwords, punctuation marks
 * etc.//from w w  w .ja v  a2 s  .  c  om
 */
private void extractKeywordCandidates() {

    Map<String, KeywordCandidate> candidatesMap = new HashMap<String, KeywordCandidate>();

    BreakIterator wordIterator = BreakIterator.getWordInstance();

    wordIterator.setText(content);
    int wordStart = wordIterator.first();

    int candidateStart = wordStart;
    String candidateStr = null;
    KeywordCandidate kwdCand = new KeywordCandidate();

    for (int wordEnd = wordIterator
            .next(); wordEnd != BreakIterator.DONE; wordStart = wordEnd, wordEnd = wordIterator.next()) {

        String word = content.substring(wordStart, wordEnd).trim().toLowerCase();
        String alpha = word.replaceAll(ILLEGAL_CHARS, "");

        if (!word.isEmpty()) {

            if (stopwords.get(lang).contains(word) || word.matches("\\W+") || isNum(word)
                    || !word.equals(alpha)) {
                candidateStr = content.substring(candidateStart, wordStart);
            } else {
                kwdCand.addWord(word);
                if (wordEnd == content.length()) {
                    candidateStr = content.substring(candidateStart, wordEnd);
                }
            }
            if (candidateStr != null) {
                candidateStr = candidateStr.trim().toLowerCase().replaceAll(ILLEGAL_CHARS, "")
                        .replaceAll("\\s+", " ");
                if (!candidateStr.isEmpty()) {
                    if (candidatesMap.containsKey(candidateStr)) {
                        candidatesMap.get(candidateStr).incCounter();
                    } else {
                        kwdCand.setKeyword(candidateStr);
                        candidatesMap.put(candidateStr, kwdCand);
                    }
                }
                candidateStr = null;
                candidateStart = wordEnd;
                kwdCand = new KeywordCandidate();
            }
        }
    }

    keywordCandidates = new ArrayList<KeywordCandidate>();
    for (Entry<String, KeywordCandidate> e : candidatesMap.entrySet()) {
        keywordCandidates.add(e.getValue());
    }
}

From source file:IteratorTest.java

protected void refreshDisplay() {
    int startIndex, nextIndex;
    Vector items = new Vector();
    String msgText = textArea.getText();
    Locale locale = (Locale) (localeButton.getSelectedItem());
    BreakIterator iterator = null;
    if (charButton.isSelected()) {
        iterator = BreakIterator.getCharacterInstance(locale);
    } else if (wordButton.isSelected()) {
        iterator = BreakIterator.getWordInstance(locale);
    } else if (lineButton.isSelected()) {
        iterator = BreakIterator.getLineInstance(locale);
    } else if (sentButton.isSelected()) {
        iterator = BreakIterator.getSentenceInstance(locale);
    }//  w ww.  j av a  2s.  c  o  m
    iterator.setText(msgText);
    startIndex = iterator.first();
    nextIndex = iterator.next();

    while (nextIndex != BreakIterator.DONE) {
        items.addElement(msgText.substring(startIndex, nextIndex));
        startIndex = nextIndex;
        nextIndex = iterator.next();
    }
    itemList.setListData(items);
}

From source file:Utils.java

/**
 * Wrap multi-line strings (and get the individual lines).
 * //from ww  w  .ja va 2 s .  c  om
 * @param original
 *          the original string to wrap
 * @param width
 *          the maximum width of lines
 * @param breakIterator
 *          breaks original to chars, words, sentences, depending on what
 *          instance you provide.
 * @param removeNewLines
 *          if <code>true</code>, any newlines in the original string are
 *          ignored
 * @return the lines after wrapping
 */
public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator,
        boolean removeNewLines) {
    if (original.length() == 0) {
        return new String[] { original };
    }

    String[] workingSet;

    // substitute original newlines with spaces,
    // remove newlines from head and tail
    if (removeNewLines) {
        original = trimString(original);
        original = original.replace('\n', ' ');
        workingSet = new String[] { original };
    } else {
        StringTokenizer tokens = new StringTokenizer(original, "\n"); // NOI18N
        int len = tokens.countTokens();
        workingSet = new String[len];

        for (int i = 0; i < len; i++) {
            workingSet[i] = tokens.nextToken();
        }
    }

    if (width < 1) {
        width = 1;
    }

    if (original.length() <= width) {
        return workingSet;
    }

    widthcheck: {
        boolean ok = true;

        for (int i = 0; i < workingSet.length; i++) {
            ok = ok && (workingSet[i].length() < width);

            if (!ok) {
                break widthcheck;
            }
        }

        return workingSet;
    }

    java.util.ArrayList<String> lines = new java.util.ArrayList<String>();

    int lineStart = 0; // the position of start of currently processed line in
                       // the original string

    for (int i = 0; i < workingSet.length; i++) {
        if (workingSet[i].length() < width) {
            lines.add(workingSet[i]);
        } else {
            breakIterator.setText(workingSet[i]);

            int nextStart = breakIterator.next();
            int prevStart = 0;

            do {
                while (((nextStart - lineStart) < width) && (nextStart != BreakIterator.DONE)) {
                    prevStart = nextStart;
                    nextStart = breakIterator.next();
                }

                if (nextStart == BreakIterator.DONE) {
                    nextStart = prevStart = workingSet[i].length();
                }

                if (prevStart == 0) {
                    prevStart = nextStart;
                }

                lines.add(workingSet[i].substring(lineStart, prevStart));

                lineStart = prevStart;
                prevStart = 0;
            } while (lineStart < workingSet[i].length());

            lineStart = 0;
        }
    }

    String[] s = new String[lines.size()];

    return (String[]) lines.toArray(s);
}

From source file:net.sf.jasperreports.engine.fill.TextMeasurer.java

protected void appendTruncateSuffix(TextLineWrapper lineWrapper) {
    String truncateSuffx = getTruncateSuffix();
    if (truncateSuffx == null) {
        return;// w w w  .  j  av a 2 s. c o m
    }

    int lineStart = prevMeasuredState.textOffset;

    //advance from the line start until the next line start or the first newline
    String lineText = lineWrapper.getLineText(lineStart, measuredState.textOffset);
    int linePosition = lineText.length();

    //iterate to the beginning of the line
    boolean done = false;
    do {
        measuredState = prevMeasuredState.cloneState();

        String text = lineText.substring(0, linePosition) + truncateSuffx;
        boolean truncateAtChar = isToTruncateAtChar();
        TextLineWrapper lastLineWrapper = lineWrapper.lastLineWrapper(text, measuredState.textOffset,
                linePosition, truncateAtChar);

        BreakIterator breakIterator = truncateAtChar ? BreakIterator.getCharacterInstance()
                : BreakIterator.getLineInstance();
        breakIterator.setText(text);

        if (renderNextLine(lastLineWrapper, null, new int[] { 0 }, new TabStop[] { null },
                new boolean[] { false })) {
            int lastPos = lastLineWrapper.paragraphPosition();
            //test if the entire suffix fit
            if (lastPos == linePosition + truncateSuffx.length()) {
                //subtract the suffix from the offset
                measuredState.textOffset -= truncateSuffx.length();
                measuredState.textSuffix = truncateSuffx;
                done = true;
            } else {
                linePosition = breakIterator.preceding(linePosition);
                if (linePosition == BreakIterator.DONE) {
                    //if the text suffix did not fit the line, only the part of it that fits will show

                    //truncate the suffix
                    String actualSuffix = truncateSuffx.substring(0,
                            measuredState.textOffset - prevMeasuredState.textOffset);
                    //if the last text char is not a new line
                    if (prevMeasuredState.textOffset > 0
                            && lineWrapper.charAt(prevMeasuredState.textOffset - 1) != '\n') {
                        //force a new line so that the suffix is displayed on the last line
                        actualSuffix = '\n' + actualSuffix;
                    }
                    measuredState.textSuffix = actualSuffix;

                    //restore the next to last line offset
                    measuredState.textOffset = prevMeasuredState.textOffset;

                    done = true;
                }
            }
        } else {
            //if the line did not fit, leave it empty
            done = true;
        }
    } while (!done);
}

From source file:org.jivesoftware.community.util.StringUtils.java

public static String[] toLowerCaseWordArray(String text) {
    if (text == null || text.length() == 0)
        return new String[0];
    ArrayList wordList = new ArrayList();
    BreakIterator boundary = BreakIterator.getWordInstance();
    boundary.setText(text);
    int start = 0;
    for (int end = boundary.next(); end != -1; end = boundary.next()) {
        String tmp = text.substring(start, end).trim();
        tmp = replace(tmp, "+", "");
        tmp = replace(tmp, "/", "");
        tmp = replace(tmp, "\\", "");
        tmp = replace(tmp, "#", "");
        tmp = replace(tmp, "*", "");
        tmp = replace(tmp, ")", "");
        tmp = replace(tmp, "(", "");
        tmp = replace(tmp, "&", "");
        if (tmp.length() > 0)
            wordList.add(tmp);/*from w  ww. j ava  2 s  . co  m*/
        start = end;
    }

    return (String[]) wordList.toArray(new String[wordList.size()]);
}

From source file:org.jivesoftware.community.util.StringUtils.java

public static String wordWrap(String input, int width, Locale locale) {
    if (input == null)
        return "";
    if (width < 5)
        return input;
    if (width >= input.length())
        return input;
    if (locale == null)
        locale = JiveGlobals.getLocale();
    StringBuffer buf = new StringBuffer(input);
    boolean endOfLine = false;
    int lineStart = 0;
    for (int i = 0; i < buf.length(); i++) {
        if (buf.charAt(i) == '\n') {
            lineStart = i + 1;/*w  w  w .j ava 2  s .  c o  m*/
            endOfLine = true;
        }
        if (i <= (lineStart + width) - 1)
            continue;
        if (!endOfLine) {
            int limit = i - lineStart - 1;
            BreakIterator breaks = BreakIterator.getLineInstance(locale);
            breaks.setText(buf.substring(lineStart, i));
            int end = breaks.last();
            if (end == limit + 1 && !Character.isWhitespace(buf.charAt(lineStart + end)))
                end = breaks.preceding(end - 1);
            if (end != -1 && end == limit + 1) {
                buf.replace(lineStart + end, lineStart + end + 1, "\n");
                lineStart += end;
                continue;
            }
            if (end != -1 && end != 0) {
                buf.insert(lineStart + end, '\n');
                lineStart = lineStart + end + 1;
            } else {
                buf.insert(i, '\n');
                lineStart = i + 1;
            }
        } else {
            buf.insert(i, '\n');
            lineStart = i + 1;
            endOfLine = false;
        }
    }

    return buf.toString();
}