Search code examples
textstanford-nlpopennlpcjkkuromoji

How to split Japanese text?


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?


Solution

  • 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.