Skip to main content

PyPI version Python Versions pytest codecov

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になります)

抽出する数値表現パターンを追加したい場合

予め用意した辞書に定義されていないパターンの数値表現を抽出したい場合、カスタム辞書ファイルを別途定義して読み込ませることができます。

任意のディレクトリに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

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)

Source distribution for pynormalizenumexp 0.3.4
File Size Uploaded
pynormalizenumexp-0.3.4.tar.gz 99.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pynormalizenumexp 0.3.4
File Interpreter ABI Platform
pynormalizenumexp-0.3.4-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page