ocr-stringdist 0.0.5__cp310-cp310-musllinux_1_1_i686.whl → 0.0.6__cp310-cp310-musllinux_1_1_i686.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,24 +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
39
49
  # _weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
40
50
  return _weighted_levenshtein_distance( # type: ignore # noqa: F405
41
- 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,
42
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.5
3
+ Version: 0.0.6
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python
6
6
  Classifier: Operating System :: OS Independent
@@ -36,7 +36,7 @@ pip install ocr-stringdist
36
36
 
37
37
  ## Features
38
38
 
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.
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
40
  - **Unicode Support**: Arbitrary unicode strings can be compared.
41
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".
42
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").
@@ -60,7 +60,7 @@ distance = osd.weighted_levenshtein_distance(
60
60
  "hi", "Ini",
61
61
  cost_map=custom_map,
62
62
  symmetric=True,
63
- default_cost=1.0,
63
+ max_token_characters=2,
64
64
  )
65
65
  print(f"Distance with custom map: {distance}")
66
66
  ```
@@ -0,0 +1,10 @@
1
+ ocr_stringdist-0.0.6.dist-info/METADATA,sha256=w_dnhka08_rjLZ3LxFzTqeaUuvUrzer7FkjrqQI2xIg,3522
2
+ ocr_stringdist-0.0.6.dist-info/WHEEL,sha256=Cl-l6-pHsPQyGZM6aZ9ZaqUOwfa3C4W3NzP4o7ok8tc,105
3
+ ocr_stringdist-0.0.6.dist-info/licenses/LICENSE,sha256=5BPRcjlnbl2t4TidSgpfGrtC_birSf8JlZfA-qmVoQE,1072
4
+ ocr_stringdist.libs/libgcc_s-b5472b99.so.1,sha256=wh8CpjXz9IccAyeERcB7YDEx7NH2jF-PykwOyYNeRRI,453841
5
+ ocr_stringdist/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ ocr_stringdist/__init__.py,sha256=45mZy8Gx3ygO60XAcWR8ZVS4LAoPcvKd946MwbM9-vc,4172
7
+ ocr_stringdist/default_ocr_distances.py,sha256=oSu-TpHjPA4jxKpLAfmap8z0ZsC99jsOjnRVHW7Hj_Y,1033
8
+ ocr_stringdist/matching.py,sha256=rr8R63Ttu2hTf5Mni7_P8aGBbjWs6t2QPV3wxKXspAs,3293
9
+ ocr_stringdist/_rust_stringdist.cpython-310-i386-linux-gnu.so,sha256=LVB2GInVHw9v-8q60F9yuSSyIyYofulBRhWObHn5X-4,715173
10
+ ocr_stringdist-0.0.6.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- ocr_stringdist-0.0.5.dist-info/METADATA,sha256=vX4O04CresnzPoQnQSnejENFPcgSK5XWqRewrBvl9BU,3478
2
- ocr_stringdist-0.0.5.dist-info/WHEEL,sha256=Cl-l6-pHsPQyGZM6aZ9ZaqUOwfa3C4W3NzP4o7ok8tc,105
3
- ocr_stringdist-0.0.5.dist-info/licenses/LICENSE,sha256=5BPRcjlnbl2t4TidSgpfGrtC_birSf8JlZfA-qmVoQE,1072
4
- ocr_stringdist.libs/libgcc_s-b5472b99.so.1,sha256=wh8CpjXz9IccAyeERcB7YDEx7NH2jF-PykwOyYNeRRI,453841
5
- ocr_stringdist/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- ocr_stringdist/__init__.py,sha256=zL-0Bmo6jCas2QlDCGxL2vPbjoNrdpLQ9kPfsAjC0QI,1515
7
- ocr_stringdist/default_ocr_distances.py,sha256=8jmR5aLrEfrm5Fj2-nEqdTCKEmoEcm8DxBWv7IQd5_k,887
8
- ocr_stringdist/matching.py,sha256=rr8R63Ttu2hTf5Mni7_P8aGBbjWs6t2QPV3wxKXspAs,3293
9
- ocr_stringdist/_rust_stringdist.cpython-310-i386-linux-gnu.so,sha256=ITXMBeDNzem7DUebJeAns2siEq8wg7jYRtr_U1D5iiU,559137
10
- ocr_stringdist-0.0.5.dist-info/RECORD,,