hdx-python-utilities 4.2.0__py3-none-any.whl → 4.2.2__py3-none-any.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.
hdx/utilities/_version.py CHANGED
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '4.2.0'
22
- __version_tuple__ = version_tuple = (4, 2, 0)
21
+ __version__ = version = '4.2.2'
22
+ __version_tuple__ = version_tuple = (4, 2, 2)
23
23
 
24
24
  __commit_id__ = commit_id = None
hdx/utilities/matching.py CHANGED
@@ -2,7 +2,6 @@ import difflib
2
2
  import re
3
3
  from collections.abc import Callable, Sequence
4
4
 
5
- from pyphonetics import RefinedSoundex
6
5
  from rapidfuzz import fuzz as rapidfuzz_fuzz
7
6
 
8
7
  from hdx.utilities.text import normalise
@@ -13,64 +12,54 @@ LIST_SEPARATORS = re.compile(r"\s*(?:[,;/:&\n]|\s(?:and|et)\s)\s*", re.IGNORECAS
13
12
  HYPHEN = re.compile(r"\s*-\s*")
14
13
 
15
14
 
16
- class Phonetics(RefinedSoundex):
17
- def match(
18
- self,
19
- possible_names: Sequence,
20
- name: str,
21
- alternative_name: str | None = None,
22
- transform_possible_names: Sequence[Callable] = [],
23
- threshold: int = 2,
24
- ) -> int | None:
25
- """
26
- Match name to one of the given possible names. Returns None if no match
27
- or the index of the matching name
28
-
29
- Args:
30
- possible_names: Possible names
31
- name: Name to match
32
- alternative_name: Alternative name to match. Defaults to None.
33
- transform_possible_names: Functions to transform possible names.
34
- threshold: Match threshold. Defaults to 2.
35
-
36
- Returns:
37
- Index of matching name from possible names or None
38
- """
39
- mindistance = None
40
- matching_index = None
41
-
42
- transform_possible_names.insert(0, lambda x: x)
43
-
44
- def check_name(name, possible_name):
45
- nonlocal mindistance, matching_index # noqa: E999
15
+ def expand_initials(words: list[str], other_words: list[str]) -> list[str] | None:
16
+ """Check single letters at the start or end of words against
17
+ other_words. A leading letter is an initial and is replaced by an unused
18
+ word in other_words that is not in words and starts with it ("e shewa"
19
+ -> "east shewa", "b gumuz" -> "benishangul gumuz"). A trailing letter
20
+ distinguishes places ("region iv b") so must be in other_words. Single
21
+ letters between other words are left alone as they are often conjunctions
22
+ (eg. Spanish "y").
46
23
 
47
- distance = self.distance(name, possible_name)
48
- if mindistance is None or distance < mindistance:
49
- mindistance = distance
50
- matching_index = i
24
+ Args:
25
+ words: Words that may contain single letters
26
+ other_words: Words to expand single letters to
51
27
 
52
- for i, possible_name in enumerate(possible_names):
53
- for transform_possible_name in transform_possible_names:
54
- transformed_possible_name = transform_possible_name(possible_name)
55
- if not transformed_possible_name:
56
- continue
57
- check_name(name, transformed_possible_name)
58
- if alternative_name:
59
- check_name(alternative_name, transformed_possible_name)
60
- if mindistance is None or mindistance > threshold:
28
+ Returns:
29
+ Expanded words or None if a single letter has no counterpart
30
+ """
31
+ leading = []
32
+ for word in words:
33
+ if len(word) != 1:
34
+ break
35
+ leading.append(word)
36
+ trailing = []
37
+ for word in reversed(words[len(leading) :]):
38
+ if len(word) != 1:
39
+ break
40
+ trailing.append(word)
41
+ for letter in trailing:
42
+ if letter.isalpha() and letter not in other_words:
61
43
  return None
62
- return matching_index
44
+ unmatched = [word for word in other_words if word not in words and len(word) > 1]
45
+ expanded = list(words)
46
+ for i, letter in enumerate(leading):
47
+ if not letter.isalpha() or letter in other_words:
48
+ continue
49
+ for other in unmatched:
50
+ if other[0] == letter:
51
+ unmatched.remove(other)
52
+ expanded[i] = other
53
+ break
54
+ else:
55
+ return None
56
+ return expanded
63
57
 
64
58
 
65
59
  class RapidFuzzMatcher:
66
60
  """
67
- Default matcher for names, using rapidfuzz edit-distance scoring instead
68
- of phonetic (Soundex-family) encoding as Phonetics does. Phonetic algorithms
69
- encode English pronunciation rules, so they are a poor fit for matching
70
- multilingual or transliterated names (eg. "Sana'a" vs "Sanaa") that are
71
- spelling variants rather than being phonetically similar in English
72
- terms. Edit distance on normalised strings tends to place such variants
73
- closer together.
61
+ Matcher for names using rapidfuzz edit-distance scoring, which suits
62
+ multilingual or transliterated spelling variants (eg. "Sana'a" vs "Sanaa").
74
63
 
75
64
  Args:
76
65
  scorer: Function taking two strings and returning a similarity score
@@ -119,6 +108,11 @@ class RapidFuzzMatcher:
119
108
  distinguishes places, but not names like sectors, where extra words
120
109
  are often descriptive (eg. "food security and livelihood").
121
110
 
111
+ Single letters at the start or end of name are checked first with
112
+ expand_initials ("e shewa" scores 0 against "west shewa" but matches
113
+ "east shewa"), unless the names differ only in spaces and punctuation
114
+ (eg. "sana a" vs "sanaa").
115
+
122
116
  Args:
123
117
  name: Name to match
124
118
  possible_name: Possible name
@@ -126,6 +120,15 @@ class RapidFuzzMatcher:
126
120
  Returns:
127
121
  Similarity score from 0-100
128
122
  """
123
+ joined_score = rapidfuzz_fuzz.ratio(
124
+ NON_WORD_CHARACTERS.sub("", name),
125
+ NON_WORD_CHARACTERS.sub("", possible_name),
126
+ )
127
+ if joined_score < 90:
128
+ name_words = expand_initials(name.split(), possible_name.split())
129
+ if name_words is None:
130
+ return 0.0
131
+ name = " ".join(name_words)
129
132
  words = set(name.split())
130
133
  possible_words = set(possible_name.split())
131
134
 
@@ -262,7 +265,6 @@ def get_code_from_name(
262
265
  unmatched: list[str],
263
266
  fuzzy_match: bool = True,
264
267
  match_threshold: int = 5,
265
- matcher: Phonetics | RapidFuzzMatcher | None = None,
266
268
  ) -> str | None:
267
269
  """
268
270
  Given a name (org type, sector, etc), return the corresponding code. A
@@ -275,7 +277,6 @@ def get_code_from_name(
275
277
  unmatched: List of unmatched names
276
278
  fuzzy_match: Allow fuzzy matching or not
277
279
  match_threshold: Match threshold
278
- matcher: Fuzzy matcher to use. Defaults to None (RapidFuzzMatcher).
279
280
 
280
281
  Returns:
281
282
  Matching code
@@ -301,9 +302,7 @@ def get_code_from_name(
301
302
  unmatched.append(name)
302
303
  return None
303
304
  names = [x for x in code_lookup.keys() if len(x) > match_threshold]
304
- if matcher is None:
305
- matcher = RapidFuzzMatcher()
306
- name_index = matcher.match(
305
+ name_index = RapidFuzzMatcher().match(
307
306
  possible_names=names,
308
307
  name=name,
309
308
  alternative_name=name_clean,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: hdx-python-utilities
3
- Version: 4.2.0
3
+ Version: 4.2.2
4
4
  Summary: HDX Python Utilities for streaming tabular data, date and time handling and other helpful functions
5
5
  Project-URL: Homepage, https://github.com/OCHA-DAP/hdx-python-utilities
6
6
  Author-email: Michael Rans <rans@email.com>
@@ -30,7 +30,6 @@ Requires-Dist: ijson>=3.5.0
30
30
  Requires-Dist: jsonlines>=4.0.0
31
31
  Requires-Dist: loguru
32
32
  Requires-Dist: openpyxl>=3.1.2
33
- Requires-Dist: pyphonetics
34
33
  Requires-Dist: python-dateutil<2.9.1,>=2.9.0
35
34
  Requires-Dist: rapidfuzz
36
35
  Requires-Dist: ratelimit
@@ -1,5 +1,5 @@
1
1
  hdx/utilities/__init__.py,sha256=DQztoBWScN4UoX3pTjA6JN_CHX-Lls3duu41YLJtdcM,72
2
- hdx/utilities/_version.py,sha256=nT-3r7mhc-hueGCIfa_bEdffKxwEZc-wM7Fo2U4hJAo,520
2
+ hdx/utilities/_version.py,sha256=EqBeLrNZADYhEPttnGzsir1xR8w9JWGzoEv_soUoQT8,520
3
3
  hdx/utilities/base_downloader.py,sha256=SzGSz57CwdmtvL2gaNAldXtfQ4uN4F5mjYkBAyq3U7M,3501
4
4
  hdx/utilities/compare.py,sha256=3w12iMELCBsR9gKXM7zyRZAf8YSeRylJwmGuLlEkAVQ,1145
5
5
  hdx/utilities/dateparse.py,sha256=Rlg6YM-Rt8jol-6vKy1OfLemnsx3DfRJj4dIfALUe_E,42170
@@ -14,7 +14,7 @@ hdx/utilities/file_hashing.py,sha256=p9AN7m4OUFXbzgdtBUFw_npmD_T0l7oFmErjclZSX8k
14
14
  hdx/utilities/frictionless_wrapper.py,sha256=ooDqT0FxlPa6iTBJkedm0NMRzLjt9plI_BnPPtZiu1c,8517
15
15
  hdx/utilities/html.py,sha256=KX5m9mXbSsruzqIVg60REJMvvtd0R_pahrKUb4bGPYQ,2880
16
16
  hdx/utilities/loader.py,sha256=_0cbraMPneahCiQCetUovwMMaHCBmPhirys9QPMiD7c,6097
17
- hdx/utilities/matching.py,sha256=G1-_oliN9B4qohzEnwYHpRQ1VKl_zGnXTVFvPMq-GFI,18381
17
+ hdx/utilities/matching.py,sha256=iBpPVhObqemqRsHOpNOImAKrVQvsd9aGarCvyasVXwk,18262
18
18
  hdx/utilities/path.py,sha256=CKyWY-iazfOKL2Nrwi9t8EvN5xMQKLd-yXxyE5wKLv8,15423
19
19
  hdx/utilities/retriever.py,sha256=kTrl_LneksM6C7FyWVfwd1-W7qUHnHrqr64lO8MLHuw,18386
20
20
  hdx/utilities/saver.py,sha256=O9QHGjqH9E4UMnDzmDPmvINCq8qGbljq4U_l9VnLfdM,8038
@@ -25,7 +25,7 @@ hdx/utilities/url.py,sha256=iN4tqU7sbpXc3EXMNaQfoot8Wm98AvDKN_oKS2heuAE,4312
25
25
  hdx/utilities/useragent.py,sha256=u1nwquIei93lLMAGqHrLs0w99qIIM1A2yUY7AOMSFy4,6862
26
26
  hdx/utilities/uuid.py,sha256=KUpQoo0P3RFO_n6-qdxTDxdaJMUpkTosdI0ulyk-RYA,557
27
27
  hdx/utilities/zip_crc.py,sha256=qs-81dd1oX6uIfwH7DUoZaS7OngHwOb7lnogBVmtBl8,4275
28
- hdx_python_utilities-4.2.0.dist-info/METADATA,sha256=ysWOqyrcycHkMjCtMJ1cL6gRjGpgtatgXi3NoxkiWS8,5522
29
- hdx_python_utilities-4.2.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
30
- hdx_python_utilities-4.2.0.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
31
- hdx_python_utilities-4.2.0.dist-info/RECORD,,
28
+ hdx_python_utilities-4.2.2.dist-info/METADATA,sha256=HUSmheFFsv3JuhHVu_EzPKGym77ZRNAKoDk1Z6DIbQA,5495
29
+ hdx_python_utilities-4.2.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
30
+ hdx_python_utilities-4.2.2.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
31
+ hdx_python_utilities-4.2.2.dist-info/RECORD,,