Search in sources :

Example 81 with BreakIterator

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

the class TextServiceImpl method countCharacters.

private int countCharacters(String text, Locale locale) {
    if (locale == null) {
        locale = I18nModule.getDefaultLocale();
    }
    int count = 0;
    BreakIterator characterIterator = BreakIterator.getCharacterInstance(locale);
    characterIterator.setText(text);
    int start = characterIterator.first();
    int end = characterIterator.next();
    while (end != BreakIterator.DONE) {
        char ch = text.charAt(start);
        if (Character.isLetterOrDigit(ch)) {
            count++;
        }
        start = end;
        end = characterIterator.next();
    }
    return count;
}
Also used : BreakIterator(java.text.BreakIterator)

Example 82 with BreakIterator

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

the class TextServiceImpl method countWords.

private int countWords(String text, Locale locale) {
    int count = 0;
    BreakIterator wordIterator = BreakIterator.getWordInstance(locale);
    wordIterator.setText(text);
    int start = wordIterator.first();
    int end = wordIterator.next();
    while (end != BreakIterator.DONE) {
        char ch = text.charAt(start);
        if (Character.isLetterOrDigit(ch)) {
            count++;
        }
        start = end;
        end = wordIterator.next();
    }
    return count;
}
Also used : BreakIterator(java.text.BreakIterator)

Example 83 with BreakIterator

use of java.text.BreakIterator in project Bytecoder by mirkosertic.

the class ConditionalSpecialCasing method isFinalCased.

/**
 * Implements the "Final_Cased" condition
 *
 * Specification: Within the closest word boundaries containing C, there is a cased
 * letter before C, and there is no cased letter after C.
 *
 * Regular Expression:
 *   Before C: [{cased==true}][{wordBoundary!=true}]*
 *   After C: !([{wordBoundary!=true}]*[{cased}])
 */
private static boolean isFinalCased(String src, int index, Locale locale) {
    BreakIterator wordBoundary = BreakIterator.getWordInstance(locale);
    wordBoundary.setText(src);
    int ch;
    // Look for a preceding 'cased' letter
    for (int i = index; (i >= 0) && !wordBoundary.isBoundary(i); i -= Character.charCount(ch)) {
        ch = src.codePointBefore(i);
        if (isCased(ch)) {
            int len = src.length();
            // Check that there is no 'cased' letter after the index
            for (i = index + Character.charCount(src.codePointAt(index)); (i < len) && !wordBoundary.isBoundary(i); i += Character.charCount(ch)) {
                ch = src.codePointAt(i);
                if (isCased(ch)) {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : BreakIterator(java.text.BreakIterator)

Example 84 with BreakIterator

use of java.text.BreakIterator in project RichTextFX by FXMisc.

the class SpellChecking method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(text);
    int lastIndex = wb.first();
    int lastKwEnd = 0;
    while (lastIndex != BreakIterator.DONE) {
        int firstIndex = lastIndex;
        lastIndex = wb.next();
        if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
            String word = text.substring(firstIndex, lastIndex).toLowerCase();
            if (!dictionary.contains(word)) {
                spansBuilder.add(Collections.emptyList(), firstIndex - lastKwEnd);
                spansBuilder.add(Collections.singleton("underlined"), lastIndex - firstIndex);
                lastKwEnd = lastIndex;
            }
            System.err.println();
        }
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder) BreakIterator(java.text.BreakIterator)

Example 85 with BreakIterator

use of java.text.BreakIterator in project RichTextFX by FXMisc.

the class CaretSelectionBindImpl method selectWord.

@Override
public void selectWord(int wordPositionInArea) {
    if (getAreaLength() == 0) {
        return;
    }
    BreakIterator breakIterator = BreakIterator.getWordInstance();
    breakIterator.setText(getArea().getText());
    int start = calculatePositionViaBreakingBackwards(1, breakIterator, wordPositionInArea);
    int end = calculatePositionViaBreakingForwards(1, breakIterator, wordPositionInArea);
    selectRange(start, end);
}
Also used : 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