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

Commit 89b6d27

Browse files
authored
Merge pull request #149 from katsutan/feature/readme
add Japanese tutorial
2 parents 6871e4f + cb0d791 commit 89b6d27

File tree

2 files changed

+308
-18
lines changed

2 files changed

+308
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[![Build Status](https://travis-ci.com/WorksApplications/SudachiPy.svg?branch=develop)](https://travis-ci.com/WorksApplications/SudachiPy)
55
[![](https://img.shields.io/github/license/WorksApplications/SudachiPy.svg)](https://github.com/WorksApplications/SudachiPy/blob/develop/LICENSE)
66

7+
[日本語](/docs/tutorial.md)
8+
79
SudachiPy is a Python version of [Sudachi](https://github.com/WorksApplications/Sudachi), a Japanese morphological analyzer.
810

911

docs/tutorial.md

Lines changed: 306 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,320 @@
1-
**(このドキュメントは古いです。いまはREADME.mdを参照ください。近日アップデート予定)**
1+
# 日本語形態素解析器 SudachiPy チュートリアル
2+
[![PyPi version](https://img.shields.io/pypi/v/sudachipy.svg)](https://pypi.python.org/pypi/sudachipy/)
3+
[![](https://img.shields.io/badge/python-3.5+-blue.svg)](https://www.python.org/downloads/release/python-350/)
4+
[![Build Status](https://travis-ci.com/WorksApplications/SudachiPy.svg?branch=develop)](https://travis-ci.com/WorksApplications/SudachiPy)
5+
[![](https://img.shields.io/github/license/WorksApplications/SudachiPy.svg)](https://github.com/WorksApplications/SudachiPy/blob/develop/LICENSE)
26

3-
# 日本語形態素解析器 SudachiPy([Sudachi](https://github.com/WorksApplications/Sudachi/) Pythonクローン) チュートリアル
7+
SudachiPyは日本語形態素解析器[Sudachi](https://github.com/WorksApplications/Sudachi)のpython版です。
48

5-
pip をつかってインストールします。Python 3.5以上が必要です。
69

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
730
```
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
951
```
1052

11-
パッケージには辞書が含まれていません。Java版をビルドして target/system_*.dic を取得するか、[リリース一覧](https://github.com/WorksApplications/Sudachi/releases)から辞書を取得します。
53+
また、他の辞書を選択することもできます。詳細は[このセッション](#辞書の種類)を参照してください。
54+
1255

56+
## 使用方法(CLI)
57+
CLIでは以下のように実行できます。
58+
59+
```bash
60+
$ echo "外国人参政権" | sudachipy
61+
外国人参政権 名詞,普通名詞,一般,*,*,* 外国人参政権
62+
EOS
63+
$ echo "外国人参政権" | sudachipy -m A
64+
外国 名詞,普通名詞,一般,*,*,* 外国
65+
人 接尾辞,名詞的,一般,*,*,*
66+
参政 名詞,普通名詞,一般,*,*,* 参政
67+
権 接尾辞,名詞的,一般,*,*,*
68+
EOS
1369
```
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
1789
```
1890
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)` 未知語(辞書に含まれない単語)
20107
108+
```bash
109+
$ echo "外国人参政権" | sudachipy -a
110+
外国人参政権 名詞,普通名詞,一般,*,*,* 外国人参政権 外国人参政権 ガイコクジンサンセイケン 0
111+
EOS
21112
```
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)
31118
EOS
32119
```
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

Comments
 (0)