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

Commit 6437d51

Browse files
authored
Merge pull request #152 from WorksApplications/feature_changing_dictionary_linking_mechanism
Feature changing dictionary linking mechanism
2 parents 1c407ca + 6369574 commit 6437d51

File tree

7 files changed

+286
-116
lines changed

7 files changed

+286
-116
lines changed

README.md

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ EOS
7070

7171
```bash
7272
$ sudachipy tokenize -h
73-
usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-a] [-d] [-v]
73+
usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-s string]
74+
[-a] [-d] [-v]
7475
[file [file ...]]
7576

7677
Tokenize Text
@@ -83,6 +84,7 @@ optional arguments:
8384
-r file the setting file in JSON format
8485
-m {A,B,C} the mode of splitting
8586
-o file the output file
87+
-s string sudachidict type
8688
-a print all of the fields
8789
-d print the debug information
8890
-v, --version print sudachipy version
@@ -175,33 +177,71 @@ tokenizer_obj.tokenize("シュミレーション", mode)[0].normalized_form()
175177
176178
## Dictionary Edition
177179
180+
**WARNING: `sudachipy link` is no longer available in SudachiPy v0.5.2 and later. **
181+
182+
178183
There are three editions of Sudachi Dictionary, namely, `small`, `core`, and `full`. See [WorksApplications/SudachiDict](https://github.com/WorksApplications/SudachiDict) for the detail.
179184
180-
SudachiPy uses `sudachidict_core` by default. You can specify the dictionary with the `link -t` command.
185+
SudachiPy uses `sudachidict_core` by default.
186+
187+
Dictionaries are installed as Python packages `sudachidict_small`, `sudachidict_core`, and `sudachidict_full`.
188+
189+
* [SudachiDict-small · PyPI](https://pypi.org/project/SudachiDict-small/)
190+
* [SudachiDict-core · PyPI](https://pypi.org/project/SudachiDict-core/)
191+
* [SudachiDict-full · PyPI](https://pypi.org/project/SudachiDict-full/)
192+
193+
The dictionary files are not in the package itself, but it is downloaded upon installation.
194+
195+
### Dictionary option: command line
196+
197+
You can specify the dictionary with the tokenize option `-s`.
181198
182199
```bash
183200
$ pip install sudachidict_small
184-
$ sudachipy link -t small
201+
$ echo "外国人参政権" | sudachipy -s small
185202
```
186203
187204
```bash
188205
$ pip install sudachidict_full
189-
$ sudachipy link -t full
206+
$ echo "外国人参政権" | sudachipy -s full
190207
```
191208
192-
You can remove the dictionary link with the `link -u` commnad.
209+
### Dictionary option: Python package
193210
194-
```bash
195-
$ sudachipy link -u
211+
You can specify the dictionary with the `Dicionary()` argument; `config_path` or `dict_type`.
212+
213+
```python
214+
class Dictionary(config_path=None, resource_dir=None, dict_type=None)
196215
```
197216
198-
Dictionaries are installed as Python packages `sudachidict_small`, `sudachidict_core`, and `sudachidict_full`. SudachiPy tries to refer `sudachidict` package to use a dictionary. The `link` subcommand creates *a symbolic link* of `sudachidict_*` as `sudachidict`, to switch the packages.
217+
1. `config_path`
218+
* You can specify the file path to the setting file with `config_path` (See [Dictionary in The Setting File](#Dictionary in The Setting File) for the detail).
219+
* If the dictionary file is specified in the setting file as `systemDict`, SudachiPy will use the dictionary.
220+
2. `dict_type`
221+
* You can also specify the dictionary type with `dict_type`.
222+
* The available arguments are `small`, `core`, or `full`.
223+
* If different dictionaries are specified with `config_path` and `dict_type`, **a dictionary defined `dict_type` overrides** those defined in the config path.
199224
200-
* [SudachiDict-small · PyPI](https://pypi.org/project/SudachiDict-small/)
201-
* [SudachiDict-core · PyPI](https://pypi.org/project/SudachiDict-core/)
202-
* [SudachiDict-full · PyPI](https://pypi.org/project/SudachiDict-full/)
225+
```python
226+
from sudachipy import tokenizer
227+
from sudachipy import dictionary
228+
229+
# default: sudachidict_core
230+
tokenizer_obj = dictionary.Dictionary().create()
231+
232+
# The dictionary given by the `systemDict` key in the config file (/path/to/sudachi.json) will be used
233+
tokenizer_obj = dictionary.Dictionary(config_path="/path/to/sudachi.json").create()
234+
235+
# The dictionary specified by `dict_type` will be set.
236+
tokenizer_obj = dictionary.Dictionary(dict_type="core").create() # sudachidict_core (same as default)
237+
tokenizer_obj = dictionary.Dictionary(dict_type="small").create() # sudachidict_small
238+
tokenizer_obj = dictionary.Dictionary(dict_type="full").create() # sudachidict_full
239+
240+
# The dictionary specified by `dict_type` overrides those defined in the config path.
241+
# In the following code, `sudachidict_full` will be used regardless of a dictionary defined in the config file.
242+
tokenizer_obj = dictionary.Dictionary(config_path="/path/to/sudachi.json", dict_type="full").create()
243+
```
203244
204-
The dictionary files are not in the package itself, but it is downloaded upon installation.
205245
206246
### Dictionary in The Setting File
207247
@@ -256,7 +296,7 @@ optional arguments:
256296
-h, --help show this help message and exit
257297
-d string description comment to be embedded on dictionary
258298
-o file output file (default: user.dic)
259-
-s file system dictionary (default: linked system_dic, see link -h)
299+
-s file system dictionary path (default: system core dictionary path)
260300
```
261301
262302
About the dictionary file format, please refer to [this document](https://github.com/WorksApplications/Sudachi/blob/develop/docs/user_dict.md) (written in Japanese, English version is not available yet).

docs/tutorial.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ EOS
7070

7171
```bash
7272
$ sudachipy tokenize -h
73-
usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-a] [-d] [-v]
73+
usage: sudachipy tokenize [-h] [-r file] [-m {A,B,C}] [-o file] [-s string]
74+
[-a] [-d] [-v]
7475
[file [file ...]]
7576

7677
Tokenize Text
@@ -83,6 +84,7 @@ optional arguments:
8384
-r file the setting file in JSON format
8485
-m {A,B,C} the mode of splitting
8586
-o file the output file
87+
-s string sudachidict type
8688
-a print all of the fields
8789
-d print the debug information
8890
-v, --version print sudachipy version
@@ -170,39 +172,80 @@ tokenizer_obj.tokenize("シュミレーション", mode)[0].normalized_form()
170172
(これは `20200330` `core` 辞書による出力例です。 辞書のバージョンによって変わる可能性があります。)
171173
172174
## 辞書の種類
175+
176+
**WARNING: `sudachipy link` コマンドは SudachiPy v0.5.2 以降から利用できなくなりました. **
177+
173178
Sudachi辞書は`small``core``full`の3種類があります。 詳細は[WorksApplications/SudachiDict](https://github.com/WorksApplications/SudachiDict)を参照してください。
174179
175-
SudachiPyはデフォルトでは`sudachidict_core`に設定されています。辞書設定の変更は`link -t`コマンドによって行えます。
180+
SudachiPyはデフォルトでは`sudachidict_core`に設定されています。
181+
182+
`sudachidict_small`, `sudachidict_core`, `sudachidict_full`はPythonのパッケージとしてインストールされます。
183+
184+
* [SudachiDict-small · PyPI](https://pypi.org/project/SudachiDict-small/)
185+
* [SudachiDict-core · PyPI](https://pypi.org/project/SudachiDict-core/)
186+
* [SudachiDict-full · PyPI](https://pypi.org/project/SudachiDict-full/)
187+
188+
辞書ファイルはパッケージ自体には含まれていませんが、上記のインストール時にダウンロードする処理が埋め込まれています。
189+
190+
### 辞書オプション: コマンドライン
191+
192+
辞書設定の変更は`-s`オプションで指定することができます。
176193
177194
178195
```bash
179196
$ pip install sudachidict_small
180-
$ sudachipy link -t small
197+
$ echo "外国人参政権" | sudachipy -s small
181198
```
182199
183200
```bash
184201
$ pip install sudachidict_full
185-
$ sudachipy link -t full
202+
$ echo "外国人参政権" | sudachipy -s full
186203
```
187204
188-
`link -u`によってリンクを削除するとデフォルトの`sudachidict_core`を使用します。
205+
### 辞書オプション: Python パッケージ
189206
190-
```bash
191-
$ sudachipy link -u
207+
Dictionary の引数 `config_path` または `dict_type` から利用する辞書を指定することができます。
208+
209+
```python
210+
class Dictionary(config_path=None, resource_dir=None, dict_type=None)
192211
```
193212
194-
`sudachidict_small`, `sudachidict_core`, `sudachidict_full`はPythonのパッケージとしてインストールされます。 SudachiPyは辞書を使用するとき`sudachidict` パッケージを参照します。 `link` によって`sudachidict_*``sudachidict`として参照するための *symbolic link* が作られます。
213+
1. `config_path`
214+
* `config_path` で辞書の設定ファイルのパスを指定することができます([辞書の設定ファイル](#辞書の設定ファイル) 参照)。
215+
* 指定した辞書の設定ファイルに、辞書のファイルパス `systemDict` が記述されていれば、その辞書を優先して利用します.
216+
2. `dict_type`
217+
* `dict_type` オプションで辞書の種類を直接指定することもできます。
218+
* `small`, `core`, `full` の3種類が指定可能です。
219+
* `config_path``dict_type` で異なる辞書が指定されている場合、**`dict_type` が優先**されます。
195220
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/)
221+
```python
222+
from sudachipy import tokenizer
223+
from sudachipy import dictionary
224+
225+
# デフォルトは sudachidict_core が設定されている
226+
tokenizer_obj = dictionary.Dictionary().create()
227+
228+
# /path/to/sudachi.json の systemDict で指定されている辞書が設定される
229+
tokenizer_obj = dictionary.Dictionary(config_path="/path/to/sudachi.json").create()
230+
231+
# dict_type で指定された辞書が設定される
232+
tokenizer_obj = dictionary.Dictionary(dict_type="core").create() # sudachidict_core (デフォルトと同じ)
233+
tokenizer_obj = dictionary.Dictionary(dict_type="small").create() # sudachidict_small
234+
tokenizer_obj = dictionary.Dictionary(dict_type="full").create() # sudachidict_full
235+
236+
# dict_type (sudachidict_full) が優先される
237+
tokenizer_obj = dictionary.Dictionary(config_path="/path/to/sudachi.json", dict_type="full").create()
238+
```
199239
200-
辞書ファイルはパッケージ自体には含まれていませんが、上記のインストール時にダウンロードする処理が埋め込まれています。
201240
202241
### 辞書の設定ファイル
203242
204243
また、`sudachi.json`で辞書ファイルを切り替えることができます。
205244
245+
辞書のファイルパス `systemDict` は、絶対パスと相対パスのどちらでも指定可能です。
246+
247+
相対パスは、辞書の設定ファイルからの相対パスです。
248+
206249
207250
```
208251
{
@@ -250,7 +293,7 @@ optional arguments:
250293
-h, --help show this help message and exit
251294
-d string description comment to be embedded on dictionary
252295
-o file output file (default: user.dic)
253-
-s file system dictionary (default: linked system_dic, see link -h)
296+
-s file system dictionary path (default: system core dictionary path)
254297
```
255298
256299
辞書ファイル形式については[user_dict.md](https://github.com/WorksApplications/Sudachi/blob/develop/docs/user_dict.md)を参照してください。

sudachipy/command_line.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from . import __version__
2323
from . import dictionary
2424
from . import tokenizer
25-
from .config import set_default_dict_package, settings, unlink_default_dict_package
25+
from .config import settings
2626
from .dictionarylib import BinaryDictionary
2727
from .dictionarylib import SYSTEM_DICT_VERSION_2, USER_DICT_VERSION_3
2828
from .dictionarylib.dictionarybuilder import DictionaryBuilder
@@ -125,22 +125,6 @@ def _command_build(args, print_usage):
125125
builder.build(args.in_files, rf, wf)
126126

127127

128-
def _command_link(args, print_usage):
129-
output = sys.stdout
130-
if args.unlink:
131-
unlink_default_dict_package(output=output)
132-
return
133-
134-
dict_package = 'sudachidict_' + args.dict_type
135-
try:
136-
return set_default_dict_package(dict_package, output=output)
137-
except ImportError:
138-
print('Package `{0}` does not exist.\n'
139-
'You may install it with a command `$ pip install {0}`'
140-
.format(dict_package), file=sys.stderr)
141-
exit(1)
142-
143-
144128
def _command_tokenize(args, print_usage):
145129
if args.version:
146130
print_version()
@@ -169,7 +153,10 @@ def _command_tokenize(args, print_usage):
169153
enable_dump = args.d
170154

171155
try:
172-
dict_ = dictionary.Dictionary(config_path=args.fpath_setting)
156+
if args.system_dict_type is not None:
157+
dict_ = dictionary.Dictionary(config_path=args.fpath_setting, dict_type=args.system_dict_type)
158+
else:
159+
dict_ = dictionary.Dictionary(config_path=args.fpath_setting)
173160
tokenizer_obj = dict_.create()
174161
input_ = fileinput.input(args.in_files, openhook=fileinput.hook_encoded("utf-8"))
175162
run(tokenizer_obj, mode, input_, print_all, stdout_logger, enable_dump)
@@ -192,18 +179,14 @@ def main():
192179
parser_tk.add_argument("-r", dest="fpath_setting", metavar="file", help="the setting file in JSON format")
193180
parser_tk.add_argument("-m", dest="mode", choices=["A", "B", "C"], default="C", help="the mode of splitting")
194181
parser_tk.add_argument("-o", dest="fpath_out", metavar="file", help="the output file")
182+
parser_tk.add_argument("-s", dest="system_dict_type", metavar='string', choices=["small", "core", "full"],
183+
help="sudachidict type")
195184
parser_tk.add_argument("-a", action="store_true", help="print all of the fields")
196185
parser_tk.add_argument("-d", action="store_true", help="print the debug information")
197186
parser_tk.add_argument("-v", "--version", action="store_true", dest="version", help="print sudachipy version")
198187
parser_tk.add_argument("in_files", metavar="file", nargs=argparse.ZERO_OR_MORE, help='text written in utf-8')
199188
parser_tk.set_defaults(handler=_command_tokenize, print_usage=parser_tk.print_usage)
200189

201-
# link default dict package
202-
parser_ln = subparsers.add_parser('link', help='see `link -h`', description='Link Default Dict Package')
203-
parser_ln.add_argument("-t", dest="dict_type", choices=["small", "core", "full"], default="core", help="dict dict")
204-
parser_ln.add_argument("-u", dest="unlink", action="store_true", help="unlink sudachidict")
205-
parser_ln.set_defaults(handler=_command_link, print_usage=parser_ln.print_usage)
206-
207190
# build dictionary parser
208191
parser_bd = subparsers.add_parser('build', help='see `build -h`', description='Build Sudachi Dictionary')
209192
parser_bd.add_argument('-o', dest='out_file', metavar='file', default='system.dic',
@@ -224,7 +207,7 @@ def main():
224207
parser_ubd.add_argument('-o', dest='out_file', metavar='file', default='user.dic',
225208
help='output file (default: user.dic)')
226209
parser_ubd.add_argument('-s', dest='system_dic', metavar='file', required=False,
227-
help='system dictionary (default: linked system_dic, see link -h)')
210+
help='system dictionary path (default: system core dictionary path)')
228211
parser_ubd.add_argument("in_files", metavar="file", nargs=argparse.ONE_OR_MORE,
229212
help='source files with CSV format (one or more)')
230213
parser_ubd.set_defaults(handler=_command_user_build, print_usage=parser_ubd.print_usage)

0 commit comments

Comments
 (0)