Skip to content

Commit

Permalink
Merge pull request #258 from megagonlabs/release_520
Browse files Browse the repository at this point in the history
Add clause recognition logic
  • Loading branch information
hiroshi-matsuda-rit committed Mar 30, 2024
2 parents 62dd7ac + 6ed7b37 commit f67b498
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 62 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

An Open Source Japanese NLP Library, based on Universal Dependencies

***Please read the [Important changes](#ginza-510) before you upgrade GiNZA.***
***Please read the [Important changes](#ginza-520) before you upgrade GiNZA.***

[日本語ページはこちら](https://megagonlabs.github.io/ginza/)

Expand Down Expand Up @@ -67,7 +67,7 @@ Contains information from mC4 which is made available under the ODC Attribution
```

## Runtime Environment
This project is developed with Python>=3.6 and pip for it.
This project is developed with Python>=3.8 and pip for it.
We do not recommend to use Anaconda environment because the pip install step may not work properly.

Please also see the Development Environment section below.
Expand Down Expand Up @@ -120,15 +120,15 @@ After pressing enter key, you will get the parsed results with [CoNLL-U Syntacti
$ ginza
銀座でランチをご一緒しましょう。
# text = 銀座でランチをご一緒しましょう。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 obl _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|Reading=ゴ
6 一緒 一緒 VERB 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|Reading=イッショ
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 nmod _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City|ClauseHead=6
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ|ClauseHead=6
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ|ClauseHead=6
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ|ClauseHead=6
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|NP_B|Reading=ゴ|ClauseHead=6
6 一緒 一緒 NOUN 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|NP_I|Reading=イッショ|ClauseHead=6
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ|ClauseHead=6
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ|ClauseHead=6
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。|ClauseHead=6

```
`ginzame` command provides tokenization function like [MeCab](https://taku910.github.io/mecab/).
Expand Down Expand Up @@ -239,6 +239,13 @@ Please read the official documents to compile user dictionaries with `sudachipy`

### version 5.x

#### ginza-5.2.0
- 2024-03-31
- Require python>=3.8
- Migrate to spaCy v3.7
- New functionality
- add Japanese clause recognition API (experimental)

#### ginza-5.1.3
- 2023-09-25
- Migrate to spaCy v3.6
Expand Down
6 changes: 3 additions & 3 deletions config/ja_ginza.meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"lang":"ja",
"name":"ginza",
"version":"5.1.3",
"version":"5.2.0",
"description":"Japanese multi-task CNN trained on UD-Japanese BCCWJ r2.8 + GSK2014-A(2019). Assigns word2vec token vectors. Components: tok2vec, parser, ner, morphologizer, atteribute_ruler, compound_splitter, bunsetu_recognizer.",
"author":"Megagon Labs Tokyo.",
"email":"ginza@megagon.ai",
Expand Down Expand Up @@ -34,7 +34,7 @@
}
],
"parent_package":"spacy",
"spacy_version":">=3.2.0,<3.7.0",
"spacy_version":">=3.4.4,<4.0.0",
"spacy_git_version":"0fc3dee77",
"vectors":{
"width":300,
Expand Down Expand Up @@ -66,6 +66,6 @@
"requirements":[
"sudachipy>=0.6.2,<0.7.0",
"sudachidict_core>=20210802",
"ginza>=5.1.0,<5.2.0"
"ginza>=5.2.0,<5.3.0"
]
}
7 changes: 3 additions & 4 deletions config/ja_ginza_bert_large.meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"lang":"ja",
"name":"ginza_bert_large",
"version":"5.1.3b1",
"version":"5.2.0b1",
"description":"Japanese multi-task CNN trained on UD-Japanese BCCWJ r2.8 + GSK2014-A(2019) + transformers-ud-japanese-electra--base. Components: transformer, parser, atteribute_ruler, ner, morphologizer, compound_splitter, bunsetu_recognizer.",
"author":"Megagon Labs Tokyo.",
"email":"ginza@megagon.ai",
Expand Down Expand Up @@ -33,7 +33,7 @@
"author":"Tohoku University"
}
],
"spacy_version":">=3.6.1,<3.7.0",
"spacy_version":">=3.6.1,<4.0.0",
"spacy_git_version":"458bc5f45",
"pipeline":[
"transformer",
Expand Down Expand Up @@ -65,10 +65,9 @@
"requirements":[
"sudachipy>=0.6.7,<0.7.0",
"sudachidict_core>=20230711",
"spacy>=3.6.1,<3.7.0",
"spacy-transformers>=1.2.5,<1.3.0",
"fugashi>=1.3.0",
"unidic-lite>=1.0.8",
"ginza>=5.1.3,<5.2.0"
"ginza>=5.2.0,<5.3.0"
]
}
7 changes: 3 additions & 4 deletions config/ja_ginza_electra.meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"lang":"ja",
"name":"ginza_electra",
"version":"5.1.3",
"version":"5.2.0",
"description":"Japanese multi-task CNN trained on UD-Japanese BCCWJ r2.8 + GSK2014-A(2019) + transformers-ud-japanese-electra--base. Components: transformer, parser, atteribute_ruler, ner, morphologizer, compound_splitter, bunsetu_recognizer.",
"author":"Megagon Labs Tokyo.",
"email":"ginza@megagon.ai",
Expand Down Expand Up @@ -41,7 +41,7 @@
}
],
"parent_package":"spacy",
"spacy_version":">=3.2.0,<3.7.0",
"spacy_version":">=3.4.4,<4.0.0",
"spacy_git_version":"0fc3dee77",
"pipeline":[
"transformer",
Expand Down Expand Up @@ -75,7 +75,6 @@
"sudachidict_core>=20210802",
"sudachitra>=0.1.6,<0.2.0",
"ginza-transformers>=0.4.0,<0.5.0",
"ginza>=5.1.0,<5.2.0",
"spacy-transformers>=1.1.2,<1.2.0"
"ginza>=5.2.0,<5.3.0"
]
}
6 changes: 5 additions & 1 deletion docs/bunsetu_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ for frame, count in sorted(frames.items(), key=lambda t: -t[1]):
print(count, *frame, sep="\t") # 出現頻度の高い順に表示
```

#### 表1 GiNZA v4で追加された文節APIの一覧
#### 表1 GiNZAの文節APIの一覧

| category | func or variable | description |
| --- | --- | --- |
Expand Down Expand Up @@ -72,6 +72,10 @@ for frame, count in sorted(frames.items(), key=lambda t: -t[1]):
| Subtoken | | |
| | sub_tokens() | トークンの分割情報。 |
| | set_split_mode() | デフォルトの分割モードの変更。 |
| Clause | | |
| | clauses() | 節単位に分割されたトークン列。(experimental) |
| | clause_head() | トークンが属する節のヘッドとなるトークン。(experimental) |
| | clause_head_i() | トークンが属する節のヘッドとなるトークン番号。(experimental) |

## 解説資料

Expand Down
18 changes: 9 additions & 9 deletions docs/command_line_tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
$ ginza
銀座でランチをご一緒しましょう。
# text = 銀座でランチをご一緒しましょう。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 obl _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|Reading=ゴ
6 一緒 一緒 VERB 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|Reading=イッショ
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 nmod _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City|ClauseHead=6
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ|ClauseHead=6
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ|ClauseHead=6
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ|ClauseHead=6
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|NP_B|Reading=ゴ|ClauseHead=6
6 一緒 一緒 NOUN 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|NP_I|Reading=イッショ|ClauseHead=6
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ|ClauseHead=6
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ|ClauseHead=6
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。|ClauseHead=6

```

Expand Down
60 changes: 43 additions & 17 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@

## What's new!

- [`ja_ginza_bert_large`のβ版を公開中](https://github.com/megagonlabs/ginza/releases/tag/v5.1.3)
- `GiNZA v5.2.0`をリリースしました (2024.03.31)
- 日本語の節認定のためのAPIを追加 (experimental)
- [`ja_ginza_bert_large`のβ版を公開中](https://github.com/megagonlabs/ginza/releases/tag/v5.2.0)
- [cl-tohoku/bert-large-japanese-v2](https://huggingface.co/cl-tohoku/bert-large-japanese-v2)をベースモデルに採用
- 精度が大幅に向上(LAS=0.938, UAS=0.949, UPOS=0.983, ENE=0.708)
- CUDAに対応し8GB以上のRAMを搭載したGPU環境、または、M1・M2などApple Silicon環境の利用を推奨
- `GiNZA v5.1.3`
- `spaCy v3.2 ~ v3.6`に対応
- `GiNZA v5.1`
- `spaCy v3.2``Sudachi.rs(SudachiPy v0.6.2)`に対応
- バッチ解析処理をGPU環境で50〜60%・CPU環境で10〜40%高速化
- ginzaコマンドの並列実行オプション(`ginza -p {n_process}`および`ginzame`)の処理効率を向上
- ginzaコマンドで日本語以外を含む全てのspaCyモデルが利用可能に
- `ginza -m en_core_web_md` の形でモデル名を指定することで[CoNLL-U](https://universaldependencies.org/format.html#syntactic-annotation)出力ツールとして利用可能
- [ginzaコマンドの解説ページ](https://megagonlabs.github.io/ginza/command_line_tool.html)の記述を拡充
- `ginza`コマンドで使用するGPUのgpu_idを`ginza -g 1`の形で指定可能に

***GiNZAをアップグレードする際は下記の互換性情報を確認してください。***

## GiNZA v5.2 互換性情報
- Pythonの対応バージョンが3.8以上に変更されました
- spaCyの対応バージョンがv3.4.4に変更されました
- コマンドラインのconllu出力のmisc列にClauseHeadフィールドが追加されました

## GiNZA v5.1 互換性情報
- `ginza --require_gpu`および`ginza -g`オプションが引数にgpu_idを取るようになりました
- gpu_idに-1を指定(デフォルト)するとCPUのみを使用します
Expand All @@ -43,6 +44,24 @@

## GiNZA v5 新機能

### 日本語の節認定API (experimental)

GiNZA v5.2.0で日本語の節認定機能(試用版)を実装しました。

`ginza`コマンドの実行結果のconllu出力のmisc列を拡張して、各トークンが属する節のヘッドのトークン番号を`ClauseHead`フィールドで示しています。

APIには次の関数を追加しました。
- `clauses(doc)`
- 節単位に分割されたトークン列の取得
- `clause_head(token)`
- トークンが属する節のヘッドとなるトークンの取得
- `clause_head_i(token)`
- トークンが属する節のヘッドとなるトークン番号の取得

現在の節認定の実装は次のような簡易なもので、今後さらに改良を行う予定です。
- 文に含まれる読点を節区切りの候補とする
- さらに読点で区切られた節が2文節以上で構成される場合のみ節として認定する

### Transformersモデルによる解析精度の向上

GiNZA v5の解析精度は以前のバージョンから飛躍的な向上を遂げました。精度向上の主たる貢献はTransformers事前学習モデルの導入にあります。次の図は、UD_Japanese-BCCWJ r2.8における、従来型モデルの`ja_ginza`と、Transformers事前学習モデルを用いた`ja_ginza_electra`の、依存関係ラベリングおよび単語依存構造解析の学習曲線です。
Expand All @@ -69,7 +88,7 @@ GiNZA v5の解析精度は以前のバージョンから飛躍的な向上を遂

## 実行環境

GiNZAは Python 3.6以上(および対応するpip)で動作検証を行っています。
GiNZAは Python 3.8以上(および対応するpip)で動作検証を行っています。
GiNZAをインストールする前に予めPython実行環境を構築してください。

### 実行環境のセットアップ
Expand Down Expand Up @@ -137,15 +156,15 @@ $ pip install torch thinc-apple-ops
$ ginza
銀座でランチをご一緒しましょう。
# text = 銀座でランチをご一緒しましょう。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 obl _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|Reading=ゴ
6 一緒 一緒 VERB 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|Reading=イッショ
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。
1 銀座 銀座 PROPN 名詞-固有名詞-地名-一般 _ 6 nmod _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ギンザ|NE=B-GPE|ENE=B-City|ClauseHead=6
2 で で ADP 助詞-格助詞 _ 1 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=デ|ClauseHead=6
3 ランチ ランチ NOUN 名詞-普通名詞-一般 _ 6 obj _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=SEM_HEAD|NP_B|Reading=ランチ|ClauseHead=6
4 を を ADP 助詞-格助詞 _ 3 case _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Reading=ヲ|ClauseHead=6
5 ご ご NOUN 接頭辞 _ 6 compound _ SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=CONT|NP_B|Reading=ゴ|ClauseHead=6
6 一緒 一緒 NOUN 名詞-普通名詞-サ変可能 _ 0 root _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=ROOT|NP_I|Reading=イッショ|ClauseHead=6
7 し する AUX 動詞-非自立可能 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=サ行変格,連用形-一般|Reading=シ|ClauseHead=6
8 ましょう ます AUX 助動詞 _ 6 aux _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-マス,意志推量形|Reading=マショウ|ClauseHead=6
9 。 。 PUNCT 補助記号-句点 _ 6 punct _ SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=CONT|Reading=。|ClauseHead=6

```
実行環境に`ja_ginza_electra``ja_ginza`の両方のモデルがインストールされている場合、`ginza`コマンドは`ja_ginza_electra`を優先して使用します。同じ状況で`ja_ginza`を使用するには`ginza -m`オプションでモデル名を指定します。
Expand Down Expand Up @@ -273,6 +292,13 @@ Contains information from mC4 which is made available under the ODC Attribution

### version 5.x

#### ginza-5.2.0
- 2024-03-31
- Require python>=3.8
- Migrate to spaCy v3.7
- New functionality
- add Japanese clause recognition API (experimental)

#### ginza-5.1.3
- 2023-09-25
- Migrate to spaCy v3.6
Expand Down
4 changes: 4 additions & 0 deletions ginza/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ent_label_ene", "ent_label_ontonotes",
"reading_form", "inflection",
"bunsetu_bi_label", "bunsetu_position_type", "is_bunsetu_head",
"clauses","token_clause_head",
"SEP", "default_join_func",
"traverse",
"head", "ancestors", "conjuncts", "children", "lefts", "rights", "subtree",
Expand All @@ -38,6 +39,9 @@
"bunsetu_head_tokens",
"bunsetu_bi_labels",
"bunsetu_position_types",
"clauses",
"clause_head",
"clause_head_i",
"BunsetuRecognizer",
# from compound_splitter
"CompoundSplitter",
Expand Down
Loading

0 comments on commit f67b498

Please sign in to comment.