pyNormalizeNumexp
数量表現や時間表現の抽出・正規化を行うNormalizeNumexpのPython実装です。
本家でもSWIGによるPythonバインディングが提供されていますが、NormalizeNumexp本体のインストールでトラブルに遭うことが多いため、全実装をPythonに移植しました。
Prerequisites
Python >=3.9, <=3.12
Installation
pip install pynormalizenumexp
Usage
from pynormalizenumexp.normalize_numexp import NormalizeNumexp
normalizer = NormalizeNumexp("ja")
results = normalizer.normalize("魔女狩りは15世紀~18世紀にかけてみられ、全ヨーロッパで4万人が処刑された", as_dict=True)
for r in results:
print(r)
# {'type': 'abstime', 'original_expr': '15世紀~18世紀', 'position_start': 5, 'position_end': 14, 'counter': 'none', 'value_lower_bound': {'year': 1401, 'month': inf, 'day': inf, 'hour': inf, 'minute': inf, 'second': inf}, 'value_upper_bound': {'year': 1800, 'month': -inf, 'day': -inf, 'hour': -inf, 'minute': -inf, 'second': -inf}, 'value_lower_bound_abs': None, 'value_upper_bound_abs': None, 'value_lower_bound_rel': None, 'value_upper_bound_rel': None, 'options': []}
# {'type': 'numerical', 'original_expr': '4万人', 'position_start': 29, 'position_end': 32, 'counter': '人', 'value_lower_bound': 40000, 'value_upper_bound': 40000, 'value_lower_bound_abs': None, 'value_upper_bound_abs': None, 'value_lower_bound_rel': None, 'value_upper_bound_rel': None, 'options': []}
NormalizeNumexpクラスの引数に言語識別子を指定します。(例:日本語であればja)- 本家では英語
enや中国語zhにも対応していますが、本ライブラリでは日本語のみに対応しています。(将来的には英語・中国語も入れる予定です)
- 本家では英語
NormalizeNumexpクラスのnormalize関数に抽出・正規化対象のテキストを指定します。as_dict引数にTrueを指定することで、返り値の数量・時間表現のオブジェクトがdict型になります。- 数量・時間表現のオブジェクトの属性については
Expressionクラスを参照してください。
- 数量・時間表現のオブジェクトの属性については
- 返り値が
dict型の場合のデータ構造は以下の通りです。{ "type": str, # 表現種別(numerical:数量、abstime:絶対時間、reltime:相対時間、duration:期間) "original_expr": str, # 数値・時間表現の文字列 "position_start": int, # 抽出元テキストにおける開始位置 "position_end": int, # 抽出元テキストにおける終了位置 "counter": str, # 「人」や「匹」などの単位(typeがnumerical以外の場合は "none" になる) "value_lower_bound": None | int | float | Dict[str, int | float], # ※1 "value_upper_bound": None | int | float | Dict[str, int | float], # ※1 "value_lower_bound_abs": None | Dict[str, int | float], # ※2 "value_upper_bound_abs": None | Dict[str, int | float], # ※2 "value_lower_bound_rel": None | Dict[str, int | float], # ※3 "value_upper_bound_rel": None | Dict[str, int | float], # ※3 "options": List[str] }
- ※1:数量・時間表現の下限値(lower)・上限値(upper)が入るが、
typeによって値の種類が変化します。numericalの場合:intまたはfloat- 例:
15.3ポイントの場合は下限・上限ともに15.3 - 例:
1~2人の場合は下限が1、上限が2
- 例:
abstimeまたはdurationの場合:Dict[str, int | float]- 例:
2021年1月1日の場合は下限・上限ともに{"year": 2021, "month": 1, "day": 1}(hour,minute,secondは該当する情報がないのでinfまたは-infになります) - 例:
3/3~3/5の場合は下限が{"month": 3, "day": 3}、上限が{"month": 3, "day": 5}(year,hour,minute,secondは該当する情報がないのでinfまたは-infになります) - 例:
100年間の場合は下限・上限ともに{"year": 100}(month,day,hour,minute,secondは該当する情報がないのでinfまたは-infになります)
- 例:
reltimeの場合:None
- ※2:
typeがreltimeの場合に絶対時間表現の下限値(lower)・上限値(upper)が入ります。(その他のtypeの場合はNoneになります)- 例:
昨年3月の場合は下限・上限ともに{"month": 3}(year,day,hour,minute,secondは該当する情報がないのでinfまたは-infになります)
- 例:
- ※3:
typeがreltimeの場合に相対時間表現の下限値(lower)・上限値(upper)が入ります。(その他のtypeの場合はNoneになります)- 例:
昨年3月の場合は下限・上限ともに{"year": -1}(month,day,hour,minute,secondは該当する情報がないのでinfまたは-infになります) - 例:
15年前の場合下限・上限ともに{"year": -15}(month,day,hour,minute,secondは該当する情報がないのでinfまたは-infになります)
- 例:
- ※1:数量・時間表現の下限値(lower)・上限値(upper)が入るが、
抽出する数値表現パターンを追加したい場合
予め用意した辞書に定義されていないパターンの数値表現を抽出したい場合、カスタム辞書ファイルを別途定義して読み込ませることができます。
任意のディレクトリにJSON形式の辞書ファイル(以下の例では/path/to/custom_dict.json)を定義し、NormalizeNumexpクラスの第2引数に辞書ファイルのパスを指定することで反映することができます。
辞書の作り方についてはこちらを参照してください。
from pynormalizenumexp.normalize_numexp import NormalizeNumexp
normalizer = NormalizeNumexp("ja", "/path/to/custom_dict.json")
results = normalizer.normalize("メールに2ファイル添付する", as_dict=True)
免責事項
- 本ライブラリの作成にあたり、単体テスト等で動作確認はしていますが、ケースによっては期待通りの振る舞いをしない可能性があります
- 本ライブラリの利用により、万一、利用者に何らかの不都合や損害が発生したとしても、作者は何らの責任を負うものではありません
Special thanks
- 東北大学 乾・鈴木研究室
- nullnull/normalizeNumexp(本家実装)
- cotogoto/normalize-numexp(Java移植版)
Metadata
Release files for pynormalizenumexp 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pynormalizenumexp-0.3.4.tar.gz | 99.3 kB | Without provenance | Details for pynormalizenumexp-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pynormalizenumexp-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for pynormalizenumexp-0.3.4-py3-none-any.whl |
Total release size: 223.2 kB
Release files / pynormalizenumexp-0.3.4.tar.gz
| Download URL | pynormalizenumexp-0.3.4.tar.gz |
|---|---|
| Size | 99.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d313f9410b2714c6db8b9d0f06e24b85e865589a923d8315e36f98d2834fd29
|
|
BLAKE2b-256 checksum How to use checksums |
b83b77db9985858cfdfe63cad88a79ca7cfc124ed4ffd665fee450cf37878378
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1018-azure
|
Release files / pynormalizenumexp-0.3.4-py3-none-any.whl
| Download URL | pynormalizenumexp-0.3.4-py3-none-any.whl |
|---|---|
| Size | 123.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dc144d4024329cb58dad333f361574fc175f1ff0f6028923ae666e98ff190a5
|
|
BLAKE2b-256 checksum How to use checksums |
70641eca8ff423ec805cf207e726184ae57222d9050144e213ceaaa2cc132cb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1018-azure
|
Security for pynormalizenumexp 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.