Apr 03, 2006

[Library] NGramJ にチャレンジ

NGramJ とは

NGramJ は、Natural Language Processing(NLP)用のライブラリ。 与えられたバイト配列の言語、文字エンコーディングを判別してくれる。 たとえば、"竹やぶに竹立てかけた。"という文字列を Shift_JIS のバイト配列で NGramJ に引き渡すと、「Shift_JIS の日本語」であることを判断してくれる。

NGramJ
http://ngramj.sourceforge.net/index.html
API Document
http://ngramj.sourceforge.net/javadoc/index.html

NGramJ の実行環境構築

  1. こちらから NGramJ をダウンロードする。ここでは ver.1.0-0.060327 を前提とする。
  2. アーカイブの中にある以下の ngramj.jar をクラスパスに追加する。

サンプルコード

とりあえず日本語と英語の判別を試してみた。

package jp.in_vitro.codelets.ngramj;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

import de.spieleck.app.ngramj.Categorizer;
import de.spieleck.app.ngramj.EntryProfile;
import de.spieleck.app.ngramj.Profile;
import de.spieleck.app.ngramj.lm.CategorizerImpl;

public class Codelet {

    public Codelet() {
        super();
    }

    public static void main(final String[] args) throws IOException {
        Codelet me = new Codelet();
        me.execute01();
        me.execute02();
        me.execute03();
        me.execute04();
    }

    protected void execute01() throws IOException {
        String target = "竹やぶに竹立てかけた。";
        InputStream is = new ByteArrayInputStream(target
                .getBytes("Shift_JIS"));
        Profile profile = this.match(is);
        
        Logger.global.info("" + target + " -> " + profile);
    }

    protected void execute02() throws IOException {
        String target = "竹やぶに竹立てかけた。";
        InputStream is = new ByteArrayInputStream(target
                .getBytes("EUC-JP"));
        Profile profile = this.match(is);
        
        Logger.global.info("" + target + " -> " + profile);
    }

    protected void execute03() throws IOException {
        String target = "竹やぶに竹立てかけた。";
        InputStream is = new ByteArrayInputStream(target
                .getBytes("UTF-8"));
        Profile profile = this.match(is);
        
        Logger.global.info("" + target + " -> " + profile);
    }

    protected void execute04() throws IOException {
        String target = "This is a pen. Who cares?";
        InputStream is = new ByteArrayInputStream(target
                .getBytes("ISO-8859-1"));
        Profile profile = this.match(is);
        
        Logger.global.info("" + target + " -> " + profile);
    }


    protected Profile match(final InputStream is) throws IOException {

        Categorizer categorizer = new CategorizerImpl();
        EntryProfile entryProfile = new EntryProfile(is);
        Profile profile = categorizer.match(entryProfile);

        return profile;
    }
}

実行結果

日本語の UTF-8 は判別できなかった。調べてみると標準で用意されている日本語プロファイルは Shift_JIS と EUC-JP だけだった。 Shift_JIS、EUC-JP 以外の文字エンコーディングを使用する場合は、自分でプロファイルを作成する必要がある。

2006/04/03 2:05:59 jp.in_vitro.codelets.ngramj.Codelet execute01
情報: 竹やぶに竹立てかけた。 -> japanese-shift_jis.lm
2006/04/03 2:05:59 jp.in_vitro.codelets.ngramj.Codelet execute02
情報: 竹やぶに竹立てかけた。 -> japanese-euc_jp.lm
2006/04/03 2:06:00 jp.in_vitro.codelets.ngramj.Codelet execute03
情報: 竹やぶに竹立てかけた。 -> armenian.lm
2006/04/03 2:06:00 jp.in_vitro.codelets.ngramj.Codelet execute04
情報: This is a pen. Who cares? -> english.lm

標準提供されるプロファイル

今回使用したバージョンでは下記のプロファイルが添付されていた。 実際に使用するのであれば、日本語関連で文字エンコーディングをいくつか追加する必要がありそう。

afrikaans.lm
albanian.lm
amharic-utf.lm
arabic-iso8859_6.lm
arabic-windows1256.lm
armenian.lm
basque.lm
belarus-windows1251.lm
bosnian.lm
breton.lm
bulgarian-iso8859_5.lm
catalan.lm
chinese-big5.lm
chinese-gb2312.lm
croatian-ascii.lm
czech-iso8859_2.lm
danish.lm
dutch.lm
english.lm
esperanto.lm
estonian.lm
finnish.lm
french.lm
frisian.lm
georgian.lm
german.lm
greek-iso8859-7.lm
hawaian.lm
hebrew-iso8859_8.lm
hindi.lm
hungarian.lm
icelandic.lm
indonesian.lm
irish.lm
italian.lm
japanese-euc_jp.lm
japanese-shift_jis.lm
korean.lm
latin.lm
latvian.lm
lithuanian.lm
malay.lm
manx.lm
marathi.lm
middle_frisian.lm
mingo.lm
nepali.lm
norwegian.lm
persian.lm
polish.lm
portuguese.lm
quechua.lm
romanian.lm
rumantsch.lm
russian-iso8859_5.lm
russian-koi8_r.lm
russian-windows1251.lm
sanskrit.lm
scots.lm
scots_gaelic.lm
serbian-ascii.lm
slovak-ascii.lm
slovak-windows1250.lm
slovenian-ascii.lm
slovenian-iso8859_2.lm
spanish.lm
swahili.lm
swedish.lm
tagalog.lm
tamil.lm
thai.lm
turkish.lm
ukrainian-koi8_r.lm
vietnamese.lm
welsh.lm
yiddish-utf.lm