thesaurus-artifacts 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- thesaurus/__init__.py +16 -0
- thesaurus/__main__.py +3 -0
- thesaurus/adapters/__init__.py +1 -0
- thesaurus/adapters/_paths.py +27 -0
- thesaurus/adapters/base.py +17 -0
- thesaurus/adapters/csv.py +99 -0
- thesaurus/adapters/json.py +137 -0
- thesaurus/adapters/registry.py +32 -0
- thesaurus/api/__init__.py +1 -0
- thesaurus/api/build/__init__.py +7 -0
- thesaurus/api/build/app.py +385 -0
- thesaurus/api/build/jobs.py +116 -0
- thesaurus/api/build/servers.py +127 -0
- thesaurus/api/common.py +48 -0
- thesaurus/api/serve/__init__.py +6 -0
- thesaurus/api/serve/app.py +271 -0
- thesaurus/api/serve/store.py +95 -0
- thesaurus/artifact/__init__.py +22 -0
- thesaurus/artifact/bundle.py +180 -0
- thesaurus/artifact/reader.py +344 -0
- thesaurus/artifact/validate.py +273 -0
- thesaurus/artifact/writer.py +156 -0
- thesaurus/cli.py +367 -0
- thesaurus/compiler/__init__.py +5 -0
- thesaurus/compiler/cache.py +265 -0
- thesaurus/compiler/context.py +95 -0
- thesaurus/compiler/core.py +1014 -0
- thesaurus/compiler/glossary.py +47 -0
- thesaurus/compiler/graph.py +203 -0
- thesaurus/compiler/schemas.py +75 -0
- thesaurus/compiler/select.py +184 -0
- thesaurus/compiler/services.py +277 -0
- thesaurus/config.py +217 -0
- thesaurus/ids.py +38 -0
- thesaurus/models.py +257 -0
- thesaurus/normalize.py +19 -0
- thesaurus/types.py +89 -0
- thesaurus_artifacts-0.2.0.dist-info/METADATA +520 -0
- thesaurus_artifacts-0.2.0.dist-info/RECORD +50 -0
- thesaurus_artifacts-0.2.0.dist-info/WHEEL +4 -0
- thesaurus_artifacts-0.2.0.dist-info/entry_points.txt +3 -0
- thesaurus_artifacts-0.2.0.dist-info/licenses/LICENSE +21 -0
- thesaurus_search/__init__.py +25 -0
- thesaurus_search/cli.py +113 -0
- thesaurus_search/client.py +284 -0
- thesaurus_search/fuzzy.py +86 -0
- thesaurus_search/index.py +262 -0
- thesaurus_search/models.py +84 -0
- thesaurus_search/query.py +155 -0
- thesaurus_search/text.py +27 -0
thesaurus/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Offline compiler for multilingual lexical search artifacts."""
|
|
2
|
+
|
|
3
|
+
from thesaurus.artifact.reader import Artifact
|
|
4
|
+
from thesaurus.compiler import Compiler, compile_records
|
|
5
|
+
from thesaurus.config import CompilerConfig, load_config
|
|
6
|
+
|
|
7
|
+
__version__ = "0.2.0"
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Artifact",
|
|
11
|
+
"Compiler",
|
|
12
|
+
"CompilerConfig",
|
|
13
|
+
"__version__",
|
|
14
|
+
"compile_records",
|
|
15
|
+
"load_config",
|
|
16
|
+
]
|
thesaurus/__main__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Input adapters."""
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Path helpers shared by input adapters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_path(data: Any, path: str | None) -> Any:
|
|
9
|
+
"""Read a dotted path from nested dictionaries."""
|
|
10
|
+
if not path:
|
|
11
|
+
return None
|
|
12
|
+
current = data
|
|
13
|
+
for part in path.split("."):
|
|
14
|
+
if not isinstance(current, dict) or part not in current:
|
|
15
|
+
return None
|
|
16
|
+
current = current[part]
|
|
17
|
+
return current
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def matches(path: str, patterns: set[str]) -> bool:
|
|
21
|
+
"""Return whether a path is equal to or below any configured pattern."""
|
|
22
|
+
return any(
|
|
23
|
+
path == pattern
|
|
24
|
+
or path.startswith(f"{pattern}.")
|
|
25
|
+
or path.startswith(f"{pattern}[")
|
|
26
|
+
for pattern in patterns
|
|
27
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Input adapter contract."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
from typing import Protocol
|
|
5
|
+
|
|
6
|
+
from thesaurus.config import CompilerConfig
|
|
7
|
+
from thesaurus.models import Record
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class InputAdapter(Protocol):
|
|
11
|
+
"""Turn an input source into canonical records."""
|
|
12
|
+
|
|
13
|
+
name: str
|
|
14
|
+
|
|
15
|
+
def read(self, config: CompilerConfig) -> Iterator[Record]:
|
|
16
|
+
"""Yield one canonical record per input document."""
|
|
17
|
+
...
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""CSV input adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import csv
|
|
6
|
+
import hashlib
|
|
7
|
+
import json
|
|
8
|
+
from collections.abc import Iterator
|
|
9
|
+
|
|
10
|
+
from thesaurus.adapters._paths import matches
|
|
11
|
+
from thesaurus.adapters.base import InputAdapter
|
|
12
|
+
from thesaurus.config import CompilerConfig
|
|
13
|
+
from thesaurus.models import Record
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CsvAdapter(InputAdapter):
|
|
17
|
+
"""Read a CSV file where each row is a document."""
|
|
18
|
+
|
|
19
|
+
name = "csv"
|
|
20
|
+
|
|
21
|
+
def read(self, config: CompilerConfig) -> Iterator[Record]:
|
|
22
|
+
cfg = config.input
|
|
23
|
+
with cfg.path.open("r", encoding=cfg.encoding, newline="") as handle:
|
|
24
|
+
reader = csv.DictReader(handle, delimiter=cfg.csv.delimiter)
|
|
25
|
+
for row in reader:
|
|
26
|
+
yield self._to_record(row, config)
|
|
27
|
+
|
|
28
|
+
def _to_record(self, row: dict[str, str], config: CompilerConfig) -> Record:
|
|
29
|
+
cfg = config.input
|
|
30
|
+
original = {
|
|
31
|
+
key: (value or "").strip()
|
|
32
|
+
for key, value in row.items()
|
|
33
|
+
if key is not None
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
record_id = original.get(cfg.id_path, "")
|
|
37
|
+
language = original.get(cfg.language_path, "") if cfg.language_path else ""
|
|
38
|
+
|
|
39
|
+
fields: dict[str, str] = {}
|
|
40
|
+
translations: dict[str, dict[str, str]] = {}
|
|
41
|
+
metadata: dict[str, str] = {}
|
|
42
|
+
metadata_paths = set(cfg.metadata_paths)
|
|
43
|
+
|
|
44
|
+
for column, value in original.items():
|
|
45
|
+
if column in {cfg.id_path, cfg.language_path}:
|
|
46
|
+
continue
|
|
47
|
+
if not value:
|
|
48
|
+
continue
|
|
49
|
+
|
|
50
|
+
parsed = _parse_translation_column(column, cfg.csv.translation_suffix)
|
|
51
|
+
if parsed:
|
|
52
|
+
lang, base = parsed
|
|
53
|
+
translations.setdefault(lang, {})[base] = value
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
if matches(column, metadata_paths):
|
|
57
|
+
metadata[column] = value
|
|
58
|
+
continue
|
|
59
|
+
if cfg.field_paths and not matches(column, set(cfg.field_paths)):
|
|
60
|
+
continue
|
|
61
|
+
if matches(column, set(cfg.exclude_paths)):
|
|
62
|
+
continue
|
|
63
|
+
fields[column] = value
|
|
64
|
+
|
|
65
|
+
return Record(
|
|
66
|
+
id=record_id or _row_id(original),
|
|
67
|
+
language=language or config.languages.source,
|
|
68
|
+
fields=fields,
|
|
69
|
+
translations=translations,
|
|
70
|
+
metadata=metadata,
|
|
71
|
+
source=original if cfg.keep_source else None,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _parse_translation_column(column: str, template: str) -> tuple[str, str] | None:
|
|
76
|
+
"""Split ``title__en`` into ``("en", "title")`` for a ``__{lang}`` template."""
|
|
77
|
+
if "{lang}" not in template:
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
before, _, after = template.partition("{lang}")
|
|
81
|
+
if after and not column.endswith(after):
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
body = column[: -len(after)] if after else column
|
|
85
|
+
index = body.rfind(before)
|
|
86
|
+
if index < 0:
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
language = body[index + len(before) :]
|
|
90
|
+
base = body[:index]
|
|
91
|
+
if not language or not base:
|
|
92
|
+
return None
|
|
93
|
+
return language, base
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _row_id(row: dict[str, str]) -> str:
|
|
97
|
+
canonical = json.dumps(row, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
|
|
98
|
+
digest = hashlib.sha256(canonical.encode("utf-8")).hexdigest()[:12]
|
|
99
|
+
return f"doc-{digest}"
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""JSON and JSONL input adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import Iterator
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from thesaurus.adapters._paths import get_path, matches
|
|
12
|
+
from thesaurus.adapters.base import InputAdapter
|
|
13
|
+
from thesaurus.config import CompilerConfig, InputConfig
|
|
14
|
+
from thesaurus.models import Record
|
|
15
|
+
from thesaurus.types import ArrayMode, InputFormat
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class JsonAdapter(InputAdapter):
|
|
19
|
+
"""Read a JSON document, a JSON array, or JSON Lines."""
|
|
20
|
+
|
|
21
|
+
name = "json"
|
|
22
|
+
|
|
23
|
+
def read(self, config: CompilerConfig) -> Iterator[Record]:
|
|
24
|
+
path = config.input.path
|
|
25
|
+
fmt = config.input.format
|
|
26
|
+
if fmt == InputFormat.AUTO:
|
|
27
|
+
fmt = InputFormat.JSONL if path.suffix.lower() == ".jsonl" else InputFormat.JSON
|
|
28
|
+
for raw in self._load(path, fmt, config.input.encoding):
|
|
29
|
+
yield self._to_record(raw, config)
|
|
30
|
+
|
|
31
|
+
def _load(self, path: Path, fmt: InputFormat, encoding: str) -> Iterator[Any]:
|
|
32
|
+
if fmt == InputFormat.JSONL:
|
|
33
|
+
with path.open("r", encoding=encoding) as handle:
|
|
34
|
+
for line in handle:
|
|
35
|
+
line = line.strip()
|
|
36
|
+
if line:
|
|
37
|
+
yield json.loads(line)
|
|
38
|
+
return
|
|
39
|
+
|
|
40
|
+
with path.open("r", encoding=encoding) as handle:
|
|
41
|
+
data = json.load(handle)
|
|
42
|
+
if isinstance(data, list):
|
|
43
|
+
yield from data
|
|
44
|
+
else:
|
|
45
|
+
yield data
|
|
46
|
+
|
|
47
|
+
def _to_record(self, raw: Any, config: CompilerConfig) -> Record:
|
|
48
|
+
if not isinstance(raw, dict):
|
|
49
|
+
raw = {"value": raw}
|
|
50
|
+
|
|
51
|
+
cfg = config.input
|
|
52
|
+
id_value = get_path(raw, cfg.id_path)
|
|
53
|
+
language_value = get_path(raw, cfg.language_path) if cfg.language_path else None
|
|
54
|
+
translations = (
|
|
55
|
+
get_path(raw, cfg.translations_path) if cfg.translations_path else None
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
metadata: dict[str, Any] = {}
|
|
59
|
+
reserved = {cfg.id_path}
|
|
60
|
+
if cfg.language_path:
|
|
61
|
+
reserved.add(cfg.language_path)
|
|
62
|
+
if cfg.translations_path:
|
|
63
|
+
reserved.add(cfg.translations_path)
|
|
64
|
+
|
|
65
|
+
for path in cfg.metadata_paths:
|
|
66
|
+
value = get_path(raw, path)
|
|
67
|
+
if value is not None:
|
|
68
|
+
metadata[path] = value
|
|
69
|
+
reserved.add(path)
|
|
70
|
+
|
|
71
|
+
if isinstance(raw.get("metadata"), dict):
|
|
72
|
+
metadata.update(raw["metadata"])
|
|
73
|
+
reserved.add("metadata")
|
|
74
|
+
|
|
75
|
+
fields: dict[str, str] = {}
|
|
76
|
+
_flatten(raw, "", cfg, reserved, fields, metadata)
|
|
77
|
+
|
|
78
|
+
return Record(
|
|
79
|
+
id=str(id_value) if id_value is not None else _content_id(raw),
|
|
80
|
+
language=str(language_value) if language_value else config.languages.source,
|
|
81
|
+
fields=fields,
|
|
82
|
+
translations=translations if isinstance(translations, dict) else {},
|
|
83
|
+
metadata=metadata,
|
|
84
|
+
source=raw if cfg.keep_source else None,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _flatten(
|
|
89
|
+
value: Any,
|
|
90
|
+
prefix: str,
|
|
91
|
+
config: InputConfig,
|
|
92
|
+
reserved: set[str],
|
|
93
|
+
fields: dict[str, str],
|
|
94
|
+
metadata: dict[str, Any],
|
|
95
|
+
) -> None:
|
|
96
|
+
if prefix and matches(prefix, reserved):
|
|
97
|
+
return
|
|
98
|
+
if prefix and matches(prefix, set(config.exclude_paths)):
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
if isinstance(value, dict):
|
|
102
|
+
for key, item in value.items():
|
|
103
|
+
path = f"{prefix}.{key}" if prefix else key
|
|
104
|
+
_flatten(item, path, config, reserved, fields, metadata)
|
|
105
|
+
return
|
|
106
|
+
|
|
107
|
+
if isinstance(value, list):
|
|
108
|
+
if config.array_mode == ArrayMode.JOIN:
|
|
109
|
+
if value and all(isinstance(item, str) for item in value):
|
|
110
|
+
text = " ".join(item for item in value if item.strip())
|
|
111
|
+
if text:
|
|
112
|
+
fields[prefix] = text
|
|
113
|
+
elif value:
|
|
114
|
+
metadata[prefix] = value
|
|
115
|
+
return
|
|
116
|
+
if config.array_mode == ArrayMode.FIRST:
|
|
117
|
+
if value:
|
|
118
|
+
_flatten(value[0], prefix, config, reserved, fields, metadata)
|
|
119
|
+
return
|
|
120
|
+
for index, item in enumerate(value):
|
|
121
|
+
_flatten(item, f"{prefix}[{index}]", config, reserved, fields, metadata)
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
if config.field_paths and not matches(prefix, set(config.field_paths)):
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
if isinstance(value, str):
|
|
128
|
+
if value.strip():
|
|
129
|
+
fields[prefix] = value
|
|
130
|
+
elif value is not None:
|
|
131
|
+
metadata[prefix] = value
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _content_id(raw: Any) -> str:
|
|
135
|
+
canonical = json.dumps(raw, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
|
|
136
|
+
digest = hashlib.sha256(canonical.encode("utf-8")).hexdigest()[:12]
|
|
137
|
+
return f"doc-{digest}"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Adapter selection by configured or inferred format."""
|
|
2
|
+
|
|
3
|
+
from thesaurus.adapters.base import InputAdapter
|
|
4
|
+
from thesaurus.adapters.csv import CsvAdapter
|
|
5
|
+
from thesaurus.adapters.json import JsonAdapter
|
|
6
|
+
from thesaurus.config import CompilerConfig
|
|
7
|
+
from thesaurus.types import InputFormat
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AdapterRegistry:
|
|
11
|
+
"""Resolve the adapter for a configuration."""
|
|
12
|
+
|
|
13
|
+
def __init__(self) -> None:
|
|
14
|
+
self._adapters: dict[InputFormat, InputAdapter] = {
|
|
15
|
+
InputFormat.JSON: JsonAdapter(),
|
|
16
|
+
InputFormat.JSONL: JsonAdapter(),
|
|
17
|
+
InputFormat.CSV: CsvAdapter(),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def get(self, config: CompilerConfig) -> InputAdapter:
|
|
21
|
+
if config.input.path is None:
|
|
22
|
+
raise ValueError("input.path is required when records are not provided")
|
|
23
|
+
fmt = config.input.format
|
|
24
|
+
if fmt == InputFormat.AUTO:
|
|
25
|
+
suffix = config.input.path.suffix.lower().lstrip(".")
|
|
26
|
+
try:
|
|
27
|
+
fmt = InputFormat(suffix)
|
|
28
|
+
except ValueError:
|
|
29
|
+
fmt = InputFormat.JSON
|
|
30
|
+
if fmt == InputFormat.AUTO:
|
|
31
|
+
fmt = InputFormat.JSON
|
|
32
|
+
return self._adapters[fmt]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""HTTP APIs for build and serving planes."""
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Build API: artifact generation jobs."""
|
|
2
|
+
|
|
3
|
+
from thesaurus.api.build.app import BuildSettings, create_build_app
|
|
4
|
+
from thesaurus.api.build.jobs import BuildJob, JobStore
|
|
5
|
+
from thesaurus.api.build.servers import ServerManager
|
|
6
|
+
|
|
7
|
+
__all__ = ["BuildJob", "BuildSettings", "JobStore", "ServerManager", "create_build_app"]
|