ndlocr-lite 2.0.0.dev0__tar.gz → 2.0.0.dev2__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.
- {ndlocr_lite-2.0.0.dev0/src/ndlocr_lite.egg-info → ndlocr_lite-2.0.0.dev2}/PKG-INFO +3 -3
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/README.md +2 -2
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/ndlocr-lite-gui/main.py +50 -15
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/pyproject.toml +3 -3
- ndlocr_lite-2.0.0.dev2/src/character_inclusion.py +63 -0
- ndlocr_lite-2.0.0.dev2/src/config/character_inclusion_mapping.csv +4906 -0
- ndlocr_lite-2.0.0.dev2/src/html_output.py +45 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2/src/ndlocr_lite.egg-info}/PKG-INFO +3 -3
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndlocr_lite.egg-info/SOURCES.txt +5 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndlocr_lite.egg-info/top_level.txt +3 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ocr.py +13 -3
- ndlocr_lite-2.0.0.dev2/src/tablecenternet.py +1946 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/version.py +1 -1
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/tests/test_cli_arguments.py +27 -0
- ndlocr_lite-2.0.0.dev2/tests/test_output_directory.py +74 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/tests/test_set_version.py +9 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/LICENCE +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/LICENCE_DEPENDENCEIES +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/MANIFEST.in +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/ndlocr-lite-gui/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/ndlocr-lite-gui/assets/icon.png +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/ndlocr-lite-gui/gui_config.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/requirements.txt +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/setup.cfg +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/setup.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/cli.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/config/NDLmoji.yaml +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/config/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/config/ndl.yaml +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/deim.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/model/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/model_download.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndl_parser.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndlocr_lite.egg-info/dependency_links.txt +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndlocr_lite.egg-info/entry_points.txt +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/ndlocr_lite.egg-info/requires.txt +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/optional_features/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/optional_features/table_structure.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/parseq.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/pdf_output_utils.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/parse_xml.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/reorder.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/smooth_order.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/warichu_block.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/order/wrap_dataset.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/utils/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/utils/file.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/utils/logger.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/utils/time.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/utils/xml.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/xy_cut/__init__.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/xy_cut/block_xy_cut.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/reading_order/xy_cut/eval.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/tcy_wrapper.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/tools/ndlkoten2tei.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/uicomponent/localelabel.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/src/uicomponent/textdaisy.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/tests/test_gui_command.py +0 -0
- {ndlocr_lite-2.0.0.dev0 → ndlocr_lite-2.0.0.dev2}/tests/test_model_download.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ndlocr-lite
|
|
3
|
-
Version: 2.0.0.
|
|
3
|
+
Version: 2.0.0.dev2
|
|
4
4
|
Summary: NDLOCR-Lite CLI Application
|
|
5
5
|
Requires-Python: >=3.11
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -112,7 +112,7 @@ uv tool install --prerelease allow ndlocr-lite
|
|
|
112
112
|
|
|
113
113
|
pip/uvでインストールする配布物にはONNXモデルを含めません。`ndlocr-lite`の初回OCR実行時に、未指定のモデルを`https://lab.ndl.go.jp/dataset/ndlocr-lite/onnx/v2/`から取得し、SHA-256を検証してユーザーのキャッシュに保存します。初回はネット接続と約157 MBの空き容量が必要です。以降はキャッシュを再利用します。`--help`と`--version`ではダウンロードしません。
|
|
114
114
|
|
|
115
|
-
キャッシュ先はWindowsでは`%LOCALAPPDATA%\ndlocr-lite\models\v2`、macOSでは`~/Library/Caches/ndlocr-lite/models/v2`、Linuxでは`${XDG_CACHE_HOME:-~/.cache}/ndlocr-lite/models/v2`です。各`--*-weights
|
|
115
|
+
キャッシュ先はWindowsでは`%LOCALAPPDATA%\ndlocr-lite\models\v2`、macOSでは`~/Library/Caches/ndlocr-lite/models/v2`、Linuxでは`${XDG_CACHE_HOME:-~/.cache}/ndlocr-lite/models/v2`です。各`--*-weights`オプションで指定したファイルはそのまま使用します。`ndlocr-lite gui`で起動するGUIも同じキャッシュを使用し、表構造推定モデル(約28 MB)も初回に取得します。配布済みのデスクトップGUI版と`python ocr.py`は従来どおり同梱モデルを使用し、ダウンロードしません。
|
|
116
116
|
|
|
117
117
|
### パラメータの説明
|
|
118
118
|
|
|
@@ -126,7 +126,7 @@ pip/uvでインストールする配布物にはONNXモデルを含めません
|
|
|
126
126
|
|
|
127
127
|
#### `--output`オプション
|
|
128
128
|
|
|
129
|
-
OCR
|
|
129
|
+
OCR結果を保存する出力先ディレクトリを相対パスまたは絶対パスで指定する。存在しない場合は、親ディレクトリを含めて新規作成する。権限不足などで作成できない場合は、エラーを表示して終了する。
|
|
130
130
|
|
|
131
131
|
#### `--viz`オプション
|
|
132
132
|
|
|
@@ -83,7 +83,7 @@ uv tool install --prerelease allow ndlocr-lite
|
|
|
83
83
|
|
|
84
84
|
pip/uvでインストールする配布物にはONNXモデルを含めません。`ndlocr-lite`の初回OCR実行時に、未指定のモデルを`https://lab.ndl.go.jp/dataset/ndlocr-lite/onnx/v2/`から取得し、SHA-256を検証してユーザーのキャッシュに保存します。初回はネット接続と約157 MBの空き容量が必要です。以降はキャッシュを再利用します。`--help`と`--version`ではダウンロードしません。
|
|
85
85
|
|
|
86
|
-
キャッシュ先はWindowsでは`%LOCALAPPDATA%\ndlocr-lite\models\v2`、macOSでは`~/Library/Caches/ndlocr-lite/models/v2`、Linuxでは`${XDG_CACHE_HOME:-~/.cache}/ndlocr-lite/models/v2`です。各`--*-weights
|
|
86
|
+
キャッシュ先はWindowsでは`%LOCALAPPDATA%\ndlocr-lite\models\v2`、macOSでは`~/Library/Caches/ndlocr-lite/models/v2`、Linuxでは`${XDG_CACHE_HOME:-~/.cache}/ndlocr-lite/models/v2`です。各`--*-weights`オプションで指定したファイルはそのまま使用します。`ndlocr-lite gui`で起動するGUIも同じキャッシュを使用し、表構造推定モデル(約28 MB)も初回に取得します。配布済みのデスクトップGUI版と`python ocr.py`は従来どおり同梱モデルを使用し、ダウンロードしません。
|
|
87
87
|
|
|
88
88
|
### パラメータの説明
|
|
89
89
|
|
|
@@ -97,7 +97,7 @@ pip/uvでインストールする配布物にはONNXモデルを含めません
|
|
|
97
97
|
|
|
98
98
|
#### `--output`オプション
|
|
99
99
|
|
|
100
|
-
OCR
|
|
100
|
+
OCR結果を保存する出力先ディレクトリを相対パスまたは絶対パスで指定する。存在しない場合は、親ディレクトリを含めて新規作成する。権限不足などで作成できない場合は、エラーを表示して終了する。
|
|
101
101
|
|
|
102
102
|
#### `--viz`オプション
|
|
103
103
|
|
|
@@ -10,12 +10,17 @@ from pathlib import Path
|
|
|
10
10
|
import threading
|
|
11
11
|
|
|
12
12
|
APP_DIR = Path(__file__).resolve().parent
|
|
13
|
-
|
|
14
|
-
if not SOURCE_DIR.is_dir():
|
|
15
|
-
# Source checkout: CI/builds copy src beside main.py, while local development
|
|
16
|
-
# uses the repository's top-level src directory directly.
|
|
13
|
+
if __package__:
|
|
17
14
|
SOURCE_DIR = APP_DIR.parent / 'src'
|
|
18
|
-
|
|
15
|
+
if not SOURCE_DIR.is_dir():
|
|
16
|
+
# Wheels install the OCR modules and config beside the GUI package.
|
|
17
|
+
SOURCE_DIR = APP_DIR.parent
|
|
18
|
+
else:
|
|
19
|
+
SOURCE_DIR = APP_DIR / 'src'
|
|
20
|
+
if not SOURCE_DIR.is_dir():
|
|
21
|
+
SOURCE_DIR = APP_DIR.parent / 'src'
|
|
22
|
+
if str(SOURCE_DIR) not in sys.path:
|
|
23
|
+
sys.path.append(str(SOURCE_DIR))
|
|
19
24
|
|
|
20
25
|
import xml.etree.ElementTree as ET
|
|
21
26
|
import time
|
|
@@ -28,7 +33,10 @@ import io
|
|
|
28
33
|
import glob
|
|
29
34
|
import ctypes
|
|
30
35
|
from io import BytesIO
|
|
31
|
-
|
|
36
|
+
if __package__:
|
|
37
|
+
from .gui_config import build_default_config
|
|
38
|
+
else:
|
|
39
|
+
from gui_config import build_default_config
|
|
32
40
|
from uicomponent.localelabel import TRANSLATIONS
|
|
33
41
|
from uicomponent.textdaisy import TextDaisyExporter
|
|
34
42
|
from collections import Counter
|
|
@@ -318,8 +326,12 @@ def process_cascade(alllineobj: RecogLine, recognizer30, recognizer50, recognize
|
|
|
318
326
|
return [t.pred_str for t in targetdflistall]
|
|
319
327
|
|
|
320
328
|
|
|
321
|
-
def load_ocr_models(args):
|
|
329
|
+
def load_ocr_models(args, download_models=False):
|
|
322
330
|
"""Create the four independent ONNX sessions concurrently."""
|
|
331
|
+
if download_models:
|
|
332
|
+
from model_download import resolve_model_paths
|
|
333
|
+
|
|
334
|
+
resolve_model_paths(args, SOURCE_DIR / 'model')
|
|
323
335
|
load_runtime_modules()
|
|
324
336
|
if hasattr(ocr, '_load_charlist'):
|
|
325
337
|
ocr._load_charlist(os.path.abspath(args.rec_classes))
|
|
@@ -336,6 +348,16 @@ def load_ocr_models(args):
|
|
|
336
348
|
)
|
|
337
349
|
|
|
338
350
|
|
|
351
|
+
def load_table_recognizer(args, download_models=False):
|
|
352
|
+
if download_models and args.table_weights is None:
|
|
353
|
+
from model_download import get_model_path, TABLE_MODEL
|
|
354
|
+
|
|
355
|
+
args.table_weights = str(get_model_path(*TABLE_MODEL, SOURCE_DIR / 'model'))
|
|
356
|
+
recognizer = TABLE_STRUCTURE_FEATURE.create_recognizer(args)
|
|
357
|
+
recognizer.load()
|
|
358
|
+
return recognizer
|
|
359
|
+
|
|
360
|
+
|
|
339
361
|
def load_rgb_image_array(input_path: str) -> 'np.ndarray':
|
|
340
362
|
load_runtime_modules()
|
|
341
363
|
with Image.open(input_path) as pil_image:
|
|
@@ -1024,7 +1046,7 @@ class CaptureTool:
|
|
|
1024
1046
|
self.page.pop_dialog()
|
|
1025
1047
|
self.page.show_dialog(self.dialog)
|
|
1026
1048
|
|
|
1027
|
-
def
|
|
1049
|
+
def parse_gui_args(argv=None, download_models=False):
|
|
1028
1050
|
parser = argparse.ArgumentParser(description='Argument for Inference using ONNXRuntime')
|
|
1029
1051
|
parser.add_argument('--det-weights', type=str, required=False, help='Path to rtmdet onnx file', default=str(SOURCE_DIR / 'model' / 'deim-s-1024x1024-261124.onnx'))
|
|
1030
1052
|
parser.add_argument('--det-classes', type=str, required=False, help='Path to list of class in yaml file', default=str(SOURCE_DIR / 'config' / 'ndl.yaml'))
|
|
@@ -1040,10 +1062,21 @@ def main(page: ft.Page):
|
|
|
1040
1062
|
parser.add_argument('--device', type=str, required=False, help='Device use (cpu or cuda)', choices=['cpu', 'cuda'], default='cpu')
|
|
1041
1063
|
if TABLE_STRUCTURE_FEATURE is not None:
|
|
1042
1064
|
TABLE_STRUCTURE_FEATURE.add_arguments(parser, SOURCE_DIR)
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
args, _ = parser.parse_known_args()
|
|
1065
|
+
if download_models:
|
|
1066
|
+
from model_download import MODELS
|
|
1046
1067
|
|
|
1068
|
+
parser.set_defaults(**{option: None for option in MODELS})
|
|
1069
|
+
if TABLE_STRUCTURE_FEATURE is not None:
|
|
1070
|
+
parser.set_defaults(table_weights=None)
|
|
1071
|
+
return parser.parse_args(argv)
|
|
1072
|
+
# Standalone Flet builds may append their own host arguments.
|
|
1073
|
+
args, _ = parser.parse_known_args(argv)
|
|
1074
|
+
return args
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
def main(page: ft.Page, args=None, download_models=False):
|
|
1078
|
+
if args is None:
|
|
1079
|
+
args = parse_gui_args(download_models=download_models)
|
|
1047
1080
|
page.title = 'NDLOCR-Lite-GUI v{}'.format(__version__)
|
|
1048
1081
|
page.theme_mode = ft.ThemeMode.SYSTEM
|
|
1049
1082
|
page.window.icon = str(ASSETS_DIR / 'icon.png')
|
|
@@ -2618,14 +2651,16 @@ def main(page: ft.Page):
|
|
|
2618
2651
|
origin_recognizer,
|
|
2619
2652
|
origin_recognizer30,
|
|
2620
2653
|
origin_recognizer50,
|
|
2621
|
-
) = await asyncio.to_thread(
|
|
2654
|
+
) = await asyncio.to_thread(
|
|
2655
|
+
load_ocr_models, args, download_models=download_models
|
|
2656
|
+
)
|
|
2622
2657
|
origin_table_recognizer = None
|
|
2623
2658
|
model_state['table_error'] = None
|
|
2624
2659
|
if TABLE_STRUCTURE_FEATURE is not None:
|
|
2625
2660
|
try:
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2661
|
+
origin_table_recognizer = await asyncio.to_thread(
|
|
2662
|
+
load_table_recognizer, args, download_models=download_models
|
|
2663
|
+
)
|
|
2629
2664
|
except Exception as table_ex:
|
|
2630
2665
|
model_state['table_error'] = table_ex
|
|
2631
2666
|
print(f'[WARN] Table structure initialization failed: {table_ex}')
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "ndlocr-lite"
|
|
7
|
-
version = "2.0.0.
|
|
7
|
+
version = "2.0.0.dev2"
|
|
8
8
|
description = "NDLOCR-Lite CLI Application"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -17,14 +17,14 @@ dependencies = { file = ["requirements.txt"] }
|
|
|
17
17
|
ndlocr-lite = "cli:main"
|
|
18
18
|
|
|
19
19
|
[tool.setuptools]
|
|
20
|
-
py-modules = ["cli", "model_download", "ocr", "deim", "parseq", "ndl_parser", "tcy_wrapper","pdf_output_utils","version"]
|
|
20
|
+
py-modules = ["cli", "model_download", "ocr", "deim", "parseq", "ndl_parser", "tcy_wrapper", "pdf_output_utils", "version", "character_inclusion", "html_output", "tablecenternet"]
|
|
21
21
|
|
|
22
22
|
[tool.setuptools.package-dir]
|
|
23
23
|
"" = "src"
|
|
24
24
|
ndlocr_lite_gui = "ndlocr-lite-gui"
|
|
25
25
|
|
|
26
26
|
[tool.setuptools.package-data]
|
|
27
|
-
"config" = ["*.yaml"]
|
|
27
|
+
"config" = ["*.yaml", "*.csv"]
|
|
28
28
|
"ndlocr_lite_gui" = ["assets/*"]
|
|
29
29
|
|
|
30
30
|
[tool.setuptools.exclude-package-data]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
import re
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
MAPPING_SOURCE_URL = (
|
|
7
|
+
"https://github.com/ndl-lab/tugidigi-web/blob/master/"
|
|
8
|
+
"src/main/resources/config/index/jd_book.json"
|
|
9
|
+
)
|
|
10
|
+
MAPPING_PATH = Path(__file__).resolve().parent / "config" / "character_inclusion_mapping.csv"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _load_mapping_tables():
|
|
14
|
+
single_character_mappings = {}
|
|
15
|
+
multi_character_mappings = {}
|
|
16
|
+
|
|
17
|
+
with MAPPING_PATH.open("r", encoding="utf-8", newline="") as stream:
|
|
18
|
+
reader = csv.DictReader(stream)
|
|
19
|
+
expected_fields = {
|
|
20
|
+
"source",
|
|
21
|
+
"target",
|
|
22
|
+
"source_codepoints",
|
|
23
|
+
"target_codepoints",
|
|
24
|
+
}
|
|
25
|
+
if set(reader.fieldnames or ()) != expected_fields:
|
|
26
|
+
raise ValueError(f"Unexpected inclusion mapping columns: {reader.fieldnames}")
|
|
27
|
+
|
|
28
|
+
for row in reader:
|
|
29
|
+
source = row["source"]
|
|
30
|
+
target = row["target"]
|
|
31
|
+
if not source:
|
|
32
|
+
raise ValueError("Inclusion mapping sources must not be empty")
|
|
33
|
+
if source in single_character_mappings or source in multi_character_mappings:
|
|
34
|
+
raise ValueError(f"Duplicate inclusion mapping source: {source!r}")
|
|
35
|
+
if len(source) == 1:
|
|
36
|
+
single_character_mappings[source] = target
|
|
37
|
+
else:
|
|
38
|
+
multi_character_mappings[source] = target
|
|
39
|
+
|
|
40
|
+
translation_table = str.maketrans(single_character_mappings)
|
|
41
|
+
multi_pattern = None
|
|
42
|
+
if multi_character_mappings:
|
|
43
|
+
sources = sorted(multi_character_mappings, key=len, reverse=True)
|
|
44
|
+
multi_pattern = re.compile("|".join(re.escape(source) for source in sources))
|
|
45
|
+
return translation_table, multi_character_mappings, multi_pattern
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
_TRANSLATION_TABLE, _MULTI_CHARACTER_MAPPINGS, _MULTI_CHARACTER_PATTERN = (
|
|
49
|
+
_load_mapping_tables()
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _replace_multi_character_mapping(match):
|
|
54
|
+
return _MULTI_CHARACTER_MAPPINGS[match.group(0)]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def apply_character_inclusion(text: str) -> str:
|
|
58
|
+
"""Apply the jd_book mapping char filter with longest matches first."""
|
|
59
|
+
if not text:
|
|
60
|
+
return text
|
|
61
|
+
if _MULTI_CHARACTER_PATTERN is not None:
|
|
62
|
+
text = _MULTI_CHARACTER_PATTERN.sub(_replace_multi_character_mapping, text)
|
|
63
|
+
return text.translate(_TRANSLATION_TABLE)
|