Skip to content

Commit

Permalink
feat: add FmRunnerInflectionTable
Browse files Browse the repository at this point in the history
  • Loading branch information
kod-kristoff committed Mar 15, 2024
1 parent 4b104ec commit e6cbbc3
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/sblex/application/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sblex.application.queries.fullforms import FullformQuery
from sblex.application.queries.inflection import GenerateInflectionTable
from sblex.application.queries.lex_fullforms import FullformLexQuery
from sblex.application.queries.lookup_lid import LookupLid

__all__ = ["FullformLexQuery", "LookupLid", "FullformQuery"]
__all__ = ["FullformLexQuery", "LookupLid", "FullformQuery", "GenerateInflectionTable"]
16 changes: 16 additions & 0 deletions src/sblex/application/queries/inflection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import abc
from typing import Iterable, TypedDict


class InflectionTableRow(TypedDict):
form: str
gf: str
pos: str
inhs: list[str]
msd: str
p: str


class GenerateInflectionTable(abc.ABC):
@abc.abstractmethod
def query(self, paradigm: str, word: str) -> Iterable[InflectionTableRow]: ...
4 changes: 2 additions & 2 deletions src/sblex/fm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sblex.fm.fm_runner import FMrunner
from sblex.fm.fm_runner import FmRunner
from sblex.fm.morphology import MemMorphology, Morphology

__all__ = ["FMrunner", "Morphology", "MemMorphology"]
__all__ = ["FmRunner", "Morphology", "MemMorphology"]
16 changes: 14 additions & 2 deletions src/sblex/fm/fm_runner.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import logging
import subprocess
from pathlib import Path
from typing import TypedDict

from json_arrays import jsonlib
from opentelemetry import trace

tracer = trace.get_tracer(__name__)


class FMrunner:
class InflectionRow(TypedDict):
word: str
head: str
pos: str
inhs: list[str]
param: str
id: str
p: str
attr: str


class FmRunner:
def __init__(self, binary_path: Path, *, locale: str | None = None) -> None:
self.binary_path = binary_path.resolve()
self.locale = locale or 'LC_ALL="sv_SE.UTF-8"'

def inflection(self, paradigm: str, word: str) -> list:
def inflection(self, paradigm: str, word: str) -> list[InflectionRow]:
with tracer.start_as_current_span("call_fm_binary") as call_span:
args = f'{paradigm} "{word}";'
program = [
Expand Down
3 changes: 2 additions & 1 deletion src/sblex/infrastructure/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sblex.infrastructure.queries.fm_runner_inflection import FmRunnerInflectionTable
from sblex.infrastructure.queries.http_morpology import HttpMorphology
from sblex.infrastructure.queries.lookup_lex_fullforms import LookupFullformLexQuery
from sblex.infrastructure.queries.mem_lookup_lid import MemLookupLid

__all__ = ["HttpMorphology", "MemLookupLid", "LookupFullformLexQuery"]
__all__ = ["HttpMorphology", "MemLookupLid", "LookupFullformLexQuery", "FmRunnerInflectionTable"]
22 changes: 22 additions & 0 deletions src/sblex/infrastructure/queries/fm_runner_inflection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Iterable

from sblex.application.queries import GenerateInflectionTable
from sblex.application.queries.inflection import InflectionTableRow
from sblex.fm import FmRunner


class FmRunnerInflectionTable(GenerateInflectionTable):
def __init__(self, *, fm_runner: FmRunner) -> None:
super().__init__()
self.fm_runner = fm_runner

Check warning on line 11 in src/sblex/infrastructure/queries/fm_runner_inflection.py

View check run for this annotation

Codecov / codecov/patch

src/sblex/infrastructure/queries/fm_runner_inflection.py#L10-L11

Added lines #L10 - L11 were not covered by tests

def query(self, paradigm: str, word: str) -> Iterable[InflectionTableRow]:
for row in self.fm_runner.inflection(paradigm, word):
yield {

Check warning on line 15 in src/sblex/infrastructure/queries/fm_runner_inflection.py

View check run for this annotation

Codecov / codecov/patch

src/sblex/infrastructure/queries/fm_runner_inflection.py#L14-L15

Added lines #L14 - L15 were not covered by tests
"form": row["word"],
"gf": row["head"],
"pos": row["pos"],
"inhs": row["inhs"],
"msd": row["param"],
"p": row["p"],
}
9 changes: 0 additions & 9 deletions src/sblex/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@
def function(format, paradigm, word):
result = ""
try:
saldo = 'export LC_ALL="sv_SE.UTF-8";/home/markus/fm/sblex/bin/saldo -i'
input = paradigm + ' "' + word + '";'
fin, fout = popen2.popen2(saldo)
fout.write(input)
fout.close()
for line in fin.readlines():
result += line
if result.strip() == "":
result = "[]"
j = cjson.decode(utf8.d(result))
if format == "html":
result = htmlize(paradigm, word, j)
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/fm/test_fm_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from pathlib import Path

import pytest
from sblex.fm import FMrunner
from sblex.fm import FmRunner


@pytest.fixture(name="fm_runner")
def fixture_fm_runner() -> FMrunner:
return FMrunner(Path("./bin/saldo"))
def fixture_fm_runner() -> FmRunner:
return FmRunner(Path("./bin/saldo"))


@pytest.mark.skipif(
Expand Down

0 comments on commit e6cbbc3

Please sign in to comment.