LoginSignup
1
1

More than 1 year has passed since last update.

Windows環境のPythonで形態素解析器 -- ChaSen -- を使う方法

Posted at

Chasenのインストール手順

1.Cygwinのインストール

今回はCygwin環境にChasenをインストールするため、Cygwinをインストールします。
Cygwinのダウンロードはこちらから
使用するパッケージはlibiconv, libiconv-devel, libcrypt-devel, make, gcc-g++になります。

2.Dartsのインストール

Cygwin環境にDartsをインストールします。
Dartsのダウンロードはこちらから(今回は darts-0.32.tar.gz を使用しています。)

Cygwinを起動し、Dartsの保存先に移動する。(以下はダウンロードに保存した場合)
以下のようにインストールをする。

cd /cygdrive/c/Users/○○/Downloads/
tar -xzvf darts-0.32.tar.gz
cd darts-0.32
./configure
make
make check
make install
cd ..

3.ChaSenのインストール

Cygwin環境にChaSenをインストールします。
ChaSenのダウンロードはこちらから(今回は chasen-2.4.5.tar.gz を使用しています。)

Cygwinを起動し、ChaSenの保存先に移動する。(以下はダウンロードに保存した場合)
以下のようにインストールをする。

cd /cygdrive/c/Users/○○/Downloads/
tar -xzvf chasen-2.4.5.tar.gz
cd chasen-2.4.5
./configure LIBICONV='-liconv' LIBS='-liconv'
make
make check
make install
cd ..

4.IPAdic(ChaSenの辞書)のインストール

Cygwin環境にIPAdicをインストールします。
IPAdicのダウンロードはこちらから(今回は ipadic-2.7.0.tar.gz を使用しています。)

Cygwinを起動し、IPAdicの保存先に移動する。(以下はダウンロードに保存した場合)
以下のようにインストールをする。

cd /cygdrive/c/Users/○○/Downloads/
tar -xzvf ipadic-2.7.0.tar.gz
cd ipadic-2.7.0.tar.gz
./configure
make
make install
cd ..

5.辞書の文字コードを変更

ChaSenはEUCで作られているため、このままでは文字化けします。
そのため、辞書をUTF-8に変更する必要があります。
まず、Cygwin環境にnkfをインストールします。
nkfのダウンロードはこちらから(今回は nkf-2.1.5.tar.gz を使用しています。)

Cygwinを起動し、nkfの保存先に移動する。(以下はダウンロードに保存した場合)
以下のようにインストールをする。

cd /cygdrive/c/Users/○○/Downloads/
tar -xzvf nkf-2.1.5.tar.gz
cd nkf-2.1.5.tar.gz
./configure
make
make install
cd ..

次に、以下のように辞書の文字コードを変更する。

cd /cygdrive/c/Users/○○/Downloads/
cd ipadic-2.7.0.tar.gz
make clean
find -name '*.dic' | xargs nkf --overwrite -w
find -name '*.cha' | xargs nkf --overwrite -w
`chasen-config --mkchadic`/makemat -i w
`chasen-config --mkchadic`/makeda -i w chadic *.dic
./configure
make
make install
nkf -w --overwrite /usr/local/etc/chasenrc

ここで一度、テストする。chasenには-iwのオプションを付けて呼び出します。

テスト
echo 'テストです。出来ていますか?'|chasen -iw
結果
テスト  テスト  テスト  名詞-サ変接続
です    デス    です    助動詞  特殊・デス      基本形
                  記号-句点
出来    デキ    出来る  動詞-自立       一段    連用形
                  助詞-接続助詞
            いる    動詞-非自立     一段    連用形
ます    マス    ます    助動詞  特殊・マス      基本形
                  助詞-副助詞/並立助詞/終助詞
                  記号-一般
EOS

このようになれば、ChaSenのインストールは完了です。

コマンドプロンプトでChaSenを実行

1.環境変数を変更する

「Path」を選び、[編集]をクリックする。
「;C:\cygwin\bin;C:\cygwin\usr\bin;C:\cygwin\usr\local\bin」を追加する。

さらに[新規]でLD_LIBRARY_PATH環境変数を追加する。
値は「C:\cygwin\lib;C:\cygwin\lib\w32api;C:\cygwin\usr\local\lib」とする。

2.コマンドプロンプトでChaSenを実行

Windowsのコマンドプロンプトの文字コードをUTF-8に変更し、実行する。

chcp 65001
echo 'テストです。出来ていますか?'|chasen -iw

Cygwinの時と同じ結果が出力されればOK。

Windows環境のPythonでChaSenを実行

ソースコードを以下に載せております。

chasen.py
import subprocess
import time

def strip_cmd_injection(instr):
    inj = [";", "|", "&", "`", "(", ")", "$", "<", ">", "*", "?", "{", "}", "[", "]", "!", "\n"]
    for s in inj:
        instr = instr.replace(s, "")
    instr = instr.replace("", "")
    return instr

def chasen(arg):
    arg = strip_cmd_injection(arg)
    cmd = "echo {0} | chasen -iw".format(arg)
    subprocess.Popen("chcp 65001", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(0.1) # chcp 65001の反映待ち
    proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    if stderr != b'':
        raise(Exception(stderr.decode('utf-8')))

    try:
        lines = stdout.decode('utf-8').split("\n")
    except:
        raise(Exception(stderr.decode('utf-8')))

    for line in lines:
        if (line == "EOS"):
            break
        yield line.split("\t")

if __name__ == '__main__':
    line = "'テストです。出来ていますか?'"
    for cha in chasen(line):
        print(cha)

コマンドプロンプトで実行した時と同じ結果が出力されればOK。
以上、Windows環境のPythonでChaSenを使う方法でした。

参考にしたページ

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1