Metadata-Version: 2.4
Name: monaka
Version: 0.1.1
Summary: A Japanese parser (including support for historical Japanese)
Author: Hiroaki Ozaki (Monaka.AI)
License-Expression: BSD-3-Clause
Project-URL: Documentation, https://monaka-ai.github.io/monaka/
Project-URL: Source, https://github.com/Monaka-AI/monaka
Project-URL: Tracker, https://github.com/Monaka-AI/monaka/issues
Keywords: Japanese,NLP,parser,long unit word,bunsetsu,dependency parsing,historical Japanese
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Natural Language :: Japanese
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<2.0.0
Requires-Dist: protobuf
Requires-Dist: torch>=2.6.0
Requires-Dist: transformers<5,>=4.0
Requires-Dist: registrable
Requires-Dist: fugashi
Requires-Dist: ipadic
Requires-Dist: typer
Requires-Dist: rich
Requires-Dist: prettytable
Requires-Dist: Flask
Requires-Dist: conllu
Requires-Dist: requests
Provides-Extra: dep
Requires-Dist: ufal.chu_liu_edmonds; extra == "dep"
Provides-Extra: train
Requires-Dist: tensorboard; extra == "train"
Requires-Dist: tqdm; extra == "train"
Provides-Extra: doc
Requires-Dist: sphinx; extra == "doc"
Requires-Dist: pydata-sphinx-theme; extra == "doc"
Dynamic: license-file

# Monaka
A Japanese parser (including support for historical Japanese)

Documentation (Japanese): https://monaka-ai.github.io/monaka/

## Installation

Monaka requires Python 3.9 or later.

If you use a GPU, install [PyTorch](https://pytorch.org/get-started/locally/) for your environment (CUDA version) first.
Otherwise the default `torch` package is installed automatically.

```sh
pip install monaka
```

To install the latest development version from GitHub:
```sh
pip install git+https://github.com/Monaka-AI/monaka.git
```

Optional features are installed with extras:

| extra | for |
|-------|-----|
| `dep` | Bunsetsu dependency parsing (`ufal.chu_liu_edmonds`) |
| `train` | training models with `monaka_train` |
| `doc` | building the documentation |

```sh
pip install "monaka[dep,train]"
```

## Parse

First, download  and install appropriate UniDic dictionary:
```sh
monaka download wabun
```

Available dictionaries: (x/y means y is alternative of x.)
| name | discription |
|------|-------------|
| gendai | 現代書き言葉 |
| spoken/unidic-spoken | 現代話し言葉 |
| novel/65_novel | 近現代口語小説 |
| qkana/60b_qkana | 旧仮名口語 |
| kindai/60a_kindai-bungo" | 近代文語 |
| 50a_kinsei-bungo" | 近世文語 |
| 50c_kinsei-edo" | 近世江戸 |
| 50b_kinsei-kamigata" | 近世上方 |
| kinsei | 近世江戸口語 |
| kyogen/40_chusei-kougo | 中世口語 |
| wakan/30_chusei-bungo | 中世文語 |
| wabun/20_chuko | 中古和文 |
| manyo/10_jodai | 上代語 |
| 70_waka | 和歌 |
| 80_kansai_hougen | 関西方言 |

Dictionaries are stored in the dictionary directory (`~/.cache/monaka` by default; see [Dictionary and model directories](#dictionary-and-model-directories)).
You can also use a UniDic dictionary that is already installed on your system by passing its path to `--dic`:
```sh
monaka parse {model} 今日はいい天気ですね --dic /usr/local/lib/mecab/dic/unidic
```

### Dictionary and model directories

The directories for UniDic dictionaries and Monaka models can be changed with environment variables or command options.
The priority is: command option > environment variable > `~/.cache/monaka` (default; `$XDG_CACHE_HOME/monaka` if `XDG_CACHE_HOME` is set).

| target | option | environment variable | default |
|--------|--------|----------------------|---------|
| UniDic dictionaries | `--dic-dir` | `MONAKA_DIC_DIR` | `~/.cache/monaka` |
| Monaka models | `--model-dir` | `MONAKA_MODEL_DIR` | `~/.cache/monaka` |

For example, to keep dictionaries in a shared MeCab dictionary directory:
```sh
export MONAKA_DIC_DIR=/usr/local/lib/mecab/dic
monaka download wabun          # -> /usr/local/lib/mecab/dic/wabun
monaka parse {model} 今は昔竹取の翁といふものありけり --dic wabun
```

`--dic` accepts either a dictionary name in the dictionary directory or a path to a dictionary directory.
Dictionaries and models that were installed in `monaka/resource` (the default of earlier versions) are still found.

Then, call parse command:
```sh
monaka parse {model} 今日はいい天気ですね
```

`{model}` is either a model name (e.g. `bccwj_core_bigbird`) or a path to a model directory.
A model name that is not installed yet is downloaded automatically from GitHub (see [Model download](#model-download)).

output:
```json
{
  "tokens": [
    "今日",
    "は",
    "いい",
    "天気",
    "です",
    "ね"
  ],
  "pos": [
    "名詞-普通名詞-副詞可能",
    "助詞-係助詞",
    "形容詞-非自立可能-形容詞",
    "名詞-普通名詞-一般",
    "助動詞-助動詞-デス",
    "助詞-終助詞"
  ],
  "luw": [
    "名詞-普通名詞-一般",
    "助詞-係助詞",
    "形容詞-一般-形容詞",
    "名詞-普通名詞-一般",
    "助動詞-助動詞-デス",
    "助詞-終助詞"
  ],
  "chunk": [
    "B",
    "I",
    "B",
    "B",
    "I",
    "I"
  ],
  "sentence": "今日はいい天気ですね"
}
```

You can specify output format ("bunsetsu-split" and "luw-split" )

```sh
monaka parse {model} 今日はいい天気ですね --output-format bunsetu-split

今日は いい 天気ですね
```

You can also specify MeCab compatible format:
```sh
monaka parse {model} 今日はいい天気ですね --output-format mecab

今日    キョー  キョウ  今日    名詞-普通名詞-副詞可能  *       *       *       名詞-普通名詞-一般      B
は      ワ      ハ      は      助詞-係助詞     *       *       *       助詞-係助詞     I
いい    イー    ヨイ    良い    形容詞-非自立可能       形容詞  連体形-一般     *       形容詞-一般-形容詞      B
天気    テンキ  テンキ  天気    名詞-普通名詞-一般      *       *       *       名詞-普通名詞-一般      B
です    デス    デス    です    助動詞  助動詞-デス     終止形-一般     *       助動詞-助動詞-デス      I
ね      ネ      ネ      ね      助詞-終助詞     *       *       *       助詞-終助詞     I
EOS
```

You can use MeCab formatting strings:
```sh
monaka parse {model} 今日はいい天気ですね --output-format mecab --node-format "%m,%f[0,1,2],%l,%b\n" --eos-format "\n"

今日,名詞,普通名詞,副詞可能,名詞-普通名詞-一般,B
は,助詞,係助詞,*,助詞-係助詞,I
いい,形容詞,非自立可能,*,形容詞-一般-形容詞,B
天気,名詞,普通名詞,一般,名詞-普通名詞-一般,B
です,助動詞,*,*,助動詞-助動詞-デス,I
ね,助詞,終助詞,*,助詞-終助詞,I
```

| Format | Detail |
|--------|--------|
| %b     | Bunsetsu boundary flag |
| %l     | LUW PoS tag |

## Python API (pipeline DSL)

`monaka.dsl` provides a spaCy-like pipeline for SUW (short unit word), LUW (long unit word), Bunsetsu and Bunsetsu dependency analysis.

```python
import monaka.dsl as monaka

nlp = monaka.load("bccwj_core_bigbird", dic="gendai")   # MeCab + UniDic -> "luw" component
doc = nlp("今日はいい天気ですね")

for token in doc:                       # SUW
    print(token.text, token.pos, token.lemma, token.reading)
for luw in doc.luws:                    # LUW
    print(luw.text, luw.pos)
print([b.text for b in doc.bunsetsu])   # Bunsetsu: ['今日は', 'いい', '天気ですね']
print(doc.to_table())                   # all annotations as a table
```

Add components to a pipeline (`dependency` and `luw_lemma` require their own trained models, and `dependency` requires the `dep` extra):
```python
nlp = monaka.Pipeline(dic="wabun", device="0")         # UniDic for Heian Japanese, GPU 0
nlp.add_pipe("luw", model="heian_all_bigbird")          # LUW and Bunsetsu
nlp.add_pipe("dependency", model="/path/to/dep_model")  # Bunsetsu dependency

doc = nlp("今は昔竹取の翁といふものありけり")
for b in doc.bunsetsu:
    print(b.text, "->", b.head.text if b.head else "ROOT", b.dep)
```

Process many sentences in batches and write them in any `--output-format`:
```python
for doc in nlp.pipe(sentences, batch_size=64):
    print(doc.format("mecab", node_format="%m\t%l\t%b\n"))
    # doc.format("conllu"), doc.to_json(), Doc.from_json(...) are also available
```

Custom components are functions (or `monaka.Component` subclasses) that take and return a `Doc`:
```python
@monaka.component("noun_luws")
def noun_luws(doc):
    doc.user_data["nouns"] = [l.text for l in doc.luws if l.pos.startswith("名詞")]
    return doc

nlp.add_pipe("noun_luws", after="luw")
```

| component | description | main arguments |
|-----------|-------------|----------------|
| `luw` | LUW boundary/PoS and Bunsetsu boundary (ensemble of models supported) | `model`, `device`, `batch_size`, `model_dir` |
| `dependency` | Bunsetsu dependency (place after `luw`) | `model`, `device`, `batch_size`, `left2right` |
| `luw_lemma` | LUW lemma (place after `luw`) | `model`, `device`, `min_suws` |

Model names are resolved (and downloaded) in the same way as the `monaka` command, and
`dic_dir` / `model_dir` (or `MONAKA_DIC_DIR` / `MONAKA_MODEL_DIR`) are respected.
See `docs/source/04_dsl.rst` for details.

## Server
You can start parsing server (`--model-dir` and `--dic-dir`, or `MONAKA_MODEL_DIR` and `MONAKA_DIC_DIR`, are also available)
```sh
monaka_server

 * Serving Flask app 'monaka.server'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
```

Parse service URL: POST <server>/model/<model>/dic/<dic>/parse

Example:
```
http://127.0.0.1:5000/model/all_in_one/dic/wabun/parse

{
  "sentence": ["今は昔竹取の翁といふものありけり"],
  "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",
  "output_format": "mecab",
  "unk_format": "%m\t%m\t%m\t%m\tUNK\t%f[4]\t%f[5]\t\n",
  "eos_format": "EOS\n",
  "bos_format": ""
}

```

Please see scripts/monaka_client.sh for more detail.


## Model download

Trained models are published at https://github.com/Monaka-AI/datastore .
List available and installed models:
```sh
monaka models
```

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`):
```sh
monaka download bccwj_core_bigbird
```
Use `--force` to download a model again.
You can skip this step: `monaka parse` and `monaka predict` download a model automatically when it is not installed.

Available models:
| name | description | License |
|------|-------------|---------|
| 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 |
| modern_kamrakura_PB_modern_bigbird | nlp-waseda/bigbird-base-japanese trained on CHJ Meiroku-zasshi (Meiji period). Sample release. | Apache License 2.0 |

See the datastore repository for the latest list.

### Ensemble

`parse` and `predict` average the outputs of multiple models (ensemble).
A model whose directory contains several sub-models (e.g. `cv_0`, `cv_1`, ...) is ensembled automatically.
You can also combine models explicitly with a comma-separated list of names or paths:
```sh
monaka parse bccwj_core_bigbird_cv0,bccwj_core_bigbird_cv1 今日はいい天気ですね
monaka predict bccwj_core_bigbird input.jsonl --device 0
```
The input of `predict` is a JSON-L file in which each line has a `"sentence"` field.

## Training monaka model

### LUW and Bunsetsu tokenizer/chunker

#### Creating Dataset

A dataset should be JSON-L formatted and its each line shoud contains following fields:
```json
    {
        "sentence": "str", 
        "tokens": ["a list of SUW"],
        "pos": ["POS-tag labels for each SUW"],
        "labels": ["Target labels for each SUW"]
    }
```

We provide data conversion script for UD-Japanese data.
Here is an example command to convert UD-Japanese-GSD train data.

```sh
monaka_train ud2jsonl ja_gsd-ud-train.conllu ja_gsd-ud-train.jsonl
```

After creating the dataset files, then create label and pos-tag dictionaries:

```sh
monaka_train create-vocab [output_dir] ja_gsd-ud-train.jsonl ja_gsd-ud-dev.jsonl ja_gsd-ud-test.jsonl
```

#### Creating training configuration JSON file
An example configuration is in config/luw_chunk_base.config.json.

```json
{
    "train_files": [""],
    "dev_files": [""],
    "test_files": null,
    "dataeset_options" :{
        "label_file": "vocab/labels.json",
        "pos_file": "vocab/pos.json",
        "lm_tokenizer": "bert-tohoku-ja",
        "lm_tokenizer_config": {},
        "pos_as_tokens": false,
        "label_for_all_subwords": true,
        "max_length": 512
    },
    "model_name": "SeqTagging",
    "model_config": {
        "n_pos_emb": 256,
        "pos_dropout": 0.5,
        "mlp_dropout": 0.5,
        "lm_class_name": "AutoLM",
        "lm_class_config": {
            "model": "cl-tohoku/bert-base-japanese-whole-word-masking",
            "requires_grad": true,
            "use_scalar_mix": false,
            "sclar_mix_dropout": 0.5,
            "use_attentions": false
        },
        "pos_padding_idx": 1
    },
    "batch_size": 32,
    "epochs": 50,
    "lr": 2e-5,
    "decay": 0.75,
    "decay_steps": 5000,
    "evaluate_step": 500
}
```
Each field in the above corresponds to the arguments of monaka.Trainer.
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).

#### Execution
You can specify device to use and local_rank if you do distribute training.
```sh
monaka_train train --device "0" [config_file] [output_dir]
```

All checkpoint data, a log file, and configuration files are saved under [output_dir].
If you want to mointor training with tensorboad, tensorboad logs are output under [output_dir]/tb.
 
```sh
tensorboard --logdir [output_dir]/tb
```

## References and Citations
[Long Unit Word Tokenization and Bunsetsu Segmentation of Historical Japanese](https://aclanthology.org/2024.ml4al-1.6) (Ozaki et al., ML4AL-WS 2024)

```bibtex
@inproceedings{ozaki-etal-2024-long,
    title = "Long Unit Word Tokenization and Bunsetsu Segmentation of Historical {J}apanese",
    author = "Ozaki, Hiroaki  and
      Komiya, Kanako  and
      Asahara, Masayuki  and
      Ogiso, Toshinobu",
    editor = "Pavlopoulos, John  and
      Sommerschield, Thea  and
      Assael, Yannis  and
      Gordin, Shai  and
      Cho, Kyunghyun  and
      Passarotti, Marco  and
      Sprugnoli, Rachele  and
      Liu, Yudong  and
      Li, Bin  and
      Anderson, Adam",
    booktitle = "Proceedings of the 1st Workshop on Machine Learning for Ancient Languages (ML4AL 2024)",
    month = aug,
    year = "2024",
    address = "Hybrid in Bangkok, Thailand and online",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2024.ml4al-1.6",
    doi = "10.18653/v1/2024.ml4al-1.6",
    pages = "48--55",
    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.",
}

```
