monaka 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
monaka-0.1.0/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, komiya-lab, Monaka.AI, Hiroaki Ozaki
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
monaka-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,444 @@
1
+ Metadata-Version: 2.4
2
+ Name: monaka
3
+ Version: 0.1.0
4
+ Summary: A Japanese parser (including support for historical Japanese)
5
+ Author: Hiroaki Ozaki (Monaka.AI)
6
+ License-Expression: BSD-3-Clause
7
+ Project-URL: Documentation, https://monaka-ai.github.io/monaka/
8
+ Project-URL: Source, https://github.com/Monaka-AI/monaka
9
+ Project-URL: Tracker, https://github.com/Monaka-AI/monaka/issues
10
+ Keywords: Japanese,NLP,parser,long unit word,bunsetsu,dependency parsing,historical Japanese
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Text Processing :: Linguistic
14
+ Classifier: Natural Language :: Japanese
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: numpy<2.0.0
19
+ Requires-Dist: protobuf
20
+ Requires-Dist: torch
21
+ Requires-Dist: transformers
22
+ Requires-Dist: registrable
23
+ Requires-Dist: fugashi
24
+ Requires-Dist: ipadic
25
+ Requires-Dist: typer
26
+ Requires-Dist: rich
27
+ Requires-Dist: prettytable
28
+ Requires-Dist: Flask
29
+ Requires-Dist: conllu
30
+ Requires-Dist: requests
31
+ Provides-Extra: dep
32
+ Requires-Dist: ufal.chu_liu_edmonds; extra == "dep"
33
+ Provides-Extra: train
34
+ Requires-Dist: tensorboard; extra == "train"
35
+ Requires-Dist: tqdm; extra == "train"
36
+ Provides-Extra: doc
37
+ Requires-Dist: sphinx; extra == "doc"
38
+ Requires-Dist: pydata-sphinx-theme; extra == "doc"
39
+ Dynamic: license-file
40
+
41
+ # Monaka
42
+ A Japanese parser (including support for historical Japanese)
43
+
44
+ Documentation (Japanese): https://monaka-ai.github.io/monaka/
45
+
46
+ ## Installation
47
+
48
+ Monaka requires Python 3.9 or later.
49
+
50
+ If you use a GPU, install [PyTorch](https://pytorch.org/get-started/locally/) for your environment (CUDA version) first.
51
+ Otherwise the default `torch` package is installed automatically.
52
+
53
+ ```sh
54
+ pip install git+https://github.com/Monaka-AI/monaka.git
55
+ ```
56
+
57
+ Optional features are installed with extras:
58
+
59
+ | extra | for |
60
+ |-------|-----|
61
+ | `dep` | Bunsetsu dependency parsing (`ufal.chu_liu_edmonds`) |
62
+ | `train` | training models with `monaka_train` |
63
+ | `doc` | building the documentation |
64
+
65
+ ```sh
66
+ pip install "monaka[dep,train] @ git+https://github.com/Monaka-AI/monaka.git"
67
+ ```
68
+
69
+ ## Parse
70
+
71
+ First, download and install appropriate UniDic dictionary:
72
+ ```sh
73
+ monaka download wabun
74
+ ```
75
+
76
+ Available dictionaries: (x/y means y is alternative of x.)
77
+ | name | discription |
78
+ |------|-------------|
79
+ | gendai | 現代書き言葉 |
80
+ | spoken/unidic-spoken | 現代話し言葉 |
81
+ | novel/65_novel | 近現代口語小説 |
82
+ | qkana/60b_qkana | 旧仮名口語 |
83
+ | kindai/60a_kindai-bungo" | 近代文語 |
84
+ | 50a_kinsei-bungo" | 近世文語 |
85
+ | 50c_kinsei-edo" | 近世江戸 |
86
+ | 50b_kinsei-kamigata" | 近世上方 |
87
+ | kinsei | 近世江戸口語 |
88
+ | kyogen/40_chusei-kougo | 中世口語 |
89
+ | wakan/30_chusei-bungo | 中世文語 |
90
+ | wabun/20_chuko | 中古和文 |
91
+ | manyo/10_jodai | 上代語 |
92
+ | 70_waka | 和歌 |
93
+ | 80_kansai_hougen | 関西方言 |
94
+
95
+ Dictionaries are stored in the dictionary directory (`~/.cache/monaka` by default; see [Dictionary and model directories](#dictionary-and-model-directories)).
96
+ You can also use a UniDic dictionary that is already installed on your system by passing its path to `--dic`:
97
+ ```sh
98
+ monaka parse {model} 今日はいい天気ですね --dic /usr/local/lib/mecab/dic/unidic
99
+ ```
100
+
101
+ ### Dictionary and model directories
102
+
103
+ The directories for UniDic dictionaries and Monaka models can be changed with environment variables or command options.
104
+ The priority is: command option > environment variable > `~/.cache/monaka` (default; `$XDG_CACHE_HOME/monaka` if `XDG_CACHE_HOME` is set).
105
+
106
+ | target | option | environment variable | default |
107
+ |--------|--------|----------------------|---------|
108
+ | UniDic dictionaries | `--dic-dir` | `MONAKA_DIC_DIR` | `~/.cache/monaka` |
109
+ | Monaka models | `--model-dir` | `MONAKA_MODEL_DIR` | `~/.cache/monaka` |
110
+
111
+ For example, to keep dictionaries in a shared MeCab dictionary directory:
112
+ ```sh
113
+ export MONAKA_DIC_DIR=/usr/local/lib/mecab/dic
114
+ monaka download wabun # -> /usr/local/lib/mecab/dic/wabun
115
+ monaka parse {model} 今は昔竹取の翁といふものありけり --dic wabun
116
+ ```
117
+
118
+ `--dic` accepts either a dictionary name in the dictionary directory or a path to a dictionary directory.
119
+ Dictionaries and models that were installed in `monaka/resource` (the default of earlier versions) are still found.
120
+
121
+ Then, call parse command:
122
+ ```sh
123
+ monaka parse {model} 今日はいい天気ですね
124
+ ```
125
+
126
+ `{model}` is either a model name (e.g. `bccwj_core_bigbird`) or a path to a model directory.
127
+ A model name that is not installed yet is downloaded automatically from GitHub (see [Model download](#model-download)).
128
+
129
+ output:
130
+ ```json
131
+ {
132
+ "tokens": [
133
+ "今日",
134
+ "は",
135
+ "いい",
136
+ "天気",
137
+ "です",
138
+ "ね"
139
+ ],
140
+ "pos": [
141
+ "名詞-普通名詞-副詞可能",
142
+ "助詞-係助詞",
143
+ "形容詞-非自立可能-形容詞",
144
+ "名詞-普通名詞-一般",
145
+ "助動詞-助動詞-デス",
146
+ "助詞-終助詞"
147
+ ],
148
+ "luw": [
149
+ "名詞-普通名詞-一般",
150
+ "助詞-係助詞",
151
+ "形容詞-一般-形容詞",
152
+ "名詞-普通名詞-一般",
153
+ "助動詞-助動詞-デス",
154
+ "助詞-終助詞"
155
+ ],
156
+ "chunk": [
157
+ "B",
158
+ "I",
159
+ "B",
160
+ "B",
161
+ "I",
162
+ "I"
163
+ ],
164
+ "sentence": "今日はいい天気ですね"
165
+ }
166
+ ```
167
+
168
+ You can specify output format ("bunsetsu-split" and "luw-split" )
169
+
170
+ ```sh
171
+ monaka parse {model} 今日はいい天気ですね --output-format bunsetu-split
172
+
173
+ 今日は いい 天気ですね
174
+ ```
175
+
176
+ You can also specify MeCab compatible format:
177
+ ```sh
178
+ monaka parse {model} 今日はいい天気ですね --output-format mecab
179
+
180
+ 今日 キョー キョウ 今日 名詞-普通名詞-副詞可能 * * * 名詞-普通名詞-一般 B
181
+ は ワ ハ は 助詞-係助詞 * * * 助詞-係助詞 I
182
+ いい イー ヨイ 良い 形容詞-非自立可能 形容詞 連体形-一般 * 形容詞-一般-形容詞 B
183
+ 天気 テンキ テンキ 天気 名詞-普通名詞-一般 * * * 名詞-普通名詞-一般 B
184
+ です デス デス です 助動詞 助動詞-デス 終止形-一般 * 助動詞-助動詞-デス I
185
+ ね ネ ネ ね 助詞-終助詞 * * * 助詞-終助詞 I
186
+ EOS
187
+ ```
188
+
189
+ You can use MeCab formatting strings:
190
+ ```sh
191
+ monaka parse {model} 今日はいい天気ですね --output-format mecab --node-format "%m,%f[0,1,2],%l,%b\n" --eos-format "\n"
192
+
193
+ 今日,名詞,普通名詞,副詞可能,名詞-普通名詞-一般,B
194
+ は,助詞,係助詞,*,助詞-係助詞,I
195
+ いい,形容詞,非自立可能,*,形容詞-一般-形容詞,B
196
+ 天気,名詞,普通名詞,一般,名詞-普通名詞-一般,B
197
+ です,助動詞,*,*,助動詞-助動詞-デス,I
198
+ ね,助詞,終助詞,*,助詞-終助詞,I
199
+ ```
200
+
201
+ | Format | Detail |
202
+ |--------|--------|
203
+ | %b | Bunsetsu boundary flag |
204
+ | %l | LUW PoS tag |
205
+
206
+ ## Python API (pipeline DSL)
207
+
208
+ `monaka.dsl` provides a spaCy-like pipeline for SUW (short unit word), LUW (long unit word), Bunsetsu and Bunsetsu dependency analysis.
209
+
210
+ ```python
211
+ import monaka.dsl as monaka
212
+
213
+ nlp = monaka.load("bccwj_core_bigbird", dic="gendai") # MeCab + UniDic -> "luw" component
214
+ doc = nlp("今日はいい天気ですね")
215
+
216
+ for token in doc: # SUW
217
+ print(token.text, token.pos, token.lemma, token.reading)
218
+ for luw in doc.luws: # LUW
219
+ print(luw.text, luw.pos)
220
+ print([b.text for b in doc.bunsetsu]) # Bunsetsu: ['今日は', 'いい', '天気ですね']
221
+ print(doc.to_table()) # all annotations as a table
222
+ ```
223
+
224
+ Add components to a pipeline (`dependency` and `luw_lemma` require their own trained models, and `dependency` requires the `dep` extra):
225
+ ```python
226
+ nlp = monaka.Pipeline(dic="wabun", device="0") # UniDic for Heian Japanese, GPU 0
227
+ nlp.add_pipe("luw", model="heian_all_bigbird") # LUW and Bunsetsu
228
+ nlp.add_pipe("dependency", model="/path/to/dep_model") # Bunsetsu dependency
229
+
230
+ doc = nlp("今は昔竹取の翁といふものありけり")
231
+ for b in doc.bunsetsu:
232
+ print(b.text, "->", b.head.text if b.head else "ROOT", b.dep)
233
+ ```
234
+
235
+ Process many sentences in batches and write them in any `--output-format`:
236
+ ```python
237
+ for doc in nlp.pipe(sentences, batch_size=64):
238
+ print(doc.format("mecab", node_format="%m\t%l\t%b\n"))
239
+ # doc.format("conllu"), doc.to_json(), Doc.from_json(...) are also available
240
+ ```
241
+
242
+ Custom components are functions (or `monaka.Component` subclasses) that take and return a `Doc`:
243
+ ```python
244
+ @monaka.component("noun_luws")
245
+ def noun_luws(doc):
246
+ doc.user_data["nouns"] = [l.text for l in doc.luws if l.pos.startswith("名詞")]
247
+ return doc
248
+
249
+ nlp.add_pipe("noun_luws", after="luw")
250
+ ```
251
+
252
+ | component | description | main arguments |
253
+ |-----------|-------------|----------------|
254
+ | `luw` | LUW boundary/PoS and Bunsetsu boundary (ensemble of models supported) | `model`, `device`, `batch_size`, `model_dir` |
255
+ | `dependency` | Bunsetsu dependency (place after `luw`) | `model`, `device`, `batch_size`, `left2right` |
256
+ | `luw_lemma` | LUW lemma (place after `luw`) | `model`, `device`, `min_suws` |
257
+
258
+ Model names are resolved (and downloaded) in the same way as the `monaka` command, and
259
+ `dic_dir` / `model_dir` (or `MONAKA_DIC_DIR` / `MONAKA_MODEL_DIR`) are respected.
260
+ See `docs/source/04_dsl.rst` for details.
261
+
262
+ ## Server
263
+ You can start parsing server (`--model-dir` and `--dic-dir`, or `MONAKA_MODEL_DIR` and `MONAKA_DIC_DIR`, are also available)
264
+ ```sh
265
+ monaka_server
266
+
267
+ * Serving Flask app 'monaka.server'
268
+ * Debug mode: off
269
+ WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
270
+ * Running on http://127.0.0.1:5000
271
+ Press CTRL+C to quit
272
+ ```
273
+
274
+ Parse service URL: POST <server>/model/<model>/dic/<dic>/parse
275
+
276
+ Example:
277
+ ```
278
+ http://127.0.0.1:5000/model/all_in_one/dic/wabun/parse
279
+
280
+ {
281
+ "sentence": ["今は昔竹取の翁といふものありけり"],
282
+ "node_format": "%m\t%f[9]\t%f[6]\t%f[7]\t%F-[0,1,2,3]\t%f[4]\t%f[5]\t%f[13]\t%f[26]\t%f[27]\t%f[28]\n",
283
+ "output_format": "mecab",
284
+ "unk_format": "%m\t%m\t%m\t%m\tUNK\t%f[4]\t%f[5]\t\n",
285
+ "eos_format": "EOS\n",
286
+ "bos_format": ""
287
+ }
288
+
289
+ ```
290
+
291
+ Please see scripts/monaka_client.sh for more detail.
292
+
293
+
294
+ ## Model download
295
+
296
+ Trained models are published at https://github.com/Monaka-AI/datastore .
297
+ List available and installed models:
298
+ ```sh
299
+ monaka models
300
+ ```
301
+
302
+ Download a model in advance (models are stored in `<model directory>/<model name>`; the model directory is `~/.cache/monaka` by default and can be changed with `--model-dir` or `MONAKA_MODEL_DIR`):
303
+ ```sh
304
+ monaka download bccwj_core_bigbird
305
+ ```
306
+ Use `--force` to download a model again.
307
+ You can skip this step: `monaka parse` and `monaka predict` download a model automatically when it is not installed.
308
+
309
+ Available models:
310
+ | name | description | License |
311
+ |------|-------------|---------|
312
+ | bccwj_core_bigbird | nlp-waseda/bigbird-base-japanese trained on BCCWJ1 core data. Ensemble of the three cross-validation models `bccwj_core_bigbird_cv0`, `_cv1` and `_cv2` (each can also be used alone). BCCWJ2 official model. | Apache License 2.0 |
313
+ | modern_kamrakura_PB_modern_bigbird | nlp-waseda/bigbird-base-japanese trained on CHJ Meiroku-zasshi (Meiji period). Sample release. | Apache License 2.0 |
314
+
315
+ See the datastore repository for the latest list.
316
+
317
+ ### Ensemble
318
+
319
+ `parse` and `predict` average the outputs of multiple models (ensemble).
320
+ A model whose directory contains several sub-models (e.g. `cv_0`, `cv_1`, ...) is ensembled automatically.
321
+ You can also combine models explicitly with a comma-separated list of names or paths:
322
+ ```sh
323
+ monaka parse bccwj_core_bigbird_cv0,bccwj_core_bigbird_cv1 今日はいい天気ですね
324
+ monaka predict bccwj_core_bigbird input.jsonl --device 0
325
+ ```
326
+ The input of `predict` is a JSON-L file in which each line has a `"sentence"` field.
327
+
328
+ ## Training monaka model
329
+
330
+ ### LUW and Bunsetsu tokenizer/chunker
331
+
332
+ #### Creating Dataset
333
+
334
+ A dataset should be JSON-L formatted and its each line shoud contains following fields:
335
+ ```json
336
+ {
337
+ "sentence": "str",
338
+ "tokens": ["a list of SUW"],
339
+ "pos": ["POS-tag labels for each SUW"],
340
+ "labels": ["Target labels for each SUW"]
341
+ }
342
+ ```
343
+
344
+ We provide data conversion script for UD-Japanese data.
345
+ Here is an example command to convert UD-Japanese-GSD train data.
346
+
347
+ ```sh
348
+ monaka_train ud2jsonl ja_gsd-ud-train.conllu ja_gsd-ud-train.jsonl
349
+ ```
350
+
351
+ After creating the dataset files, then create label and pos-tag dictionaries:
352
+
353
+ ```sh
354
+ monaka_train create-vocab [output_dir] ja_gsd-ud-train.jsonl ja_gsd-ud-dev.jsonl ja_gsd-ud-test.jsonl
355
+ ```
356
+
357
+ #### Creating training configuration JSON file
358
+ An example configuration is in config/luw_chunk_base.config.json.
359
+
360
+ ```json
361
+ {
362
+ "train_files": [""],
363
+ "dev_files": [""],
364
+ "test_files": null,
365
+ "dataeset_options" :{
366
+ "label_file": "vocab/labels.json",
367
+ "pos_file": "vocab/pos.json",
368
+ "lm_tokenizer": "bert-tohoku-ja",
369
+ "lm_tokenizer_config": {},
370
+ "pos_as_tokens": false,
371
+ "label_for_all_subwords": true,
372
+ "max_length": 512
373
+ },
374
+ "model_name": "SeqTagging",
375
+ "model_config": {
376
+ "n_pos_emb": 256,
377
+ "pos_dropout": 0.5,
378
+ "mlp_dropout": 0.5,
379
+ "lm_class_name": "AutoLM",
380
+ "lm_class_config": {
381
+ "model": "cl-tohoku/bert-base-japanese-whole-word-masking",
382
+ "requires_grad": true,
383
+ "use_scalar_mix": false,
384
+ "sclar_mix_dropout": 0.5,
385
+ "use_attentions": false
386
+ },
387
+ "pos_padding_idx": 1
388
+ },
389
+ "batch_size": 32,
390
+ "epochs": 50,
391
+ "lr": 2e-5,
392
+ "decay": 0.75,
393
+ "decay_steps": 5000,
394
+ "evaluate_step": 500
395
+ }
396
+ ```
397
+ Each field in the above corresponds to the arguments of monaka.Trainer.
398
+ You can change parser model (SeqTagging to any class defined as subclass of monaka.model.LUWParserModel) and LM class (AutoLM to any class defined as subclass of monaka.module.LMEmbedding).
399
+
400
+ #### Execution
401
+ You can specify device to use and local_rank if you do distribute training.
402
+ ```sh
403
+ monaka_train train --device "0" [config_file] [output_dir]
404
+ ```
405
+
406
+ All checkpoint data, a log file, and configuration files are saved under [output_dir].
407
+ If you want to mointor training with tensorboad, tensorboad logs are output under [output_dir]/tb.
408
+
409
+ ```sh
410
+ tensorboard --logdir [output_dir]/tb
411
+ ```
412
+
413
+ ## References and Citations
414
+ [Long Unit Word Tokenization and Bunsetsu Segmentation of Historical Japanese](https://aclanthology.org/2024.ml4al-1.6) (Ozaki et al., ML4AL-WS 2024)
415
+
416
+ ```bibtex
417
+ @inproceedings{ozaki-etal-2024-long,
418
+ title = "Long Unit Word Tokenization and Bunsetsu Segmentation of Historical {J}apanese",
419
+ author = "Ozaki, Hiroaki and
420
+ Komiya, Kanako and
421
+ Asahara, Masayuki and
422
+ Ogiso, Toshinobu",
423
+ editor = "Pavlopoulos, John and
424
+ Sommerschield, Thea and
425
+ Assael, Yannis and
426
+ Gordin, Shai and
427
+ Cho, Kyunghyun and
428
+ Passarotti, Marco and
429
+ Sprugnoli, Rachele and
430
+ Liu, Yudong and
431
+ Li, Bin and
432
+ Anderson, Adam",
433
+ booktitle = "Proceedings of the 1st Workshop on Machine Learning for Ancient Languages (ML4AL 2024)",
434
+ month = aug,
435
+ year = "2024",
436
+ address = "Hybrid in Bangkok, Thailand and online",
437
+ publisher = "Association for Computational Linguistics",
438
+ url = "https://aclanthology.org/2024.ml4al-1.6",
439
+ doi = "10.18653/v1/2024.ml4al-1.6",
440
+ pages = "48--55",
441
+ abstract = "In Japanese, the natural minimal phrase of a sentence is the {``}bunsetsu{''} and it serves as a natural boundary of a sentence for native speakers rather than words, and thus grammatical analysis in Japanese linguistics commonly operates on the basis of bunsetsu units.In contrast, because Japanese does not have delimiters between words, there are two major categories of word definition, namely, Short Unit Words (SUWs) and Long Unit Words (LUWs).Though a SUW dictionary is available, LUW is not.Hence, this study focuses on providing deep learning-based (or LLM-based) bunsetsu and Long Unit Words analyzer for the Heian period (AD 794-1185) and evaluating its performances.We model the parser as transformer-based joint sequential labels model, which combine bunsetsu BI tag, LUW BI tag, and LUW Part-of-Speech (POS) tag for each SUW token.We train our models on corpora of each period including contemporary and historical Japanese.The results range from 0.976 to 0.996 in f1 value for both bunsetsu and LUW reconstruction indicating that our models achieve comparable performance with models for a contemporary Japanese corpus.Through the statistical analysis and diachronic case study, the estimation of bunsetsu could be influenced by the grammaticalization of morphemes.",
442
+ }
443
+
444
+ ```