Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Latest commit

History

History
363 lines (254 loc) · 11.9 KB

File metadata and controls

363 lines (254 loc) · 11.9 KB

日本語形態素解析器 SudachiPy チュートリアル

PyPi versionbuild

SudachiPyは日本語形態素解析器Sudachiのpython版です。

とりあえず動かしたい場合

$ pip install sudachipy sudachidict_core
$ echo "高輪ゲートウェイ駅" | sudachipy
高輪ゲートウェイ駅	名詞,固有名詞,一般,*,*,*	高輪ゲートウェイ駅
EOS
$ echo "高輪ゲートウェイ駅" | sudachipy -m A
高輪	名詞,固有名詞,地名,一般,*,*	高輪
ゲートウェイ	名詞,普通名詞,一般,*,*,*	ゲートウェー
駅	名詞,普通名詞,一般,*,*,*	駅
EOS
$ echo "空缶空罐空きカン" | sudachipy -a
空缶	名詞,普通名詞,一般,*,*,*	空き缶	空缶	アキカン	0
空罐	名詞,普通名詞,一般,*,*,*	空き缶	空罐	アキカン	0
空きカン	名詞,普通名詞,一般,*,*,*	空き缶	空きカン	アキカン	0
EOS

導入

SudachiPyを使う場合は辞書が必要になります。 ※パッケージには辞書が含まれていません。

Step 1. SudachiPyのインストール

$ pip install sudachipy

Step 2. 辞書のインストール

辞書はPythonのパッケージとしてダウンロードできます。

※辞書のダウンロードは時間がかかります (core辞書は約70MB ).

$ pip install sudachidict_core

また、他の辞書を選択することもできます。詳細はこのセッションを参照してください。

使用方法(CLI)

CLIでは以下のように実行できます。

$ echo"外国人参政権"| sudachipy
外国人参政権	名詞,普通名詞,一般,*,*,*	外国人参政権
EOS
$ echo"外国人参政権"| sudachipy -m A
外国	名詞,普通名詞,一般,*,*,*	外国
人	接尾辞,名詞的,一般,*,*,*	人
参政	名詞,普通名詞,一般,*,*,*	参政
権	接尾辞,名詞的,一般,*,*,*	権
EOS
$ sudachipy tokenize -h
usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-s string]
[-a] [-d] [-v]
[file [file ...]]
Tokenize Text
positional arguments:
file text written in utf-8
optional arguments:
-h, --help show this help message and exit-r file the setting file in JSON format
-m {A,B,C} the mode of splitting
-o file the output file
-s string sudachidict type-a print all of the fields
-d print the debug information
-v, --version print sudachipy version

出力形式

タブ区切りで出力されます。 デフォルトは以下の情報が含まれます。

  • 表層形
  • 品詞(コンマ区切り)
  • 正規化表記

オプションで -a を指定すると以下の情報が追加されます。

  • 辞書形
  • 読み
  • 辞書ID
    • 0 システム辞書
    • 1 ユーザー辞書
    • -1\t(OOV) 未知語(辞書に含まれない単語)
$ echo"外国人参政権"| sudachipy -a
外国人参政権	名詞,普通名詞,一般,*,*,*	外国人参政権	外国人参政権	ガイコクジンサンセイケン	0
EOS
echo"阿quei"| sudachipy -a
阿	名詞,普通名詞,一般,*,*,*	阿	阿 -1	(OOV)
quei	名詞,普通名詞,一般,*,*,*	quei	quei -1	(OOV)
EOS

使用方法(Python パッケージ)

fromsudachipyimporttokenizerfromsudachipyimportdictionarytokenizer_obj=dictionary.Dictionary().create()
# 複数粒度分割mode=tokenizer.Tokenizer.SplitMode.C
[m.surface() formintokenizer_obj.tokenize("国家公務員", mode)]
# => ['国家公務員']mode=tokenizer.Tokenizer.SplitMode.B
[m.surface() formintokenizer_obj.tokenize("国家公務員", mode)]
# => ['国家', '公務員']mode=tokenizer.Tokenizer.SplitMode.A
[m.surface() formintokenizer_obj.tokenize("国家公務員", mode)]
# => ['国家', '公務', '員']
# 形態素情報m=tokenizer_obj.tokenize("食べ", mode)[0]
m.surface() # => '食べ'm.dictionary_form() # => '食べる'm.reading_form() # => 'タベ'm.part_of_speech() # => ['動詞', '一般', '*', '*', '下一段-バ行', '連用形-一般']
# 正規化tokenizer_obj.tokenize("附属", mode)[0].normalized_form()
# => '付属'tokenizer_obj.tokenize("SUMMER", mode)[0].normalized_form()
# => 'サマー'tokenizer_obj.tokenize("シュミレーション", mode)[0].normalized_form()
# => 'シミュレーション'

(これは 20200330core 辞書による出力例です。 辞書のバージョンによって変わる可能性があります。)

辞書の種類

**WARNING: sudachipy link コマンドは SudachiPy v0.5.2 以降から利用できなくなりました. **

Sudachi辞書はsmallcorefullの3種類があります。 詳細はWorksApplications/SudachiDictを参照してください。

SudachiPyはデフォルトではsudachidict_coreに設定されています。

sudachidict_small, sudachidict_core, sudachidict_fullはPythonのパッケージとしてインストールされます。

辞書ファイルはパッケージ自体には含まれていませんが、上記のインストール時にダウンロードする処理が埋め込まれています。

辞書オプション: コマンドライン

辞書設定の変更は-sオプションで指定することができます。

$ pip install sudachidict_small
$ echo"外国人参政権"| sudachipy -s small
$ pip install sudachidict_full
$ echo"外国人参政権"| sudachipy -s full

辞書オプション: Python パッケージ

Dictionary の引数 config_path または dict_type から利用する辞書を指定することができます。

classDictionary(config_path=None, resource_dir=None, dict_type=None)
  1. config_path
    • config_path で辞書の設定ファイルのパスを指定することができます(辞書の設定ファイル 参照)。
    • 指定した辞書の設定ファイルに、辞書のファイルパス systemDict が記述されていれば、その辞書を優先して利用します.
  2. dict_type
    • dict_type オプションで辞書の種類を直接指定することもできます。
    • small, core, full の3種類が指定可能です。
    • config_pathdict_type で異なる辞書が指定されている場合、dict_type が優先されます。
fromsudachipyimporttokenizerfromsudachipyimportdictionary# デフォルトは sudachidict_core が設定されているtokenizer_obj=dictionary.Dictionary().create() # /path/to/sudachi.json の systemDict で指定されている辞書が設定されるtokenizer_obj=dictionary.Dictionary(config_path="/path/to/sudachi.json").create() # dict_type で指定された辞書が設定されるtokenizer_obj=dictionary.Dictionary(dict_type="core").create() # sudachidict_core (デフォルトと同じ)tokenizer_obj=dictionary.Dictionary(dict_type="small").create() # sudachidict_smalltokenizer_obj=dictionary.Dictionary(dict_type="full").create() # sudachidict_full# dict_type (sudachidict_full) が優先されるtokenizer_obj=dictionary.Dictionary(config_path="/path/to/sudachi.json", dict_type="full").create() 

辞書の設定ファイル

また、sudachi.jsonで辞書ファイルを切り替えることができます。

辞書のファイルパス systemDict は、絶対パスと相対パスのどちらでも指定可能です。

相対パスは、辞書の設定ファイルからの相対パスです。

{
"systemDict" : "relative/path/to/system.dic",
...
}

デフォルトはsudachipy/resources/sudachi.jsonを参照します。 sudachi.jsonを新たに用意する場合は -rで指定してください.

$ sudachipy -r path/to/sudachi.json

ユーザー辞書

ユーザー辞書user.dicを使用する場合は、sudachi.jsonを好きな場所に配置し、sudachi.jsonからuser.dicへの相対パスをuserDictの値に追加してください。

{"userDict" : ["relative/path/to/user.dic"],
...
}

そして、その sudachi.json-rで指定します。

$ sudachipy -r path/to/sudachi.json

サブコマンドubuildによってユーザー辞書を作成できます。

WARNING: v0.3.* ubuildはバグを含んでいます

$ sudachipy ubuild -h
usage: sudachipy ubuild [-h] [-d string] [-o file] [-s file] file [file ...]
Build User Dictionary
positional arguments:
file source files with CSV format (one or more)
optional arguments:
-h, --help show this help message and exit
-d string description comment to be embedded on dictionary
-o file output file (default: user.dic)
-s file system dictionary path (default: system core dictionary path)

辞書ファイル形式についてはuser_dict.mdを参照してください。

システム辞書のカスタマイズ

$ sudachipy build -h
usage: sudachipy build [-h] [-o file] [-d string] -m file file [file ...]
Build Sudachi Dictionary
positional arguments:
file source files with CSV format (one of more)
optional arguments:
-h, --help show this help message and exit
-o file output file (default: system.dic)
-d string description comment to be embedded on dictionary
required named arguments:
-m file connection matrix file with MeCab's matrix.def format

カスタマイズしたシステム辞書system.dicを使用する場合は、sudachi.jsonを好きな場所に配置し、sudachi.jsonからsystem.dicへの相対パスでsystemDictの値を上書きしてください。

{
"systemDict" : "relative/path/to/system.dic",
...
}

そして、その sudachi.json-rで指定します。

$ sudachipy -r path/to/sudachi.json

開発者向け

Cython Build

$ python setup.py build_ext --inplace

Code Format

scripts/format.shを実行して、コードが正しいフォーマットかを確認してください。

flake8flake8-import-orderflake8-buitinsが必要です。 (requirements.txt参照).

Test

scripts/test.shを実行してテストしてください。

Contact

SudachiとSudachiPyはWAP Tokushima Laboratory of AI and NLPによって開発されています.

開発者やユーザーの方々が質問したり議論するためのSlackワークスペースを用意しています。