From 9210c6e86dc72528f9a0aed554c26df877dbf99e Mon Sep 17 00:00:00 2001 From: Repinoid Date: Sat, 31 Oct 2026 10:10:59 +0300 Subject: [PATCH] fix: match prefixed contract core aliases --- site/services/grouping.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/site/services/grouping.py b/site/services/grouping.py index 1f58794..ad24062 100644 --- a/site/services/grouping.py +++ b/site/services/grouping.py @@ -59,12 +59,27 @@ def _contract_number_identity(number): normalized = str(number or "").upper().translate(_HOMOGLYPHS) prefix_match = re.match(r"^(?:XXX|ХХХ)\d+[-_/](.+)$", normalized) if not prefix_match: - return {"full": full_key, "suffix": full_key, "prefixed": False} + suffix_aliases = [full_key] + numbered_suffix = re.match(r"^(.+)_\d+$", normalized) + if numbered_suffix: + core_key = canonicalize_contract_number(numbered_suffix.group(1)) + if sum(character.isdigit() for character in core_key) >= 4: + suffix_aliases.append(core_key) + return { + "full": full_key, "suffix": full_key, + "suffix_aliases": suffix_aliases, "prefixed": False, + } suffix_key = canonicalize_contract_number(prefix_match.group(1)) digit_count = sum(character.isdigit() for character in suffix_key) if digit_count < 4: - return {"full": full_key, "suffix": full_key, "prefixed": True} - return {"full": full_key, "suffix": suffix_key, "prefixed": True} + return { + "full": full_key, "suffix": full_key, + "suffix_aliases": [full_key], "prefixed": True, + } + return { + "full": full_key, "suffix": suffix_key, + "suffix_aliases": [suffix_key], "prefixed": True, + } def canonicalize_counterparty(name, inn): @@ -112,7 +127,12 @@ def _shared_contract_match(left_keys, right_keys): for right_key, right in right_keys.items(): if left_key == right_key: return left_key, left, right - same_suffix = left.get("suffix") == right.get("suffix") + if left.get("prefixed") and not right.get("prefixed"): + same_suffix = left.get("suffix") in right.get("suffix_aliases", []) + elif right.get("prefixed") and not left.get("prefixed"): + same_suffix = right.get("suffix") in left.get("suffix_aliases", []) + else: + same_suffix = left.get("suffix") == right.get("suffix") if same_suffix and (left.get("prefixed") or right.get("prefixed")): if left.get("prefixed") and right.get("prefixed"): continue