Repository: imankulov/linguee-api

Issue:
500 error for "über"
xextract.parsers.ParsingError: Parser String(name="usage_frequency") matched 2 elements ("?" expected).

looks like there are two span with tag_c class.


Discussion:

Patch:
diff --git a/linguee_api/parser_utils.py b/linguee_api/parser_utils.py
index cadea2b..6708037 100644
--- a/linguee_api/parser_utils.py
+++ b/linguee_api/parser_utils.py
@@ -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
diff --git a/linguee_api/parsers.py b/linguee_api/parsers.py
index 77527d7..d93a36c 100644
--- a/linguee_api/parsers.py
+++ b/linguee_api/parsers.py
@@ -13,7 +13,12 @@ from linguee_api.models import (
     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):
@@ -237,12 +242,19 @@ lemma_schema = [
                 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",


Test patch:
diff --git a/tests/parsers/test_search_result.py b/tests/parsers/test_search_result.py
index bc71300..ea77397 100644
--- a/tests/parsers/test_search_result.py
+++ b/tests/parsers/test_search_result.py
@@ -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