Skip to content

Commit

Permalink
babel.messages.catalog: deduplicate _to_fuzzy_match_key logic (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Mar 2, 2023
1 parent 0ce196f commit 94e533f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions babel/messages/catalog.py
Expand Up @@ -827,15 +827,13 @@ def update(
self._messages = OrderedDict()

# Prepare for fuzzy matching
fuzzy_candidates = []
fuzzy_candidates = {}
if not no_fuzzy_matching:
fuzzy_candidates = {}
for msgid in messages:
if msgid and messages[msgid].string:
key = self._key_for(msgid)
ctxt = messages[msgid].context
modified_key = key.lower().strip()
fuzzy_candidates[modified_key] = (key, ctxt)
fuzzy_candidates[self._to_fuzzy_match_key(key)] = (key, ctxt)
fuzzy_matches = set()

def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, str] | str) -> None:
Expand Down Expand Up @@ -883,12 +881,11 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
else:
if not no_fuzzy_matching:
# do some fuzzy matching with difflib
if isinstance(key, tuple):
matchkey = key[0] # just the msgid, no context
else:
matchkey = key
matches = get_close_matches(matchkey.lower().strip(),
fuzzy_candidates.keys(), 1)
matches = get_close_matches(
self._to_fuzzy_match_key(key),
fuzzy_candidates.keys(),
1,
)
if matches:
modified_key = matches[0]
newkey, newctxt = fuzzy_candidates[modified_key]
Expand All @@ -912,6 +909,14 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
# used to update the catalog
self.creation_date = template.creation_date

def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str:
"""Converts a message key to a string suitable for fuzzy matching."""
if isinstance(key, tuple):
matchkey = key[0] # just the msgid, no context
else:
matchkey = key
return matchkey.lower().strip()

def _key_for(self, id: _MessageID, context: str | None = None) -> tuple[str, str] | str:
"""The key for a message is just the singular ID even for pluralizable
messages, but is a ``(msgid, msgctxt)`` tuple for context-specific
Expand Down

0 comments on commit 94e533f

Please sign in to comment.