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;
}
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;
}
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;
}
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();
}
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);
}
Aggregations