Skip to content

Commit

Permalink
Fix translation for über
Browse files Browse the repository at this point in the history
Make sure that parser is not confused when it finds two elements that
look like the usage frequency.

Example:

   "präp (temporal; e.g. for 3 years)  (meistens verwendet)"

Closes #48
  • Loading branch information
imankulov committed Apr 25, 2024
1 parent 7ca9ca3 commit 3978fb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 8 additions & 0 deletions linguee_api/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ def take_first_item(variants) -> Optional[str]:
if not variants["item"]:
return None
return variants["item"][0]


def take_first_non_empty_item(variants) -> Optional[str]:
"""Take the first non-empty item variant and normalize."""
for item in variants["item"]:
if item:
return item
return None
24 changes: 18 additions & 6 deletions linguee_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
SearchResultOrError,
UsageFrequency,
)
from linguee_api.parser_utils import concat_values, normalize, take_first_item
from linguee_api.parser_utils import (
concat_values,
normalize,
take_first_item,
take_first_non_empty_item,
)


class IParser(abc.ABC):
Expand Down Expand Up @@ -237,12 +242,19 @@ def normalize_lemma_text(children):
attr="onclick",
callback=parse_audio_links,
),
String(
Group(
name="usage_frequency",
quant="?",
css="span.tag_c",
attr="class",
callback=parse_usage_frequency,
quant=1,
callback=take_first_non_empty_item,
children=[
String(
name="item",
quant="*",
css="span.tag_c",
attr="class",
callback=parse_usage_frequency,
),
],
),
Group(
name="examples",
Expand Down
1 change: 1 addition & 0 deletions tests/parsers/test_search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async def test_parser_should_find_correction(
("wünschen", "de", "en"),
("envisage", "en", "zh"),
("envisage", "en", "sv"),
("über", "de", "en"),
],
)
@pytest.mark.asyncio
Expand Down

0 comments on commit 3978fb7

Please sign in to comment.