JavaTM Platform
Standard Ed. 6

java.text
クラス BreakIterator

java.lang.Object
  上位を拡張 java.text.BreakIterator
すべての実装されたインタフェース:
Cloneable

public abstract class BreakIterator
extends Object
implements Cloneable

BreakIterator クラスは、テキスト内の境界の位置を見つけるメソッドを実装します。BreakIterator のインスタンスは現在の位置を維持し、テキストをスキャンして境界が発生する文字のインデックスを返します。内部的には、BreakIteratorCharacterIterator を使ってテキストをスキャンするため、このプロトコルを実装する任意のオブジェクトによって保持されるテキストをスキャンできます。StringCharacterIterator は、setText に渡された String オブジェクトのスキャンに使用されます。

このクラスによって提供されるファクトリメソッドを使って、さまざまな型の分割反復子のインスタンスを生成します。特に、単語、行、文、および文字の境界解析を実行する BreakIterator を生成するには、それぞれ getWordIteratorgetLineIteratorgetSentenceIterator、および getCharacterIterator を使用します。単一の BreakIterator は、1 つのユニット (単語、行、文など) のみを処理するため、実行するユニット境界解析ごとに異なる反復子を使用する必要があります。  

行の境界解析では、テキスト文字列を行折り返しで分割する位置を判定します。句読点およびハイフネーションされた単語も、機構により正しく処理されます。実際の行分割は、使用可能な行幅も考慮する必要があるため、より高いレベルのソフトウェアによって処理されます。  

文の境界解析では、数字と略語の中のピリオド、さらに引用符や括弧などの終了文字の正しい解釈について選択が可能です。  

単語の境界解析は、検索置換機能で使用されます。また、テキスト編集アプリケーション内で、ダブルクリックによって単語が選択可能になります。単語選択では、句読点とともに、記号や句読点などのように単語の一部でない文字、前後に分割のある文字も正しく解釈されます。  

文字の境界解析では、たとえばカーソルをテキスト文字列に沿って動かすような場合に、ユーザーが予測するとおりの操作が行われるようにします。文字の境界解析により、文字の格納方法に依存せず、文字列の正しいナビゲーションが可能になります。返される境界は、補助文字、結合文字シーケンス、または合字クラスタの境界になる場合があります。たとえば、アクセント付きの文字は、基準文字と発音区別符号として格納されている場合があります。ユーザーの文字に対する認識は言語間で異なります。

このクラスのファクトリメソッドから返される BreakIterator インスタンスは、自然言語での使用のみを想定しており、プログラミング言語のテキストには使用できません。ただし、プログラミング言語をトークン化するサブクラスを定義することはできます。

:

テキスト境界を作成し使用します。

 
 public static void main(String args[]) {
      if (args.length == 1) {
          String stringToExamine = args[0];
          //print each word in order
          BreakIterator boundary = BreakIterator.getWordInstance();
          boundary.setText(stringToExamine);
          printEachForward(boundary, stringToExamine);
          //print each sentence in reverse order
          boundary = BreakIterator.getSentenceInstance(Locale.US);
          boundary.setText(stringToExamine);
          printEachBackward(boundary, stringToExamine);
          printFirst(boundary, stringToExamine);
          printLast(boundary, stringToExamine);
      }
 }
 
各要素を順に出力します。
 
 public static void printEachForward(BreakIterator boundary, String source) {
     int start = boundary.first();
     for (int end = boundary.next();
          end != BreakIterator.DONE;
          start = end, end = boundary.next()) {
          System.out.println(source.substring(start,end));
     }
 }
 
各要素を逆順に出力します。
 
 public static void printEachBackward(BreakIterator boundary, String source) {
     int end = boundary.last();
     for (int start = boundary.previous();
          start != BreakIterator.DONE;
          end = start, start = boundary.previous()) {
         System.out.println(source.substring(start,end));
     }
 }
 
最初の要素を出力します。
 
 public static void printFirst(BreakIterator boundary, String source) {
     int start = boundary.first();
     int end = boundary.next();
     System.out.println(source.substring(start,end));
 }
 
最後の要素を出力します。
 
 public static void printLast(BreakIterator boundary, String source) {
     int end = boundary.last();
     int start = boundary.previous();
     System.out.println(source.substring(start,end));
 }
 
指定された位置にある要素を出力します。
 
 public static void printAt(BreakIterator boundary, int pos, String source) {
     int end = boundary.following(pos);
     int start = boundary.previous();
     System.out.println(source.substring(start,end));
 }
 
次の単語を検索します。
 
 public static int nextWordStartAfter(int pos, String text) {
     BreakIterator wb = BreakIterator.getWordInstance();
     wb.setText(text);
     int last = wb.following(pos);
     int current = wb.next();
     while (current != BreakIterator.DONE) {
         for (int p = last; p < current; p++) {
             if (Character.isLetter(text.codePointAt(p)))
                 return last;
         }
         last = current;
         current = wb.next();
     }
     return BreakIterator.DONE;
 }
 
(The iterator returned by BreakIterator.getWordInstance() is unique in that the break positions it returns don't represent both the start and end of the thing being iterated over. That is, a sentence-break iterator returns breaks that each represent the end of one sentence and the beginning of the next. With the word-break iterator, the characters between two boundaries might be a word, or they might be the punctuation or whitespace between two words. The above code uses a simple heuristic to determine which boundary is the beginning of a word: If the characters between this boundary and the next boundary include at least one letter (this can be an alphabetical letter, a CJK ideograph, a Hangul syllable, a Kana character, etc.), then the text between this boundary and the next is a word; otherwise, it's the material between words.)

関連項目:
CharacterIterator

フィールドの概要
static int DONE
          DONE is returned by previous(), next(), next(int), preceding(int) and following(int) when either the first or last text boundary has been reached.
 
コンストラクタの概要
protected BreakIterator()
          Constructor.
 
メソッドの概要
 Object clone()
          Create a copy of this iterator
abstract  int current()
          Returns character index of the text boundary that was most recently returned by next(), next(int), previous(), first(), last(), following(int) or preceding(int).
abstract  int first()
          Returns the first boundary.
abstract  int following(int offset)
          Returns the first boundary following the specified character offset.
static Locale[] getAvailableLocales()
          Returns an array of all locales for which the get*Instance methods of this class can return localized instances.
static BreakIterator getCharacterInstance()
          Returns a new BreakIterator instance for character breaks for the default locale.
static BreakIterator getCharacterInstance(Locale locale)
          Returns a new BreakIterator instance for character breaks for the given locale.
static BreakIterator getLineInstance()
          Returns a new BreakIterator instance for line breaks for the default locale.
static BreakIterator getLineInstance(Locale locale)
          Returns a new BreakIterator instance for line breaks for the given locale.
static BreakIterator getSentenceInstance()
          Returns a new BreakIterator instance for sentence breaks for the default locale.
static BreakIterator getSentenceInstance(Locale locale)
          Returns a new BreakIterator instance for sentence breaks for the given locale.
abstract  CharacterIterator getText()
          Get the text being scanned
static BreakIterator getWordInstance()
          Returns a new BreakIterator instance for word breaks for the default locale.
static BreakIterator getWordInstance(Locale locale)
          Returns a new BreakIterator instance for word breaks for the given locale.
 boolean isBoundary(int offset)
          Returns true if the specified character offset is a text boundary.
abstract  int last()
          Returns the last boundary.
abstract  int next()
          Returns the boundary following the current boundary.
abstract  int next(int n)
          Returns the nth boundary from the current boundary.
 int preceding(int offset)
          Returns the last boundary preceding the specified character offset.
abstract  int previous()
          Returns the boundary preceding the current boundary.
abstract  void setText(CharacterIterator newText)
          Set a new text for scanning.
 void setText(String newText)
          Set a new text string to be scanned.
 
クラス java.lang.Object から継承されたメソッド
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

DONE

public static final int DONE
DONE is returned by previous(), next(), next(int), preceding(int) and following(int) when either the first or last text boundary has been reached.

関連項目:
定数フィールド値
コンストラクタの詳細

BreakIterator

protected BreakIterator()
Constructor. BreakIterator is stateless and has no default behavior.

メソッドの詳細

clone

public Object clone()
Create a copy of this iterator

オーバーライド:
クラス Object 内の clone
戻り値:
A copy of this
関連項目:
Cloneable

first

public abstract int first()
Returns the first boundary. The iterator's current position is set to the first text boundary.

戻り値:
The character index of the first text boundary.

last

public abstract int last()
Returns the last boundary. The iterator's current position is set to the last text boundary.

戻り値:
The character index of the last text boundary.

next

public abstract int next(int n)
Returns the nth boundary from the current boundary. If either the first or last text boundary has been reached, it returns BreakIterator.DONE and the current position is set to either the first or last text boundary depending on which one is reached. Otherwise, the iterator's current position is set to the new boundary. For example, if the iterator's current position is the mth text boundary and three more boundaries exist from the current boundary to the last text boundary, the next(2) call will return m + 2. The new text position is set to the (m + 2)th text boundary. A next(4) call would return BreakIterator.DONE and the last text boundary would become the new text position.

パラメータ:
n - which boundary to return. A value of 0 does nothing. Negative values move to previous boundaries and positive values move to later boundaries.
戻り値:
The character index of the nth boundary from the current position or BreakIterator.DONE if either first or last text boundary has been reached.

next

public abstract int next()
Returns the boundary following the current boundary. If the current boundary is the last text boundary, it returns BreakIterator.DONE and the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the boundary following the current boundary.

戻り値:
The character index of the next text boundary or BreakIterator.DONE if the current boundary is the last text boundary. Equivalent to next(1).
関連項目:
next(int)

previous

public abstract int previous()
Returns the boundary preceding the current boundary. If the current boundary is the first text boundary, it returns BreakIterator.DONE and the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the boundary preceding the current boundary.

戻り値:
The character index of the previous text boundary or BreakIterator.DONE if the current boundary is the first text boundary.

following

public abstract int following(int offset)
Returns the first boundary following the specified character offset. If the specified offset equals to the last text boundary, it returns BreakIterator.DONE and the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the returned boundary. The value returned is always greater than the offset or the value BreakIterator.DONE.

パラメータ:
offset - the character offset to begin scanning.
戻り値:
The first boundary after the specified offset or BreakIterator.DONE if the last text boundary is passed in as the offset.
例外:
IllegalArgumentException - if the specified offset is less than the first text boundary or greater than the last text boundary.

preceding

public int preceding(int offset)
Returns the last boundary preceding the specified character offset. If the specified offset equals to the first text boundary, it returns BreakIterator.DONE and the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the returned boundary. The value returned is always less than the offset or the value BreakIterator.DONE.

パラメータ:
offset - the characater offset to begin scanning.
戻り値:
The last boundary before the specified offset or BreakIterator.DONE if the first text boundary is passed in as the offset.
例外:
IllegalArgumentException - if the specified offset is less than the first text boundary or greater than the last text boundary.
導入されたバージョン:
1.2

isBoundary

public boolean isBoundary(int offset)
Returns true if the specified character offset is a text boundary.

パラメータ:
offset - the character offset to check.
戻り値:
true if "offset" is a boundary position, false otherwise.
導入されたバージョン:
1.2

current

public abstract int current()
Returns character index of the text boundary that was most recently returned by next(), next(int), previous(), first(), last(), following(int) or preceding(int). If any of these methods returns BreakIterator.DONE because either first or last text boundary has been reached, it returns the first or last text boundary depending on which one is reached.

戻り値:
The text boundary returned from the above methods, first or last text boundary.
関連項目:
next(), next(int), previous(), first(), last(), following(int), preceding(int)

getText

public abstract CharacterIterator getText()
Get the text being scanned

戻り値:
the text being scanned

setText

public void setText(String newText)
Set a new text string to be scanned. The current scan position is reset to first().

パラメータ:
newText - new text to scan.

setText

public abstract void setText(CharacterIterator newText)
Set a new text for scanning. The current scan position is reset to first().

パラメータ:
newText - new text to scan.

getWordInstance

public static BreakIterator getWordInstance()
Returns a new BreakIterator instance for word breaks for the default locale.

戻り値:
A break iterator for word breaks

getWordInstance

public static BreakIterator getWordInstance(Locale locale)
Returns a new BreakIterator instance for word breaks for the given locale.

パラメータ:
locale - the desired locale
戻り値:
A break iterator for word breaks
例外:
NullPointerException - if locale is null

getLineInstance

public static BreakIterator getLineInstance()
Returns a new BreakIterator instance for line breaks for the default locale.

戻り値:
A break iterator for line breaks

getLineInstance

public static BreakIterator getLineInstance(Locale locale)
Returns a new BreakIterator instance for line breaks for the given locale.

パラメータ:
locale - the desired locale
戻り値:
A break iterator for line breaks
例外:
NullPointerException - if locale is null

getCharacterInstance

public static BreakIterator getCharacterInstance()
Returns a new BreakIterator instance for character breaks for the default locale.

戻り値:
A break iterator for character breaks

getCharacterInstance

public static BreakIterator getCharacterInstance(Locale locale)
Returns a new BreakIterator instance for character breaks for the given locale.

パラメータ:
locale - the desired locale
戻り値:
A break iterator for character breaks
例外:
NullPointerException - if locale is null

getSentenceInstance

public static BreakIterator getSentenceInstance()
Returns a new BreakIterator instance for sentence breaks for the default locale.

戻り値:
A break iterator for sentence breaks

getSentenceInstance

public static BreakIterator getSentenceInstance(Locale locale)
Returns a new BreakIterator instance for sentence breaks for the given locale.

パラメータ:
locale - the desired locale
戻り値:
A break iterator for sentence breaks
例外:
NullPointerException - if locale is null

getAvailableLocales

public static Locale[] getAvailableLocales()
Returns an array of all locales for which the get*Instance methods of this class can return localized instances. The returned array represents the union of locales supported by the Java runtime and by installed BreakIteratorProvider implementations. It must contain at least a Locale instance equal to Locale.US.

戻り値:
An array of locales for which localized BreakIterator instances are available.

JavaTM Platform
Standard Ed. 6

バグの報告と機能のリクエスト
さらに詳しい API リファレンスおよび開発者ドキュメントについては、Java SE 開発者用ドキュメントを参照してください。開発者向けの詳細な解説、概念の概要、用語の定義、バグの回避策、およびコード実例が含まれています。

Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。