Files
ThothII/harness/tht/cli/lsh_cmd.py
T
marcopan 91a374492c feat(harness): port sql/cte/datamart/lsh cmd (Onda 4) — CLI completa F1→F8
Ultima onda CLI. 4 cmd portati con rename + grep-per-file (3 residui nsp nei messaggi
fixati). Nessun drift costanti phase in questi cmd.

La CLI tht e' ora COMPLETA: 14 gruppi di comandi (phase config schema session vector
memory search evidence db decision sql cte datamart lsh). tht --help li list tutti.
Suite: 165 passed.

Il loop skill->LLM->gate ora ha tutti i comandi che la skill chiamera'. Resta:
skill riscritta (S), setup pre-sessione (0b), sessione L2 manuale.
2026-06-27 14:16:35 +02:00

98 lines
3.7 KiB
Python

from pathlib import Path
import typer
from tht.cli.config_cmd import CONFIG_OPT
from tht.cli.schema_cmd import _load_config_or_exit, physical_path
lsh_app = typer.Typer(help="Indice LSH su valori dei campi (derivato, rigenerabile)")
def _lsh_dir(cfg) -> Path:
return cfg.paths.indexes / "lsh"
@lsh_app.command("build")
def build_cmd(config: Path = CONFIG_OPT) -> None:
"""Costruisce l'indice LSH dai valori del database e lo salva su pickle."""
from tht.lshindex import build_index, save_index
from tht.mschema.models import Annotations, PhysicalSchema
cfg = _load_config_or_exit(config)
phys_file = physical_path(cfg)
if not phys_file.exists():
typer.secho(
f"ERRORE: {phys_file} non trovato. Esegui prima `tht schema introspect`.",
fg=typer.colors.RED, err=True,
)
raise typer.Exit(code=1)
physical = PhysicalSchema.from_yaml(phys_file)
from tht.cli.schema_cmd import annotations_path
annotations = Annotations.from_yaml(annotations_path(cfg))
typer.echo("Estrazione valori (i più frequenti) dalle colonne testuali eligible...")
if cfg.database.transport == "rest":
from tht.db.sampling import unique_values_for_lsh_rest
from tht.rest.client import RestClient
values, skipped, truncated = unique_values_for_lsh_rest(
RestClient(cfg.rest), physical, cfg.lsh, annotations
)
else:
from tht.db.connection import make_engine
from tht.db.sampling import unique_values_for_lsh
values, skipped, truncated = unique_values_for_lsh(
make_engine(cfg.database), physical, cfg.lsh, annotations
)
n_values = sum(len(v) for t in values.values() for v in t.values())
typer.echo(f" {n_values} valori da {sum(len(t) for t in values.values())} colonne")
for s in skipped:
typer.secho(f" saltata {s.table}.{s.column}: {s.reason}", fg=typer.colors.YELLOW)
for t in truncated:
typer.secho(
f" troncata {t.table}.{t.column}: indicizzati i {t.indexed} valori più frequenti "
f"(limite max_values_per_column raggiunto; altri valori distinti NON indicizzati)",
fg=typer.colors.YELLOW,
)
lsh, minhashes = build_index(values, cfg.lsh, verbose=True)
save_index(lsh, minhashes, cfg.lsh, _lsh_dir(cfg), name=cfg.database.db_schema)
typer.secho(
f"OK: indice LSH ({len(minhashes)} entry) -> {_lsh_dir(cfg)}", fg=typer.colors.GREEN
)
@lsh_app.command("query")
def query_cmd(
keyword: str = typer.Argument(..., help="Termine da cercare, es. 'ablazione'."),
config: Path = CONFIG_OPT,
top: int = typer.Option(10, "--top", help="Numero massimo di risultati."),
) -> None:
"""Probe visuale: mostra i candidati LSH per un termine, con score Jaccard."""
from rich.console import Console
from rich.table import Table
from tht.lshindex import LshIndexError, load_index, query_index
cfg = _load_config_or_exit(config)
try:
lsh, minhashes, meta = load_index(_lsh_dir(cfg), name=cfg.database.db_schema)
except LshIndexError as e:
typer.secho(f"ERRORE: {e}", fg=typer.colors.RED)
raise typer.Exit(code=1)
hits = query_index(lsh, minhashes, keyword, meta, top_n=top)
if not hits:
typer.secho(f"Nessun candidato LSH per '{keyword}'.", fg=typer.colors.YELLOW)
return
table = Table(title=f"Candidati LSH per '{keyword}' ({len(hits)})")
table.add_column("Tabella")
table.add_column("Colonna")
table.add_column("Valore")
table.add_column("Score", justify="right")
for h in hits:
table.add_row(h.table, h.column, h.value, f"{h.score:.3f}")
Console().print(table)