LoginSignup
0
0

JUMAN++をWindows環境で動かしてみる

Posted at

はじめに

京大の研究室が公開しているBARTの日本語Pretrainedモデルの利用を検討しています。
JUMAN++で形態素解析した結果を渡す必要があるため、まずはJUMAN++を導入してみます。

インストーラを取得する

jumanpp-2.0.0-rc3.tar.xz をダウンロードします。

Visual Studio Community をインストールする

ダウンロードした資材をビルドするために、Visual Studio 2022をインストールします。

インストール時に、以下の2つの項目を選択してください。

  • 「ワークロード」>「C++によるデスクトップ開発」
  • 「個別のコンポーネント」>「Windows用 C++ CMakeツール」

image.png

image.png

Cmakeでビルドする

「x86 Native Tools Command Prompt for VS 2022」を開き、以下を実行します。

ビルド
$ cd ●●●/●●●/ ... /jumanpp-2.0.0-rc3
$ mkdir cmake-build-dir
$ cd cmake-build-dir

$ cmake ..
$ cmake --build . --config Release
$ ctest -C Release --output-on-failure

実行用のディレクトリを作成する

以下のファイルを取得して、Cドライブ直下に配置します。

  • jumanpp-2.0.0-rc3\cmake-build-dir\src\jumandic\Release\jumanpp_v2.exe
  • jumanpp-2.0.0-rc3.tar\jumanpp-2.0.0-rc3\model\jumandic.conf.in
  • jumanpp-2.0.0-rc3.tar\jumanpp-2.0.0-rc3\model\jumandic.jppmdl
フォルダ構成
C:jumanpp
│  jumanpp_v2.exe
│
└─libexec
        jumandic.conf //jumandic.conf.inをリネームする
        jumandic.jppmdl

フォルダを作成したら、環境変数PATH に C:jumanpp を追加してください。

jumandic.confを編集する

jumandic.conf を開き、modelのパスを変更します。

jumandic.conf
--model=C:/jumanpp/libexec/jumandic.jppmdl
--rnn-nce-bias=5.62844432562
--rnn-unk-constant=-3.4748115191
--rnn-unk-length=-2.92994951022
--feature-weight-perceptron=1
--feature-weight-rnn=0.0176

実行用のバッチファイルを作成する

実行用のバッチファイルjumanpp.batを作成して、C:jumanpp に配置します。

jumanpp.bat
@echo off
jumanpp_v2 --config=C:\jumanpp\libexec\jumandic.conf %*

これで、コマンドプロンプトからJUMAN++ を実行できるようになりました。

pyknpをインストールする

JUMAN++ をpythonで利用するために、pyknpをインストールします。

install
$ pip install pyknp

pyknpの中身を修正する

Python3\Lib\site-packages\pyknp\utils\process.py を修正します。
Windows環境で動かすために、signalで書かれているコードを書き換えます。

修正箇所 (L3~L4)
#import signal
import threading
修正箇所 (L75~L79)
        #signal.signal(signal.SIGALRM, alarm_handler)
        #signal.alarm(self.process_timeout)
        alarm = threading.Timer(self.process_timeout, alarm_handler)
        alarm.start()
修正箇所 (L88~L90)
        finally:
            #signal.alarm(0)
            alarm.cancel()

サンプルコード

以下のようなコードで、分かち書きができるようになりました。

sample.py
from pyknp import Juman

# 入力文
text="すもももももももものうち"
# 改行削除、半角スペースを全角スペースに変換
clean_text=text.replace('\n', '').replace(' ', ' ')

list=[]
jumanpp = Juman(command='jumanpp_v2', option='--config=C:/jumanpp/libexec/jumandic.conf')
mrphs=juman.analysis(clean_text)
for m in mrphs:
    list.append(m.midasi)

print(" ".join(list))

参考文献

0
0
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
0
0