71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from rich import print
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
|
|
from scripts.common import DB_FILE
|
|
from scripts.search_utils import search_database
|
|
|
|
|
|
def print_results(query: str, mode: str, results: list[dict]) -> None:
|
|
print(f"[bold]Dopyt:[/bold] {query}")
|
|
print(f"[bold]Režim:[/bold] {mode}")
|
|
print(f"[bold]Počet výsledkov:[/bold] {len(results)}")
|
|
print("\n[bold]Top výsledky:[/bold]\n")
|
|
|
|
for rank, item in enumerate(results, start=1):
|
|
print(f"[cyan]{rank}. Skóre: {item['score']}[/cyan]")
|
|
print(f"[bold]Názov:[/bold] {item['title']}")
|
|
print(f"[bold]Cesta:[/bold] {item['document_path']}")
|
|
print(f"[bold]URL:[/bold] {item['source_url']}")
|
|
print(f"[bold]Chunk:[/bold] {item['chunk_index']}")
|
|
print(f"[bold]Kategórie:[/bold] {item['categories']}")
|
|
print(f"[bold]Tagy:[/bold] {item['tags']}")
|
|
print(f"[bold]Autor:[/bold] {item['author']}")
|
|
print("[bold]Text:[/bold]")
|
|
print((item["text"] or "")[:700])
|
|
print("-" * 80)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(
|
|
description="Vyhľadávanie v SQLite indexe zpwiki."
|
|
)
|
|
|
|
parser.add_argument(
|
|
"query",
|
|
nargs="+",
|
|
help="Text, ktorý sa má vyhľadať.",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--limit",
|
|
type=int,
|
|
default=10,
|
|
help="Počet výsledkov.",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
query = " ".join(args.query)
|
|
|
|
try:
|
|
mode, results = search_database(DB_FILE, query, args.limit)
|
|
except FileNotFoundError as error:
|
|
raise SystemExit(str(error)) from error
|
|
|
|
print_results(query, mode, results)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|