ocr-stringdist 0.0.4__cp313-cp313-win32.whl → 0.0.6__cp313-cp313-win32.whl

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.
@@ -6,7 +6,8 @@ from .matching import find_best_candidate
6
6
 
7
7
  __all__ = [
8
8
  "ocr_distance_map",
9
- "weighted_levenshtein_distance", # noqa: F405
9
+ "weighted_levenshtein_distance",
10
+ "batch_weighted_levenshtein_distance",
10
11
  "find_best_candidate",
11
12
  ]
12
13
 
@@ -19,23 +20,79 @@ def weighted_levenshtein_distance(
19
20
  *,
20
21
  symmetric: bool = True,
21
22
  default_cost: float = 1.0,
23
+ max_token_characters: int = 1,
22
24
  ) -> float:
23
25
  """
24
26
  Levenshtein distance with custom substitution costs.
25
27
  Insertion/deletion costs are 1.
26
28
 
27
- The default `cost_map` considers common OCR errors, see `ocr_stringdist.ocr_distance_map`.
29
+ The default `cost_map` considers common OCR errors, see
30
+ :py:data:`ocr_stringdist.default_ocr_distances.ocr_distance_map`.
28
31
 
29
32
  :param s1: First string
30
33
  :param s2: Second string
31
- :param cost_map: Dictionary mapping tuples of characters to their substitution cost.
34
+ :param cost_map: Dictionary mapping tuples of strings ("substitution tokens") to their
35
+ substitution costs.
32
36
  Only one direction needs to be configured unless `symmetric` is False.
37
+ Note that you need to set `max_token_characters` if the substitution tokens
38
+ have more than one character, for example when substituting "w" for "vv".
33
39
  Defaults to `ocr_stringdist.ocr_distance_map`.
34
40
  :param symmetric: Should the keys of `cost_map` be considered to be symmetric? Defaults to True.
35
41
  :param default_cost: The default substitution cost for character pairs not found in `cost_map`.
42
+ :param max_token_characters: A positive integer, indicating the maximum number of characters a
43
+ substitution token in `cost_map` may have. The default 1 indicates
44
+ that only single characters can be substituted for each other.
45
+ Higher values lead to slower calculations.
36
46
  """
37
47
  if cost_map is None:
38
48
  cost_map = ocr_distance_map
49
+ # _weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
39
50
  return _weighted_levenshtein_distance( # type: ignore # noqa: F405
40
- s1, s2, cost_map=cost_map, symmetric=symmetric, default_cost=default_cost
51
+ s1,
52
+ s2,
53
+ cost_map=cost_map,
54
+ symmetric=symmetric,
55
+ default_cost=default_cost,
56
+ max_token_characters=max_token_characters,
57
+ )
58
+
59
+
60
+ def batch_weighted_levenshtein_distance(
61
+ s: str,
62
+ candidates: list[str],
63
+ /,
64
+ cost_map: Optional[dict[tuple[str, str], float]] = None,
65
+ *,
66
+ symmetric: bool = True,
67
+ default_cost: float = 1.0,
68
+ max_token_characters: int = 1,
69
+ ) -> list[float]:
70
+ """
71
+ Calculate weighted Levenshtein distances between a string and multiple candidates.
72
+
73
+ This is more efficient than calling :func:`weighted_levenshtein_distance` multiple times.
74
+
75
+ :param s: The string to compare
76
+ :param candidates: List of candidate strings to compare against
77
+ :param cost_map: Dictionary mapping tuples of characters to their substitution cost.
78
+ Only one direction needs to be configured unless `symmetric` is False.
79
+ Defaults to `ocr_stringdist.ocr_distance_map`.
80
+ :param symmetric: Should the keys of `cost_map` be considered to be symmetric? Defaults to True.
81
+ :param default_cost: The default substitution cost for character pairs not found in `cost_map`.
82
+ :param max_token_characters: A positive integer, indicating the maximum number of characters a
83
+ substitution token in `cost_map` may have. The default 1 indicates
84
+ that only single characters can be substituted for each other.
85
+ Higher values lead to slower calculations.
86
+ :return: A list of distances corresponding to each candidate
87
+ """
88
+ if cost_map is None:
89
+ cost_map = ocr_distance_map
90
+ # _batch_weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
91
+ return _batch_weighted_levenshtein_distance( # type: ignore # noqa: F405
92
+ s,
93
+ candidates,
94
+ cost_map=cost_map,
95
+ symmetric=symmetric,
96
+ default_cost=default_cost,
97
+ max_token_characters=max_token_characters,
41
98
  )
@@ -1,3 +1,5 @@
1
+ # Start marker for literalinclude, see docs/source/api/index.rst.
2
+ # OCR_DISTANCE_MAP_START
1
3
  ocr_distance_map: dict[tuple[str, str], float] = {
2
4
  ("O", "0"): 0.1,
3
5
  ("l", "1"): 0.1,
@@ -31,6 +33,8 @@ ocr_distance_map: dict[tuple[str, str], float] = {
31
33
  ("é", "á"): 0.7,
32
34
  ("E", "F"): 0.8,
33
35
  }
36
+ # OCR_DISTANCE_MAP_END
37
+ # End marker for literalinclude
34
38
  """
35
39
  Pre-defined distance map between characters, considering common OCR errors.
36
40
  The distances are between 0 and 1.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocr_stringdist
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python
6
6
  Classifier: Operating System :: OS Independent
@@ -17,6 +17,8 @@ Project-URL: repository, https://github.com/NiklasvonM/ocr-stringdist
17
17
 
18
18
  A Python library for string distance calculations that account for common OCR (optical character recognition) errors.
19
19
 
20
+ Documentation: https://niklasvonm.github.io/ocr-stringdist/
21
+
20
22
  [![PyPI](https://img.shields.io/badge/PyPI-Package-blue)](https://pypi.org/project/ocr-stringdist/)
21
23
  [![License](https://img.shields.io/badge/License-MIT-green)](LICENSE)
22
24
 
@@ -34,10 +36,12 @@ pip install ocr-stringdist
34
36
 
35
37
  ## Features
36
38
 
37
- - **Weighted Levenshtein Distance**: An adaptation of the classic Levenshtein algorithm with custom substitution costs for character pairs that are commonly confused in OCR models.
39
+ - **Weighted Levenshtein Distance**: An adaptation of the classic Levenshtein algorithm with custom substitution costs for character pairs that are commonly confused in OCR models, including efficient batch processing.
40
+ - **Unicode Support**: Arbitrary unicode strings can be compared.
41
+ - **Substitution of Multiple Characters**: Not just character pairs, but string pairs may be substituted, for example the Korean syllable "이" for the two letters "OI".
38
42
  - **Pre-defined OCR Distance Map**: A built-in distance map for common OCR confusions (e.g., "0" vs "O", "1" vs "l", "5" vs "S").
39
43
  - **Customizable Cost Maps**: Create your own substitution cost maps for specific OCR systems or domains.
40
- - **Best Match Finder**: Utility function find_best_candidate to efficiently find the best matching string from a collection of candidates using any specified distance function (including the library's OCR-aware ones). Supports early stopping for performance optimization.
44
+ - **Best Match Finder**: Utility function `find_best_candidate` to efficiently find the best matching string from a collection of candidates using any specified distance function (including the library's OCR-aware ones).
41
45
 
42
46
  ## Usage
43
47
 
@@ -51,12 +55,12 @@ distance = osd.weighted_levenshtein_distance("OCR5", "OCRS")
51
55
  print(f"Distance between 'OCR5' and 'OCRS': {distance}") # Will be less than 1.0
52
56
 
53
57
  # Custom cost map
54
- custom_map = {("f", "t"): 0.2, ("m", "n"): 0.1}
58
+ custom_map = {("In", "h"): 0.5}
55
59
  distance = osd.weighted_levenshtein_distance(
56
- "first", "tirst",
60
+ "hi", "Ini",
57
61
  cost_map=custom_map,
58
62
  symmetric=True,
59
- default_cost=1.0
63
+ max_token_characters=2,
60
64
  )
61
65
  print(f"Distance with custom map: {distance}")
62
66
  ```
@@ -0,0 +1,9 @@
1
+ ocr_stringdist-0.0.6.dist-info/METADATA,sha256=c64RjWARPaiMAlVCUQrrCk2-pvgN1LjOMUOOHkg4FH4,3593
2
+ ocr_stringdist-0.0.6.dist-info/WHEEL,sha256=eh90R9THiv1HYPhYUCnpm_RAErMfEQKvZWVMxF3uaCM,92
3
+ ocr_stringdist-0.0.6.dist-info/licenses/LICENSE,sha256=3cNRiJag5vI0KMMDNf0oiaY4vg43rLxRszbMJs1GBoU,1092
4
+ ocr_stringdist/default_ocr_distances.py,sha256=vlhzQCCcE-D1xor5RvMW0oaMuL_HP_5Y7SO4ESkdb4w,1075
5
+ ocr_stringdist/matching.py,sha256=KEzYBBEHZhfLA9eD3MxDaehKiD9lUb0RQq74u5qWpVw,3376
6
+ ocr_stringdist/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ ocr_stringdist/__init__.py,sha256=scHB0ZWB5vh5FShJIubGJlh3Pl8YaQ5HwWYXfof3MGA,4270
8
+ ocr_stringdist/_rust_stringdist.cp313-win32.pyd,sha256=fxvB4CbQdRfa4CyooBCwS4blrPhf2STPLIMD34SG3ZM,303104
9
+ ocr_stringdist-0.0.6.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- ocr_stringdist-0.0.4.dist-info/METADATA,sha256=1lSh8HZ9TrBv0BWdBbMZk-_0qbE1tEEUd25xfAFnd7s,3320
2
- ocr_stringdist-0.0.4.dist-info/WHEEL,sha256=eh90R9THiv1HYPhYUCnpm_RAErMfEQKvZWVMxF3uaCM,92
3
- ocr_stringdist-0.0.4.dist-info/licenses/LICENSE,sha256=3cNRiJag5vI0KMMDNf0oiaY4vg43rLxRszbMJs1GBoU,1092
4
- ocr_stringdist/default_ocr_distances.py,sha256=STNRMGWEYOCHo11uP51JUQfvNrSZleMCxt6wsPkctfg,925
5
- ocr_stringdist/matching.py,sha256=KEzYBBEHZhfLA9eD3MxDaehKiD9lUb0RQq74u5qWpVw,3376
6
- ocr_stringdist/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- ocr_stringdist/__init__.py,sha256=Z6ZeTSfpKRaUM15FPW00MfLBKVDUCP21Xh5VLtnC4Tk,1471
8
- ocr_stringdist/_rust_stringdist.cp313-win32.pyd,sha256=_gG1gEjjgQmcDh0QaxCEI1OLMw5NrUngnvxckxjhl94,199680
9
- ocr_stringdist-0.0.4.dist-info/RECORD,,