3

What is the best way of splitting Japanese text using Java? For Example, for the below text:

こんにちは。私の名前はオバマです。私はアメリカに行く。

I need the following output:

こんにちは
私の名前はオバマです
私はアメリカに行く

Is it possible using Kuromoji?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
din_oops
  • 698
  • 1
  • 9
  • 27

1 Answers1

4

You can use java.text.BreakIterator.

String TEXT = "こんにちは。私の名前はオバマです。私はアメリカに行く。";
BreakIterator boundary = BreakIterator.getSentenceInstance(Locale.JAPAN);
boundary.setText(TEXT);
int start = boundary.first();
for (int end = boundary.next();
     end != BreakIterator.DONE;
     start = end, end = boundary.next()) {
     System.out.println(TEXT.substring(start, end));
}

The output of this program is:

こんにちは。
私の名前はオバマです。
私はアメリカに行く。

You cannot use Kuromoji to look for Japanese sentence boundaries. It can split a sentence into words.

Koji
  • 101
  • 2