TikaのLanguageDetector

Tikaには文字列を渡すと、その言語を渡してくれる機能がある。使うためにはまずpom.xmlに以下の依存関係を追加する。

                <dependency>
                        <groupId>org.apache.tika</groupId>
                        <artifactId>tika-langdetect</artifactId>
                        <version>1.20</version>
                </dependency>

あとは、LanguageDetectorを生成して利用する。

import java.io.IOException;

import org.apache.tika.langdetect.OptimaizeLangDetector;
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;

public class LanguageDetectorExample {

    public String detectLanguage(String text) throws IOException {
        LanguageDetector detector = new OptimaizeLangDetector().loadModels();
        LanguageResult result = detector.detect(text);
        return result.getLanguage();
    }
}

上記はTikaにあるexampleコードだが、OptimaizeLangDetectorをnewして、loadModels()でLanguageDetectorを取得する。あとはLanguageDetectorに言語判定したいテキストを渡せば言語情報が返ってくる。

まぁ、とはいえ、そこそこ判定が外れる気もする…。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です