本文整理汇总了Java中java.text.BreakIterator类的典型用法代码示例。如果您正苦于以下问题:Java BreakIterator类的具体用法?Java BreakIterator怎么用?Java BreakIterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BreakIterator类属于java.text包,在下文中一共展示了BreakIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLastAndPrevious
import java.text.BreakIterator; //导入依赖的package包/类
private Vector testLastAndPrevious(BreakIterator bi, String text) {
int p = bi.last();
int lastP = p;
Vector<String> result = new Vector<String>();
if (p != text.length())
errln("last() returned " + p + " instead of " + text.length());
while (p != BreakIterator.DONE) {
p = bi.previous();
if (p != BreakIterator.DONE) {
if (p >= lastP)
errln("previous() failed to move backward: previous() on position "
+ lastP + " yielded " + p);
result.insertElementAt(text.substring(p, lastP), 0);
}
else {
if (lastP != 0)
errln("previous() returned DONE prematurely: offset was "
+ lastP + " instead of 0");
}
lastP = p;
}
return result;
}
示例2: getFirstSentenceWithoutDot
import java.text.BreakIterator; //导入依赖的package包/类
@NotNull
public static String getFirstSentenceWithoutDot(String fullSentence) {
if (containsChar(fullSentence, '.')) {
BreakIterator breakIterator = getSentenceInstance(Locale.US);
breakIterator.setText(fullSentence);
fullSentence = fullSentence.substring(breakIterator.first(), breakIterator.next()).trim();
}
if (isNotEmpty(fullSentence)) {
String withoutDot = endsWithChar(fullSentence, '.') ?
fullSentence.substring(0, fullSentence.length() - 1) :
fullSentence;
return replace(withoutDot, "\n", "");
} else {
return "";
}
}
示例3: copy
import java.text.BreakIterator; //导入依赖的package包/类
private int copy(BreakIterator line, Text text, Text columnValue, int offset) {
// Deceive the BreakIterator to ensure no line breaks after '-' character
line.setText(text.plainString().replace("-", "\u00ff"));
int done = 0;
for (int start = line.first(), end = line.next(); end != BreakIterator.DONE; start = end, end = line.next()) {
Text word = text.substring(start, end); //.replace("\u00ff", "-"); // not needed
if (columnValue.maxLength >= offset + done + length(word)) {
done += copy(word, columnValue, offset + done); // TODO localized length
} else {
break;
}
}
if (done == 0 && length(text) > columnValue.maxLength) {
// The value is a single word that is too big to be written to the column. Write as much as we can.
done = copy(text, columnValue, offset);
}
return done;
}
示例4: TestBug4153072
import java.text.BreakIterator; //导入依赖的package包/类
public void TestBug4153072() {
BreakIterator iter = BreakIterator.getWordInstance();
String str = "...Hello, World!...";
int begin = 3;
int end = str.length() - 3;
boolean gotException = false;
boolean dummy;
iter.setText(new StringCharacterIterator(str, begin, end, begin));
for (int index = -1; index < begin + 1; ++index) {
try {
dummy = iter.isBoundary(index);
if (index < begin)
errln("Didn't get exception with offset = " + index +
" and begin index = " + begin);
}
catch (IllegalArgumentException e) {
if (index >= begin)
errln("Got exception with offset = " + index +
" and begin index = " + begin);
}
}
}
示例5: split
import java.text.BreakIterator; //导入依赖的package包/类
@Override
public String[] split(String text) {
boundary.setText(text);
ArrayList<String> sentences = new ArrayList<>();
int start = boundary.first();
for (int end = boundary.next();
end != BreakIterator.DONE;
start = end, end = boundary.next()) {
sentences.add(text.substring(start, end).trim());
}
String[] array = new String[sentences.size()];
for (int i = 0; i < array.length; i++) {
array[i] = sentences.get(i);
}
return array;
}
示例6: highlightMisspelled
import java.text.BreakIterator; //导入依赖的package包/类
private StyleSpans<Collection<String>> highlightMisspelled(String text) {
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(text);
int lastIndex = wb.first();
int lastKeywordEnd = 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 - lastKeywordEnd);
spansBuilder.add(Collections.singleton("underlined"), lastIndex - firstIndex);
lastKeywordEnd = lastIndex;
}
}
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKeywordEnd);
return spansBuilder.create();
}
示例7: piecesOfEmbeddedLine
import java.text.BreakIterator; //导入依赖的package包/类
private List<String> piecesOfEmbeddedLine( String line, int width ) {
List<String> pieces = new ArrayList<String>();
BreakIterator words = BreakIterator.getLineInstance( Locale.US );
words.setText( line );
StringBuilder nextPiece = new StringBuilder();
int start = words.first();
for ( int end = words.next(); end != DONE; start = end, end = words.next() )
nextPiece = processNextWord( line, nextPiece, start, end, width, pieces );
if ( nextPiece.length() > 0 )
pieces.add( nextPiece.toString() );
return pieces;
}
示例8: getBoundaryScanner
import java.text.BreakIterator; //导入依赖的package包/类
private static BoundaryScanner getBoundaryScanner(Field field) {
final FieldOptions fieldOptions = field.fieldOptions();
final Locale boundaryScannerLocale = fieldOptions.boundaryScannerLocale();
switch(fieldOptions.boundaryScannerType()) {
case SENTENCE:
if (boundaryScannerLocale != null) {
return new BreakIteratorBoundaryScanner(BreakIterator.getSentenceInstance(boundaryScannerLocale));
}
return DEFAULT_SENTENCE_BOUNDARY_SCANNER;
case WORD:
if (boundaryScannerLocale != null) {
return new BreakIteratorBoundaryScanner(BreakIterator.getWordInstance(boundaryScannerLocale));
}
return DEFAULT_WORD_BOUNDARY_SCANNER;
default:
if (fieldOptions.boundaryMaxScan() != SimpleBoundaryScanner.DEFAULT_MAX_SCAN
|| fieldOptions.boundaryChars() != SimpleBoundaryScanner.DEFAULT_BOUNDARY_CHARS) {
return new SimpleBoundaryScanner(fieldOptions.boundaryMaxScan(), fieldOptions.boundaryChars());
}
return DEFAULT_SIMPLE_BOUNDARY_SCANNER;
}
}
示例9: findWordLimit
import java.text.BreakIterator; //导入依赖的package包/类
/**
* Needed to unify forward and backward searching.
* The method assumes that s is the text assigned to words.
*/
private int findWordLimit(int index, BreakIterator words, boolean direction,
String s) {
// Fix for 4256660 and 4256661.
// Words iterator is different from character and sentence iterators
// in that end of one word is not necessarily start of another word.
// Please see java.text.BreakIterator JavaDoc. The code below is
// based on nextWordStartAfter example from BreakIterator.java.
int last = (direction == NEXT) ? words.following(index)
: words.preceding(index);
int current = (direction == NEXT) ? words.next()
: words.previous();
while (current != BreakIterator.DONE) {
for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
if (Character.isLetter(s.charAt(p))) {
return last;
}
}
last = current;
current = (direction == NEXT) ? words.next()
: words.previous();
}
return BreakIterator.DONE;
}
示例10: incrementWord
import java.text.BreakIterator; //导入依赖的package包/类
@Override
protected boolean incrementWord() {
int start = wordBreaker.current();
if (start == BreakIterator.DONE) {
return false; // BreakIterator exhausted
}
// find the next set of boundaries, skipping over non-tokens
int end = wordBreaker.next();
while (end != BreakIterator.DONE &&
!Character.isLetterOrDigit(Character.codePointAt(buffer, sentenceStart + start, sentenceEnd))) {
start = end;
end = wordBreaker.next();
}
if (end == BreakIterator.DONE) {
return false; // BreakIterator exhausted
}
clearAttributes();
termAtt.copyBuffer(buffer, sentenceStart + start, end - start);
offsetAtt.setOffset(correctOffset(offset + sentenceStart + start), correctOffset(offset + sentenceStart + end));
return true;
}
示例11: incrementSentence
import java.text.BreakIterator; //导入依赖的package包/类
/**
* return true if there is a token from the buffer, or null if it is
* exhausted.
*/
private boolean incrementSentence() throws IOException {
if (length == 0) // we must refill the buffer
return false;
while (true) {
int start = iterator.current();
if (start == BreakIterator.DONE)
return false; // BreakIterator exhausted
// find the next set of boundaries
int end = iterator.next();
if (end == BreakIterator.DONE)
return false; // BreakIterator exhausted
setNextSentence(start, end);
if (incrementWord()) {
return true;
}
}
}
示例12: DocumentWordTokenizer
import java.text.BreakIterator; //导入依赖的package包/类
/**
* Creates a new DocumentWordTokenizer to work on a document
* @param document The document to spell check
*/
public DocumentWordTokenizer(Document document) {
this.document = document;
//Create a text segment over the entire document
text = new Segment();
sentenceIterator = BreakIterator.getSentenceInstance();
try {
document.getText(0, document.getLength(), text);
sentenceIterator.setText(text);
// robert: use text.getBeginIndex(), not 0, for segment's first offset
currentWordPos = getNextWordStart(text, text.getBeginIndex());
//If the current word pos is -1 then the string was all white space
if (currentWordPos != -1) {
currentWordEnd = getNextWordEnd(text, currentWordPos);
nextWordPos = getNextWordStart(text, currentWordEnd);
} else {
moreTokens = false;
}
} catch (BadLocationException ex) {
moreTokens = false;
}
}
示例13: TestIsBoundary
import java.text.BreakIterator; //导入依赖的package包/类
void TestIsBoundary() {
iter = BreakIterator.getWordInstance(Locale.US);
for (int i = 0; i < given.length; i++) {
iter.setText(given[i]);
start = iter.first();
end = iter.next();
while (end < given[i].length()) {
if (!iter.isBoundary(end)) {
errln("Word break failure: isBoundary() This should be a boundary. Index=" +
end + " for " + given[i]);
}
end = iter.next();
}
}
}
示例14: DocCommentParser
import java.text.BreakIterator; //导入依赖的package包/类
DocCommentParser(ParserFactory fac, DiagnosticSource diagSource, Comment comment) {
this.fac = fac;
this.diagSource = diagSource;
this.comment = comment;
names = fac.names;
m = fac.docTreeMaker;
Locale locale = (fac.locale == null) ? Locale.getDefault() : fac.locale;
Options options = fac.options;
boolean useBreakIterator = options.isSet("breakIterator");
if (useBreakIterator || !locale.getLanguage().equals(Locale.ENGLISH.getLanguage()))
sentenceBreaker = BreakIterator.getSentenceInstance(locale);
initTagParsers();
}
示例15: TestPrintAt_1
import java.text.BreakIterator; //导入依赖的package包/类
void TestPrintAt_1() {
iter = BreakIterator.getWordInstance(Locale.US);
int[][] index = {
{2, 8, 10, 15, 17},
{1, 8, 10, 12, 15, 17, 20},
{3, 8, 10, 13, 16, 18, 20},
{4, 6, 9, 10, 16},
};
for (int i = 0; i < given.length; i++) {
iter.setText(given[i]);
for (int j = index[i].length-1; j >= 0; j--) {
end = iter.following(index[i][j]);
start = iter.previous();
if (!expected[i][j].equals(given[i].substring(start, end))) {
errln("Word break failure: printAt_1() expected:<" +
expected[i][j] + ">, got:<" +
given[i].substring(start, end) +
"> start=" + start + " end=" + end);
}
}
}
}