全パッケージ クラス階層 このパッケージ 前項目 次項目 インデックス
java.lang.Object | +----java.text.BreakIterator
BreakIteratorクラスは、テキストにおける境界の位置を見つけるためのメソッドを実装します。BreakIteratorのインスタンスは、現在の位置を維持し、テキスト全体をスキャンして、境界(複数)にある文字(複数)のインデックスを返します。内部的には、BreakIteratorは、CharacterIteratorを使ってテキストをスキャンするので、そのプロトコルを実装するオブジェクトであれば、どのオブジェクトによって保持されるテキストでもスキャンすることができます。setTextに渡される Stringオブジェクトのスキャンには、StringCharacterIteratorが使用されます。
さまざまなタイプのブレーク反復子のインスタンスを作成するには、このクラスのファクトリメソッドを使用します。具体的には、getWordIterator、getLineIterator、getSentenceIterator、getCharacterIteratorを使って、それぞれ語、行、文、文字の境界を分析する BreakIteratorを作成します。単一の BreakIteratorは、1つの単位(語、行、文など)だけに作用します。行いたい各単位境界の分析ごとに、異なる反復子を使用する必要があります。
行境界分析は、行の折返しの際、テキスト文字列をどこで分けるかを判断するために使用します。このメカニズムによって、句読点やハイフン付きの語が正しく処理されます。
文境界分析は、数値や省略形のピリオドや、引用符やかっこなどの区切り文字を正しく解釈して文を選択します。
語境界分析は、検索と置換の機能や、ダブルクリックで語を選択できるようにするテキスト編集アプリケーションで使用されます。語の選択では、語の中の句読点や語に続く句読点が正しく解釈されます。記号や句読点など、語として認められない文字は、その両側で語のブレークが起こります。
文字境界分析を使うと、たとえば、テキスト文字列でカーソルを移動したいときなどに、文字単位で処理することができます。文字境界の分析では、その文字がどのように格納されていようとも、文字列が正規の順序で処理されます。たとえば、アクセント付きの文字は基本文字と発音区別符号として格納されることがあります。ユーザが何を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 final int DONE
protected BreakIterator()
public Object clone()
public abstract int first()
public abstract int last()
public abstract int next(int n)
public abstract int next()
public abstract int previous()
public abstract int following(int offset)
public abstract int current()
public abstract CharacterIterator getText()
public void setText(String newText)
public abstract void setText(CharacterIterator newText)
public static BreakIterator getWordInstance()
public static BreakIterator getWordInstance(Locale where)
public static BreakIterator getLineInstance()
public static BreakIterator getLineInstance(Locale where)
public static BreakIterator getCharacterInstance()
public static BreakIterator getCharacterInstance(Locale where)
public static BreakIterator getSentenceInstance()
public static BreakIterator getSentenceInstance(Locale where)
public static synchronized Locale[] getAvailableLocales()
全パッケージ クラス階層 このパッケージ 前項目 次項目 インデックス