Skip to content

Commit

Permalink
Fix ignoring of short words
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Oct 9, 2024
1 parent a0727e8 commit 9ec2fe8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 16 additions & 7 deletions df_translation_toolkit/utils/df_ignore_string_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@


class IgnoringRuleRegistry:
all_rules: list[Callable[[str], bool]]
all_rules: dict[str, Callable[[str], bool]]

def __init__(self) -> str:
self.all_rules = []
self.all_rules = {}

def register(self, function: Callable[[str], bool]) -> Callable[[str], bool]:
self.all_rules.append(function)
self.all_rules[function.__name__] = function
return function

def check_ignore(self, string: str) -> bool:
return any(rule(string) for rule in self.all_rules)
def check_ignore(self, string: str) -> str | None:
"""
Check if string should be ignored
:param string: string to check
:return: name of the rule if string should be ignored or None if string should be translated
"""
for name, rule in self.all_rules.items():
if rule(string):
return name

return None


rules = IgnoringRuleRegistry()
Expand Down Expand Up @@ -410,7 +419,7 @@ def ignore_by_blacklisted_words(string: str) -> bool:

@rules.register
def ignore_short_words(string: str) -> bool:
return len(string) <= MINIMAL_TRANSLATABLE_WORD_LENGTH and string.strip() not in allowed_short_words
return len(string) < MINIMAL_TRANSLATABLE_WORD_LENGTH and string.strip() not in allowed_short_words


blacklisted_substrings = {"placed out of bounds", "set to default", "Patched save"}
Expand All @@ -421,7 +430,7 @@ def ignore_by_blacklisted_substrings(string: str) -> bool:
return any(substring in string for substring in blacklisted_substrings)


def all_ignore_rules(string: str) -> bool:
def all_ignore_rules(string: str) -> str:
return rules.check_ignore(string)


Expand Down
5 changes: 4 additions & 1 deletion tests/test_df_ignore_string_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
(rules.all_ignore_rules, "i", True),
(rules.all_ignore_rules, "a", True),
(rules.dont_ignore, "", False),
(rules.all_ignore_rules, "She", False),
(rules.all_ignore_rules, "she", False),
],
)
def test_ignore_rules(rule, string, ignore):
assert rule(string) is ignore
rule_name = rule(string)
assert bool(rule_name) is ignore, rule_name

0 comments on commit 9ec2fe8

Please sign in to comment.