ocr-stringdist 0.0.6__pp39-pypy39_pp73-musllinux_1_1_i686.whl → 0.1.0__pp39-pypy39_pp73-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.
@@ -1,7 +1,5 @@
1
- from typing import Optional
2
-
3
- from ._rust_stringdist import * # noqa: F403
4
1
  from .default_ocr_distances import ocr_distance_map
2
+ from .levenshtein import batch_weighted_levenshtein_distance, weighted_levenshtein_distance
5
3
  from .matching import find_best_candidate
6
4
 
7
5
  __all__ = [
@@ -10,89 +8,3 @@ __all__ = [
10
8
  "batch_weighted_levenshtein_distance",
11
9
  "find_best_candidate",
12
10
  ]
13
-
14
-
15
- def weighted_levenshtein_distance(
16
- s1: str,
17
- s2: str,
18
- /,
19
- cost_map: Optional[dict[tuple[str, str], float]] = None,
20
- *,
21
- symmetric: bool = True,
22
- default_cost: float = 1.0,
23
- max_token_characters: int = 1,
24
- ) -> float:
25
- """
26
- Levenshtein distance with custom substitution costs.
27
- Insertion/deletion costs are 1.
28
-
29
- The default `cost_map` considers common OCR errors, see
30
- :py:data:`ocr_stringdist.default_ocr_distances.ocr_distance_map`.
31
-
32
- :param s1: First string
33
- :param s2: Second string
34
- :param cost_map: Dictionary mapping tuples of strings ("substitution tokens") to their
35
- substitution costs.
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".
39
- Defaults to `ocr_stringdist.ocr_distance_map`.
40
- :param symmetric: Should the keys of `cost_map` be considered to be symmetric? Defaults to True.
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.
46
- """
47
- if cost_map is None:
48
- cost_map = ocr_distance_map
49
- # _weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
50
- return _weighted_levenshtein_distance( # type: ignore # noqa: F405
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,
98
- )
@@ -0,0 +1,118 @@
1
+ from typing import Optional
2
+
3
+ from ._rust_stringdist import * # noqa: F403
4
+ from .default_ocr_distances import ocr_distance_map
5
+
6
+
7
+ def weighted_levenshtein_distance(
8
+ s1: str,
9
+ s2: str,
10
+ /,
11
+ substitution_costs: Optional[dict[tuple[str, str], float]] = None,
12
+ insertion_costs: Optional[dict[str, float]] = None,
13
+ deletion_costs: Optional[dict[str, float]] = None,
14
+ *,
15
+ symmetric_substitution: bool = True,
16
+ default_substitution_cost: float = 1.0,
17
+ default_insertion_cost: float = 1.0,
18
+ default_deletion_cost: float = 1.0,
19
+ ) -> float:
20
+ """
21
+ Levenshtein distance with custom substitution, insertion and deletion costs.
22
+
23
+ The default `substitution_costs` considers common OCR errors, see
24
+ :py:data:`ocr_stringdist.default_ocr_distances.ocr_distance_map`.
25
+
26
+ :param s1: First string (interpreted as the string read via OCR)
27
+ :param s2: Second string
28
+ :param substitution_costs: Dictionary mapping tuples of strings ("substitution tokens") to their
29
+ substitution costs. Only one direction needs to be configured unless
30
+ `symmetric_substitution` is False.
31
+ Note that the runtime scales in the length of the longest substitution token.
32
+ Defaults to `ocr_stringdist.ocr_distance_map`.
33
+ :param insertion_costs: Dictionary mapping strings to their insertion costs.
34
+ :param deletion_costs: Dictionary mapping strings to their deletion costs.
35
+ :param symmetric_substitution: Should the keys of `substitution_costs` be considered to be
36
+ symmetric? Defaults to True.
37
+ :param default_substitution_cost: The default substitution cost for character pairs not found
38
+ in `substitution_costs`.
39
+ :param default_insertion_cost: The default insertion cost for characters not found in
40
+ `insertion_costs`.
41
+ :param default_deletion_cost: The default deletion cost for characters not found in
42
+ `deletion_costs`.
43
+ """
44
+ if substitution_costs is None:
45
+ substitution_costs = ocr_distance_map
46
+ if insertion_costs is None:
47
+ insertion_costs = {}
48
+ if deletion_costs is None:
49
+ deletion_costs = {}
50
+ # _weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
51
+ return _weighted_levenshtein_distance( # type: ignore # noqa: F405
52
+ s1,
53
+ s2,
54
+ substitution_costs=substitution_costs,
55
+ insertion_costs=insertion_costs,
56
+ deletion_costs=deletion_costs,
57
+ symmetric_substitution=symmetric_substitution,
58
+ default_substitution_cost=default_substitution_cost,
59
+ default_insertion_cost=default_insertion_cost,
60
+ default_deletion_cost=default_deletion_cost,
61
+ )
62
+
63
+
64
+ def batch_weighted_levenshtein_distance(
65
+ s: str,
66
+ candidates: list[str],
67
+ /,
68
+ substitution_costs: Optional[dict[tuple[str, str], float]] = None,
69
+ insertion_costs: Optional[dict[str, float]] = None,
70
+ deletion_costs: Optional[dict[str, float]] = None,
71
+ *,
72
+ symmetric_substitution: bool = True,
73
+ default_substitution_cost: float = 1.0,
74
+ default_insertion_cost: float = 1.0,
75
+ default_deletion_cost: float = 1.0,
76
+ ) -> list[float]:
77
+ """
78
+ Calculate weighted Levenshtein distances between a string and multiple candidates.
79
+
80
+ This is more efficient than calling :func:`weighted_levenshtein_distance` multiple times.
81
+
82
+ :param s: The string to compare (interpreted as the string read via OCR)
83
+ :param candidates: List of candidate strings to compare against
84
+ :param substitution_costs: Dictionary mapping tuples of strings ("substitution tokens") to their
85
+ substitution costs. Only one direction needs to be configured unless
86
+ `symmetric_substitution` is False.
87
+ Note that the runtime scales in the length of the longest substitution token.
88
+ Defaults to `ocr_stringdist.ocr_distance_map`.
89
+ :param insertion_costs: Dictionary mapping strings to their insertion costs.
90
+ :param deletion_costs: Dictionary mapping strings to their deletion costs.
91
+ :param symmetric_substitution: Should the keys of `substitution_costs` be considered to be
92
+ symmetric? Defaults to True.
93
+ :param default_substitution_cost: The default substitution cost for character pairs not found
94
+ in `substitution_costs`.
95
+ :param default_insertion_cost: The default insertion cost for characters not found in
96
+ `insertion_costs`.
97
+ :param default_deletion_cost: The default deletion cost for characters not found in
98
+ `deletion_costs`.
99
+ :return: A list of distances corresponding to each candidate
100
+ """
101
+ if substitution_costs is None:
102
+ substitution_costs = ocr_distance_map
103
+ if insertion_costs is None:
104
+ insertion_costs = {}
105
+ if deletion_costs is None:
106
+ deletion_costs = {}
107
+ # _batch_weighted_levenshtein_distance is written in Rust, see src/rust_stringdist.rs.
108
+ return _batch_weighted_levenshtein_distance( # type: ignore # noqa: F405
109
+ s,
110
+ candidates,
111
+ substitution_costs=substitution_costs,
112
+ insertion_costs=insertion_costs,
113
+ deletion_costs=deletion_costs,
114
+ symmetric_substitution=symmetric_substitution,
115
+ default_substitution_cost=default_substitution_cost,
116
+ default_insertion_cost=default_insertion_cost,
117
+ default_deletion_cost=default_deletion_cost,
118
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocr_stringdist
3
- Version: 0.0.6
3
+ Version: 0.1.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python
6
6
  Classifier: Operating System :: OS Independent
@@ -40,7 +40,6 @@ pip install ocr-stringdist
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").
43
- - **Customizable Cost Maps**: Create your own substitution cost maps for specific OCR systems or domains.
44
43
  - **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).
45
44
 
46
45
  ## Usage
@@ -60,7 +59,6 @@ distance = osd.weighted_levenshtein_distance(
60
59
  "hi", "Ini",
61
60
  cost_map=custom_map,
62
61
  symmetric=True,
63
- max_token_characters=2,
64
62
  )
65
63
  print(f"Distance with custom map: {distance}")
66
64
  ```
@@ -1,10 +1,11 @@
1
- ocr_stringdist-0.0.6.dist-info/METADATA,sha256=w_dnhka08_rjLZ3LxFzTqeaUuvUrzer7FkjrqQI2xIg,3522
2
- ocr_stringdist-0.0.6.dist-info/WHEEL,sha256=y2ykRG-JhRC0QLiL4PpP__i-kFKqYhLnbv5PlhVhO9k,110
3
- ocr_stringdist-0.0.6.dist-info/licenses/LICENSE,sha256=5BPRcjlnbl2t4TidSgpfGrtC_birSf8JlZfA-qmVoQE,1072
1
+ ocr_stringdist-0.1.0.dist-info/METADATA,sha256=vQJRhn0AEoIm159PyKQGMmH7JW35ZWUrQ-PWEBRteg8,3388
2
+ ocr_stringdist-0.1.0.dist-info/WHEEL,sha256=y2ykRG-JhRC0QLiL4PpP__i-kFKqYhLnbv5PlhVhO9k,110
3
+ ocr_stringdist-0.1.0.dist-info/licenses/LICENSE,sha256=5BPRcjlnbl2t4TidSgpfGrtC_birSf8JlZfA-qmVoQE,1072
4
4
  ocr_stringdist.libs/libgcc_s-b5472b99.so.1,sha256=wh8CpjXz9IccAyeERcB7YDEx7NH2jF-PykwOyYNeRRI,453841
5
5
  ocr_stringdist/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- ocr_stringdist/__init__.py,sha256=45mZy8Gx3ygO60XAcWR8ZVS4LAoPcvKd946MwbM9-vc,4172
7
6
  ocr_stringdist/default_ocr_distances.py,sha256=oSu-TpHjPA4jxKpLAfmap8z0ZsC99jsOjnRVHW7Hj_Y,1033
7
+ ocr_stringdist/levenshtein.py,sha256=hHcarxjOzxhuopNNW3ZkPIcrSx2NjXYUWh9rR59W0Tc,5627
8
8
  ocr_stringdist/matching.py,sha256=rr8R63Ttu2hTf5Mni7_P8aGBbjWs6t2QPV3wxKXspAs,3293
9
- ocr_stringdist/_rust_stringdist.pypy39-pp73-x86-linux-gnu.so,sha256=hOTg1-erJegE04aTxWpBYw8FNJlA6cVqnBgXbVyz8YU,715337
10
- ocr_stringdist-0.0.6.dist-info/RECORD,,
9
+ ocr_stringdist/__init__.py,sha256=haj_CNOovN9O6j1ixmku6BNTV4U3NQ5JEN2dC_4TmXc,332
10
+ ocr_stringdist/_rust_stringdist.pypy39-pp73-x86-linux-gnu.so,sha256=UlzjaqBJzxwWOWX9PmpSmT7ZjwGhoyyQr4TAOLWUUdU,752133
11
+ ocr_stringdist-0.1.0.dist-info/RECORD,,