diff --git a/src/api/parsers.py b/src/api/parsers.py index 31a36bb0..28b1da79 100644 --- a/src/api/parsers.py +++ b/src/api/parsers.py @@ -46,12 +46,7 @@ def parse_name(name: str) -> str: else: parsed_name += char elif char.isdigit(): - num_digits = 0 - for c in name: # greedily retrieve as many digits as possible - if c.isdigit(): - num_digits += 1 - else: - break + num_digits = _greedily_count_digits_at_start_of_str(name) word = Helpers.number_to_word(int(name[:num_digits])) if parsed_name != "": word = Helpers.capitalize(word) @@ -75,6 +70,17 @@ def parse_name(name: str) -> str: return parsed_name +def _greedily_count_digits_at_start_of_str(word: str) -> int: + """Counts the number of digits at the start of the string.""" + num_digits = 0 + for c in word: + if c.isdigit(): + num_digits += 1 + else: + break + return num_digits + + def parse_unit(unit: str) -> str: """Parses the unit.""" if not isinstance(unit, str): diff --git a/tests/parsers_test.py b/tests/parsers_test.py new file mode 100644 index 00000000..aaf1fc07 --- /dev/null +++ b/tests/parsers_test.py @@ -0,0 +1,39 @@ +import pytest + +from api import parsers + + +class TestNameParser: + + @pytest.mark.parametrize( + "name, expected", + [ + ("a12", "aTwelve"), + ("a01", "aOne"), + ("042", "fortyTwo"), + ("a911bc13", "aNineHundredElevenBcThirteen"), + ("13_600", "thirteenSixHundred"), + ("a_5_6b7_$5", "aFiveSixBSevenFive"), + ], + ) + def test_number_substitution(self, name: str, expected: str): + assert parsers.parse_name(name) == expected + + @pytest.mark.parametrize( + "name, expected", + [ + ("ä", "ae"), + ("Ä", "Ae"), + ("ü", "ue"), + ("Ü", "Ue"), + ("ö", "oe"), + ("Ö", "Oe"), + ("ß", "ss"), + ("ẞ", "Ss"), + ("äh", "aeh"), + ("Füße", "Fuesse"), + ("GIEẞEN", "GIESsEN"), + ], + ) + def test_umlaut_replacement(self, name: str, expected: str): + assert parsers.parse_name(name) == expected