Skip to content

Commit f97eccb

Browse files
committed
Update type signatures in line with normality 3 logic
1 parent 66d0407 commit f97eccb

6 files changed

Lines changed: 28 additions & 29 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2024 OpenSanctions Datenbanken GmbH
3+
Copyright (c) 2025 OpenSanctions Datenbanken GmbH
44
Copyright (c) 2018 Journalism Development Network, Inc.
55
Copyright (c) 2016 Friedrich Lindenberg
66

fingerprints/cleanup.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import Optional
44
from functools import lru_cache
5-
from normality import collapse_spaces, ascii_text, category_replace
5+
from normality import squash_spaces, ascii_text, category_replace
66

77
from fingerprints.constants import WS, BRACKETED
88

@@ -68,23 +68,23 @@ def clean_name_ascii(text: Optional[str]) -> Optional[str]:
6868
It transliterates the text to ASCII, removes punctuation and symbols, converts the
6969
text to lowercase, replaces certain character categories, and collapses consecutive
7070
spaces.
71-
71+
7272
Args:
7373
text (Optional[str]): The input text to be cleaned.
74-
74+
7575
Returns:
7676
Optional[str]: The cleaned text, or None if the cleaned text is empty or too short.
7777
"""
7878
# transliterate to ascii
79-
text = ascii_text(text)
8079
if text is None:
8180
return None
81+
text = ascii_text(text)
8282
# replace punctuation and symbols
8383
text = CHARACTERS_REMOVE_RE.sub("", text)
8484
text = text.lower()
8585
cleaned = category_replace(text)
86-
cleaned = collapse_spaces(cleaned)
87-
if cleaned is None or len(cleaned) < 2:
86+
cleaned = squash_spaces(cleaned)
87+
if len(cleaned) < 2:
8888
return None
8989
return cleaned
9090

@@ -96,7 +96,7 @@ def clean_name_light(text: str) -> Optional[str]:
9696
text = CHARACTERS_REMOVE_RE.sub("", text)
9797
text = text.lower()
9898
cleaned = category_replace(text)
99-
cleaned = collapse_spaces(cleaned)
100-
if cleaned is None or len(cleaned) < 2:
99+
cleaned = squash_spaces(cleaned)
100+
if len(cleaned) < 2:
101101
return None
102102
return cleaned

fingerprints/fingerprint.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from typing import Optional
3-
from normality import collapse_spaces, stringify
3+
from normality import squash_spaces, stringify
44

55
from fingerprints.constants import WS
66
from fingerprints.types import replace_types
@@ -26,16 +26,18 @@ def fingerprint(
2626

2727
# Super hard-core string scrubbing
2828
text = clean_name_ascii(text)
29+
if text is None:
30+
return None
2931
text = replace_types(text)
3032

3133
if keep_order:
32-
text = collapse_spaces(text)
33-
elif text is not None:
34+
text = squash_spaces(text)
35+
else:
3436
# final manicure, based on openrefine algo
3537
parts = [p for p in text.split(WS) if len(p)]
3638
text = WS.join(sorted(set(parts)))
3739

38-
if text is None or not len(text):
40+
if not len(text):
3941
return None
4042

4143
return text

fingerprints/types/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
from typing import Optional
2-
from normality import collapse_spaces
1+
from normality import squash_spaces
32

43
from fingerprints.cleanup import clean_name_ascii
54
from fingerprints.types.replacer import get_replacer, NormFunc
65

76

8-
def replace_types(text: Optional[str]) -> Optional[str]:
7+
def replace_types(text: str) -> str:
98
"""Chomp down company types to a more convention form."""
109
return get_replacer()(text)
1110

1211

13-
def remove_types(
14-
text: Optional[str], clean: NormFunc = clean_name_ascii
15-
) -> Optional[str]:
12+
def remove_types(text: str, clean: NormFunc = clean_name_ascii) -> str:
1613
"""Remove company type names from a piece of text.
1714
1815
WARNING: This converts to ASCII by default, pass in a different
1916
`clean` function if you need a different behaviour."""
20-
text = clean(text)
21-
removed = get_replacer(clean, remove=True)(text)
22-
return collapse_spaces(removed)
17+
cleaned = clean(text)
18+
if cleaned is None:
19+
return ""
20+
removed = get_replacer(clean, remove=True)(cleaned)
21+
return squash_spaces(removed)

fingerprints/types/replacer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import re
22
import logging
33
from functools import lru_cache
4-
from typing import Callable, Optional, Dict, Match
4+
from typing import Callable, Dict, Match, Optional
55

66
from fingerprints.types.data import TYPES
77
from fingerprints.constants import WS
88
from fingerprints.cleanup import clean_name_ascii
99

1010
log = logging.getLogger(__name__)
11-
NormFunc = Callable[[Optional[str]], Optional[str]]
12-
ReplaceFunc = Callable[[Optional[str]], Optional[str]]
11+
NormFunc = Callable[[str], Optional[str]]
12+
ReplaceFunc = Callable[[str], str]
1313

1414

1515
class Replacer(object):
@@ -28,9 +28,7 @@ def get_canonical(self, match: Match[str]) -> str:
2828
return WS
2929
return self.replacements.get(match.group(1), match.group(1))
3030

31-
def __call__(self, text: Optional[str]) -> Optional[str]:
32-
if text is None:
33-
return None
31+
def __call__(self, text: str) -> str:
3432
return self.matcher.sub(self.get_canonical, text)
3533

3634

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ classifiers = [
1717
"Programming Language :: Python :: 3.12",
1818
]
1919
requires-python = ">= 3.10"
20-
dependencies = ["normality >= 2.5.0, < 3.0.0"]
20+
dependencies = ["normality >= 3.0.1, < 4.0.0"]
2121

2222
[project.urls]
2323
Documentation = "https://github.com/opensanctions/fingerprints/"

0 commit comments

Comments
 (0)