|
1 |
| -**(このドキュメントは古いです。いまはREADME.mdを参照ください。近日アップデート予定)** |
| 1 | +# 日本語形態素解析器 SudachiPy チュートリアル |
| 2 | +[](https://pypi.python.org/pypi/sudachipy/) |
| 3 | +[](https://www.python.org/downloads/release/python-350/) |
| 4 | +[](https://travis-ci.com/WorksApplications/SudachiPy) |
| 5 | +[](https://github.com/WorksApplications/SudachiPy/blob/develop/LICENSE) |
2 | 6 |
|
3 |
| -# 日本語形態素解析器 SudachiPy([Sudachi](https://github.com/WorksApplications/Sudachi/) Pythonクローン) チュートリアル |
| 7 | +SudachiPyは日本語形態素解析器[Sudachi](https://github.com/WorksApplications/Sudachi)のpython版です。 |
4 | 8 |
|
5 |
| -pip をつかってインストールします。Python 3.5以上が必要です。 |
6 | 9 |
|
| 10 | +## とりあえず動かしたい場合 |
| 11 | + |
| 12 | +``` |
| 13 | +$ pip install sudachipy sudachidict_core |
| 14 | +
|
| 15 | +$ echo "高輪ゲートウェイ駅" | sudachipy |
| 16 | +高輪ゲートウェイ駅 名詞,固有名詞,一般,*,*,* 高輪ゲートウェイ駅 |
| 17 | +EOS |
| 18 | +
|
| 19 | +$ echo "高輪ゲートウェイ駅" | sudachipy -m A |
| 20 | +高輪 名詞,固有名詞,地名,一般,*,* 高輪 |
| 21 | +ゲートウェイ 名詞,普通名詞,一般,*,*,* ゲートウェー |
| 22 | +駅 名詞,普通名詞,一般,*,*,* 駅 |
| 23 | +EOS |
| 24 | +
|
| 25 | +$ echo "空缶空罐空きカン" | sudachipy -a |
| 26 | +空缶 名詞,普通名詞,一般,*,*,* 空き缶 空缶 アキカン 0 |
| 27 | +空罐 名詞,普通名詞,一般,*,*,* 空き缶 空罐 アキカン 0 |
| 28 | +空きカン 名詞,普通名詞,一般,*,*,* 空き缶 空きカン アキカン 0 |
| 29 | +EOS |
7 | 30 | ```
|
8 |
| -$ pip install -e git+git://github.com/WorksApplications/SudachiPy@develop#egg=SudachiPy |
| 31 | + |
| 32 | +## 導入 |
| 33 | + |
| 34 | +SudachiPyを使う場合は辞書が必要になります。 |
| 35 | +※パッケージには辞書が含まれていません。 |
| 36 | + |
| 37 | +### Step 1. SudachiPyのインストール |
| 38 | + |
| 39 | +```bash |
| 40 | +$ pip install sudachipy |
| 41 | +``` |
| 42 | + |
| 43 | +### Step 2. 辞書のインストール |
| 44 | + |
| 45 | +辞書はPythonのパッケージとしてダウンロードできます。 |
| 46 | + |
| 47 | +※辞書のダウンロードは時間がかかります (`core`辞書は約70MB ). |
| 48 | + |
| 49 | +```bash |
| 50 | +$ pip install sudachidict_core |
9 | 51 | ```
|
10 | 52 |
|
11 |
| -パッケージには辞書が含まれていません。Java版をビルドして target/system_*.dic を取得するか、[リリース一覧](https://github.com/WorksApplications/Sudachi/releases)から辞書を取得します。 |
| 53 | +また、他の辞書を選択することもできます。詳細は[このセッション](#辞書の種類)を参照してください。 |
| 54 | + |
12 | 55 |
|
| 56 | +## 使用方法(CLI) |
| 57 | +CLIでは以下のように実行できます。 |
| 58 | + |
| 59 | +```bash |
| 60 | +$ echo "外国人参政権" | sudachipy |
| 61 | +外国人参政権 名詞,普通名詞,一般,*,*,* 外国人参政権 |
| 62 | +EOS |
| 63 | +$ echo "外国人参政権" | sudachipy -m A |
| 64 | +外国 名詞,普通名詞,一般,*,*,* 外国 |
| 65 | +人 接尾辞,名詞的,一般,*,*,* 人 |
| 66 | +参政 名詞,普通名詞,一般,*,*,* 参政 |
| 67 | +権 接尾辞,名詞的,一般,*,*,* 権 |
| 68 | +EOS |
13 | 69 | ```
|
14 |
| -$ wget https://github.com/WorksApplications/Sudachi/releases/download/v0.1.1/sudachi-0.1.1-dictionary-core.zip |
15 |
| -$ unzip sudachi-0.1.1-dictionary-core.zip |
16 |
| -$ cp system_core.dic `pip show sudachipy | grep Location | sed 's/^.*: //'`/resources/system.dic |
| 70 | + |
| 71 | +```bash |
| 72 | +$ sudachipy tokenize -h |
| 73 | +usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-a] [-d] [-v] |
| 74 | + [file [file ...]] |
| 75 | + |
| 76 | +Tokenize Text |
| 77 | + |
| 78 | +positional arguments: |
| 79 | + file text written in utf-8 |
| 80 | + |
| 81 | +optional arguments: |
| 82 | + -h, --help show this help message and exit |
| 83 | + -r file the setting file in JSON format |
| 84 | + -m {A,B,C} the mode of splitting |
| 85 | + -o file the output file |
| 86 | + -a print all of the fields |
| 87 | + -d print the debug information |
| 88 | + -v, --version print sudachipy version |
17 | 89 | ```
|
18 | 90 |
|
19 |
| -コマンドラインツールの利用方法は[Java版](https://github.com/WorksApplications/Sudachi/blob/develop/docs/tutorial.md)とほぼ同じです。 |
| 91 | +### 出力形式 |
| 92 | +タブ区切りで出力されます。 |
| 93 | +デフォルトは以下の情報が含まれます。 |
| 94 | +
|
| 95 | +- 表層形 |
| 96 | +- 品詞(コンマ区切り) |
| 97 | +- 正規化表記 |
| 98 | +
|
| 99 | +オプションで `-a` を指定すると以下の情報が追加されます。 |
| 100 | +
|
| 101 | +- 辞書形 |
| 102 | +- 読み |
| 103 | +- 辞書ID |
| 104 | + - `0` システム辞書 |
| 105 | + - `1` ユーザー辞書 |
| 106 | + - `-1\t(OOV)` 未知語(辞書に含まれない単語) |
20 | 107 |
|
| 108 | +```bash |
| 109 | +$ echo "外国人参政権" | sudachipy -a |
| 110 | +外国人参政権 名詞,普通名詞,一般,*,*,* 外国人参政権 外国人参政権 ガイコクジンサンセイケン 0 |
| 111 | +EOS |
21 | 112 | ```
|
22 |
| -$ sudachipy |
23 |
| -きょうはいい天気ですね。 |
24 |
| -きょう 名詞,普通名詞,副詞可能,*,*,* 今日 |
25 |
| -は 助詞,係助詞,*,*,*,* は |
26 |
| -いい 形容詞,非自立可能,*,*,形容詞,連体形-一般 良い |
27 |
| -天気 名詞,普通名詞,一般,*,*,* 天気 |
28 |
| -です 助動詞,*,*,*,助動詞-デス,終止形-一般 です |
29 |
| -ね 助詞,終助詞,*,*,*,* ね |
30 |
| -。 補助記号,句点,*,*,*,* 。 |
| 113 | +
|
| 114 | +```bash |
| 115 | +echo "阿quei" | sudachipy -a |
| 116 | +阿 名詞,普通名詞,一般,*,*,* 阿 阿 -1 (OOV) |
| 117 | +quei 名詞,普通名詞,一般,*,*,* quei quei -1 (OOV) |
31 | 118 | EOS
|
32 | 119 | ```
|
| 120 | +
|
| 121 | +## 使用方法(Python パッケージ) |
| 122 | +
|
| 123 | +例 |
| 124 | +
|
| 125 | +```python |
| 126 | +from sudachipy import tokenizer |
| 127 | +from sudachipy import dictionary |
| 128 | + |
| 129 | +tokenizer_obj = dictionary.Dictionary().create() |
| 130 | +``` |
| 131 | +
|
| 132 | +```python |
| 133 | +# 複数粒度分割 |
| 134 | + |
| 135 | +mode = tokenizer.Tokenizer.SplitMode.C |
| 136 | +[m.surface() for m in tokenizer_obj.tokenize("国家公務員", mode)] |
| 137 | +# => ['国家公務員'] |
| 138 | + |
| 139 | +mode = tokenizer.Tokenizer.SplitMode.B |
| 140 | +[m.surface() for m in tokenizer_obj.tokenize("国家公務員", mode)] |
| 141 | +# => ['国家', '公務員'] |
| 142 | + |
| 143 | +mode = tokenizer.Tokenizer.SplitMode.A |
| 144 | +[m.surface() for m in tokenizer_obj.tokenize("国家公務員", mode)] |
| 145 | +# => ['国家', '公務', '員'] |
| 146 | +``` |
| 147 | +
|
| 148 | +```python |
| 149 | +# 形態素情報 |
| 150 | + |
| 151 | +m = tokenizer_obj.tokenize("食べ", mode)[0] |
| 152 | + |
| 153 | +m.surface() # => '食べ' |
| 154 | +m.dictionary_form() # => '食べる' |
| 155 | +m.reading_form() # => 'タベ' |
| 156 | +m.part_of_speech() # => ['動詞', '一般', '*', '*', '下一段-バ行', '連用形-一般'] |
| 157 | +``` |
| 158 | +
|
| 159 | +```python |
| 160 | +# 正規化 |
| 161 | + |
| 162 | +tokenizer_obj.tokenize("附属", mode)[0].normalized_form() |
| 163 | +# => '付属' |
| 164 | +tokenizer_obj.tokenize("SUMMER", mode)[0].normalized_form() |
| 165 | +# => 'サマー' |
| 166 | +tokenizer_obj.tokenize("シュミレーション", mode)[0].normalized_form() |
| 167 | +# => 'シミュレーション' |
| 168 | +``` |
| 169 | +
|
| 170 | +(これは `20200330` `core` 辞書による出力例です。 辞書のバージョンによって変わる可能性があります。) |
| 171 | +
|
| 172 | +## 辞書の種類 |
| 173 | +Sudachi辞書は`small`と`core`と`full`の3種類があります。 詳細は[WorksApplications/SudachiDict](https://github.com/WorksApplications/SudachiDict)を参照してください。 |
| 174 | +
|
| 175 | +SudachiPyはデフォルトでは`sudachidict_core`に設定されています。辞書設定の変更は`link -t`コマンドによって行えます。 |
| 176 | +
|
| 177 | +
|
| 178 | +```bash |
| 179 | +$ pip install sudachidict_small |
| 180 | +$ sudachipy link -t small |
| 181 | +``` |
| 182 | +
|
| 183 | +```bash |
| 184 | +$ pip install sudachidict_full |
| 185 | +$ sudachipy link -t full |
| 186 | +``` |
| 187 | +
|
| 188 | +`link -u`によってリンクを削除するとデフォルトの`sudachidict_core`を使用します。 |
| 189 | +
|
| 190 | +```bash |
| 191 | +$ sudachipy link -u |
| 192 | +``` |
| 193 | +
|
| 194 | +`sudachidict_small`, `sudachidict_core`, `sudachidict_full`はPythonのパッケージとしてインストールされます。 SudachiPyは辞書を使用するとき`sudachidict` パッケージを参照します。 `link` によって`sudachidict_*`を`sudachidict`として参照するための *symbolic link* が作られます。 |
| 195 | +
|
| 196 | +* [SudachiDict-small · PyPI](https://pypi.org/project/SudachiDict-small/) |
| 197 | +* [SudachiDict-core · PyPI](https://pypi.org/project/SudachiDict-core/) |
| 198 | +* [SudachiDict-full · PyPI](https://pypi.org/project/SudachiDict-full/) |
| 199 | +
|
| 200 | +辞書ファイルはパッケージ自体には含まれていませんが、上記のインストール時にダウンロードする処理が埋め込まれています。 |
| 201 | +
|
| 202 | +### 辞書の設定ファイル |
| 203 | +
|
| 204 | +また、`sudachi.json`で辞書ファイルを切り替えることができます。 |
| 205 | +
|
| 206 | +
|
| 207 | +``` |
| 208 | +{ |
| 209 | + "systemDict" : "relative/path/to/system.dic", |
| 210 | + ... |
| 211 | +} |
| 212 | +``` |
| 213 | +
|
| 214 | +デフォルトは[sudachipy/resources/sudachi.json](https://github.com/WorksApplications/SudachiPy/blob/develop/sudachipy/resources/sudachi.json)を参照します。 `sudachi.json`を新たに用意する場合は `-r`で指定してください. |
| 215 | +
|
| 216 | +```bash |
| 217 | +$ sudachipy -r path/to/sudachi.json |
| 218 | +``` |
| 219 | +
|
| 220 | +## ユーザー辞書 |
| 221 | +ユーザー辞書`user.dic`を使用する場合は、[sudachi.json](https://github.com/WorksApplications/SudachiPy/blob/develop/sudachipy/resources/sudachi.json)を好きな場所に配置し、`sudachi.json`から`user.dic`への相対パスをuserDictの値に追加してください。 |
| 222 | +
|
| 223 | +```js |
| 224 | +{ |
| 225 | + "userDict" : ["relative/path/to/user.dic"], |
| 226 | + ... |
| 227 | +} |
| 228 | +``` |
| 229 | +
|
| 230 | +そして、その `sudachi.json`を`-r`で指定します。 |
| 231 | +
|
| 232 | +```bash |
| 233 | +$ sudachipy -r path/to/sudachi.json |
| 234 | +``` |
| 235 | +
|
| 236 | +サブコマンド`ubuild`によってユーザー辞書を作成できます。 |
| 237 | +
|
| 238 | +**WARNING: v0.3.\* ubuildはバグを含んでいます** |
| 239 | +
|
| 240 | +```bash |
| 241 | +$ sudachipy ubuild -h |
| 242 | +usage: sudachipy ubuild [-h] [-d string] [-o file] [-s file] file [file ...] |
| 243 | + |
| 244 | +Build User Dictionary |
| 245 | + |
| 246 | +positional arguments: |
| 247 | + file source files with CSV format (one or more) |
| 248 | + |
| 249 | +optional arguments: |
| 250 | + -h, --help show this help message and exit |
| 251 | + -d string description comment to be embedded on dictionary |
| 252 | + -o file output file (default: user.dic) |
| 253 | + -s file system dictionary (default: linked system_dic, see link -h) |
| 254 | +``` |
| 255 | +
|
| 256 | +辞書ファイル形式については[user_dict.md](https://github.com/WorksApplications/Sudachi/blob/develop/docs/user_dict.md)を参照してください。 |
| 257 | +
|
| 258 | +
|
| 259 | +## システム辞書のカスタマイズ |
| 260 | +
|
| 261 | +```bash |
| 262 | +$ sudachipy build -h |
| 263 | +usage: sudachipy build [-h] [-o file] [-d string] -m file file [file ...] |
| 264 | + |
| 265 | +Build Sudachi Dictionary |
| 266 | + |
| 267 | +positional arguments: |
| 268 | + file source files with CSV format (one of more) |
| 269 | + |
| 270 | +optional arguments: |
| 271 | + -h, --help show this help message and exit |
| 272 | + -o file output file (default: system.dic) |
| 273 | + -d string description comment to be embedded on dictionary |
| 274 | + |
| 275 | +required named arguments: |
| 276 | + -m file connection matrix file with MeCab's matrix.def format |
| 277 | +``` |
| 278 | +
|
| 279 | +カスタマイズしたシステム辞書`system.dic`を使用する場合は、[sudachi.json](https://github.com/WorksApplications/SudachiPy/blob/develop/sudachipy/resources/sudachi.json)を好きな場所に配置し、`sudachi.json`から`system.dic`への相対パスでsystemDictの値を上書きしてください。 |
| 280 | +
|
| 281 | +``` |
| 282 | +{ |
| 283 | + "systemDict" : "relative/path/to/system.dic", |
| 284 | + ... |
| 285 | +} |
| 286 | +``` |
| 287 | +
|
| 288 | +そして、その `sudachi.json`を`-r`で指定します。 |
| 289 | +
|
| 290 | +```bash |
| 291 | +$ sudachipy -r path/to/sudachi.json |
| 292 | +``` |
| 293 | +
|
| 294 | +## 開発者向け |
| 295 | +### Cython Build |
| 296 | +
|
| 297 | +```sh |
| 298 | +$ python setup.py build_ext --inplace |
| 299 | +``` |
| 300 | +
|
| 301 | +### Code Format |
| 302 | +
|
| 303 | +`scripts/format.sh`を実行して、コードが正しいフォーマットかを確認してください。 |
| 304 | +
|
| 305 | +`flake8` `flake8-import-order` `flake8-buitins`が必要です。 (`requirements.txt`参照). |
| 306 | +
|
| 307 | +### Test |
| 308 | +
|
| 309 | +`scripts/test.sh`を実行してテストしてください。 |
| 310 | +
|
| 311 | +
|
| 312 | +## Contact |
| 313 | +
|
| 314 | +SudachiとSudachiPyは[WAP Tokushima Laboratory of AI and NLP](http://nlp.worksap.co.jp/)によって開発されています. |
| 315 | +
|
| 316 | +開発者やユーザーの方々が質問したり議論するためのSlackワークスペースを用意しています。 |
| 317 | +
|
| 318 | +- https://sudachi-dev.slack.com/ ([こちら](https://join.slack.com/t/sudachi-dev/shared_invite/enQtMzg2NTI2NjYxNTUyLTMyYmNkZWQ0Y2E5NmQxMTI3ZGM3NDU0NzU4NGE1Y2UwYTVmNTViYjJmNDI0MWZiYTg4ODNmMzgxYTQ3ZmI2OWU)から招待を受けてください) |
| 319 | +
|
| 320 | +
|
0 commit comments