"""Search a downloaded RENDA index. Python 3.9+, standard library only.""" import argparse import json import re import sqlite3 import sys from contextlib import closing from pathlib import Path def main(): parser = argparse.ArgumentParser(description=__doc__) query = parser.add_mutually_exclusive_group(required=True) query.add_argument("--code", help='Stable identifier, e.g. "FC 07" or "S04"') query.add_argument("--text", help="One or more words, matched in the same passage") parser.add_argument("--lang", type=str.upper, choices=["RO", "EN", "LA"]) parser.add_argument("--kind", choices=["codex", "standard"]) parser.add_argument("--limit", type=int, default=10) parser.add_argument("--db", type=Path, default=Path(__file__).with_name("renda.sqlite")) args = parser.parse_args() if not 1 <= args.limit <= 500: parser.error("--limit must be between 1 and 500") if args.code is not None: match = re.fullmatch(r"#?\[?(FC|PA|TCR|VC|VH|CL|FS|S)[\s-]*(\d{1,2})\]?(?:[\s-]+(EN|RO|LA))?", args.code.strip().upper()) if not match: parser.error("Expected FC 7, FC07-EN, tCR05 or S4") family, number, suffix = match.groups() if suffix and args.lang and suffix != args.lang: parser.error("The language suffix and --lang disagree") args.lang = suffix or args.lang value = family + number.zfill(2) source = "passages p" tests = ["p.lookup = ?"] params = [value] order = "p.id" else: terms = re.findall(r"[^\W_]+", args.text[:500], flags=re.UNICODE) if not terms or len(args.text) > 500: parser.error("Enter 1 to 500 characters containing at least one word") # Terms are quoted literals in the FTS grammar. SQL uses bound parameters. value = " AND ".join('"' + term + '"' for term in terms) source = "search JOIN passages p ON p.id = search.rowid" tests = ["search MATCH ?"] params = [value] order = "bm25(search), p.id" if args.lang in ("RO", "LA"): parser.exit(2, "The full English corpus is indexed here. Romanian and Latin: https://doi.org/10.5281/zenodo.17012491\n") for field, value in (("lang", args.lang), ("kind", args.kind)): if value: tests.append("p." + field + " = ?") params.append(value) sql = ("SELECT p.code, p.lang, p.title, p.body, p.href FROM " + source + " WHERE " + " AND ".join(tests) + " ORDER BY " + order + " LIMIT ?") params.append(args.limit) try: uri = args.db.resolve().as_uri() + "?mode=ro" with closing(sqlite3.connect(uri, uri=True)) as db: db.row_factory = sqlite3.Row db.execute("PRAGMA query_only=ON") rows = [dict(row) for row in db.execute(sql, params)] title_note = db.execute("SELECT value FROM metadata WHERE name = 'pa_title_provenance'").fetchone() if title_note: for row in rows: if row["code"].startswith("PA "): row["title_note"] = title_note[0] except sqlite3.Error as exc: parser.exit(2, "Index unavailable. Full-text search requires SQLite FTS5. " + str(exc) + "\n") if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8") print(json.dumps(rows, ensure_ascii=False, indent=2)) if __name__ == "__main__": main()