-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from spraakbanken/136-error-on-non-existent-word
136 error on non existent word
- Loading branch information
Showing
13 changed files
with
142 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
import logging | ||
import sys | ||
|
||
import httpx | ||
from opentelemetry import trace | ||
from sblex.fm import Morphology | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HttpMorphology(Morphology): | ||
def __init__(self, http_client: httpx.AsyncClient) -> None: | ||
self._http_client = http_client | ||
|
||
async def lookup(self, word: str, n: int = 0) -> bytes: | ||
async def lookup(self, word: str, n: int = 0) -> bytes | None: | ||
with trace.get_tracer(__name__).start_as_current_span( | ||
sys._getframe().f_code.co_name | ||
) as _process_api_span: | ||
response = await self._http_client.get(f"/morph/{word}/{n}") | ||
if response.status_code == 404: | ||
return None | ||
elif response.status_code >= 400: | ||
logger.error( | ||
"Http lookup failed with status=%d", | ||
response.status_code, | ||
extra={"content": response.content}, | ||
) | ||
return None | ||
return response.content | ||
|
||
async def lookup_from_bytes(self, s: bytes) -> bytes: | ||
async def lookup_from_bytes(self, s: bytes) -> bytes | None: | ||
response = await self._http_client.get(f"/morph/{s.decode('utf-8')}/0") | ||
return response.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/e2e/test_infrastructure/__snapshots__/test_queries.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# serializer version: 1 | ||
# name: TestHttpMorphology.test_lookup_from_bytes | ||
b'{"a":[{"gf":"dv\xc3\xa4ljas","id":"dv\xc3\xa4ljas..vb.1","pos":"vb","is":[],"msd":"pres ind s-form","p":"vb_vs_dv\xc3\xa4ljas"},{"gf":"dv\xc3\xa4ljas","id":"dv\xc3\xa4ljas..vb.1","pos":"vb","is":[],"msd":"imper","p":"vb_vs_dv\xc3\xa4ljas"}],"c":""}' | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
import pytest_asyncio | ||
from httpx import AsyncClient | ||
from sblex.infrastructure.queries.http_morpology import HttpMorphology | ||
|
||
|
||
@pytest_asyncio.fixture(name="http_morphology") | ||
async def fixture_http_morphology(fm_client: AsyncClient) -> HttpMorphology: | ||
return HttpMorphology(http_client=fm_client) | ||
|
||
|
||
class TestHttpMorphology: | ||
@pytest.mark.asyncio | ||
async def test_lookup_from_bytes(self, http_morphology: HttpMorphology, snapshot) -> None: | ||
result = await http_morphology.lookup_from_bytes(b"dv\xc3\xa4ljes") | ||
|
||
assert result == snapshot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from unittest.mock import AsyncMock | ||
|
||
import pytest | ||
from httpx import AsyncClient, Response | ||
from sblex.infrastructure.queries.http_morpology import HttpMorphology | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_lookup_from_bytes() -> None: | ||
mock_client = AsyncMock(spec=AsyncClient) | ||
attrs = {"get.return_value": Response(400, content="error")} | ||
mock_client.configure_mock(**attrs) | ||
|
||
http_morphology = HttpMorphology(mock_client) | ||
result = await http_morphology.lookup("dväljes") | ||
|
||
assert result is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from sblex.trie.trie import TrieBuilder | ||
|
||
|
||
def test_trie(): | ||
trie_builder = TrieBuilder() | ||
trie_builder.insert("ösja", b"{head:osja,pos:vb}") | ||
trie_builder.insert("örliga", b"{head:orliga,pos:vb}") | ||
print(f"{trie_builder.trie=}") | ||
trie = trie_builder.build() | ||
print(f"{trie._trie=}") | ||
# assert False |