#!/usr/bin/env python3
"""Compare exact, phrase and spelling-tolerant archive searches.

Run: python3 archive-search.py
Requires Python 3 with SQLite FTS5. Uses an in-memory database only.
The invented English corpus illustrates search behavior, not client results.
Fuzzy expansion uses ordinary Levenshtein distance, not Solr query semantics.
"""

import sqlite3
import sys


CORPUS = {
    "A": "The canal opened in 1890. The canal carried coal.",
    "B": "The canai carried coal.",
    "C": "The canals carried coal.",
    "D": "The cabal met at dusk.",
    "E": "The ca-\nnal carried coal.",
    "F": "Coal moved along the canal.",
}


def edit_distance(left, right):
    """Unit-cost insertion, deletion and substitution; no transposition rule."""
    previous = list(range(len(right) + 1))
    for i, a in enumerate(left, 1):
        current = [i]
        for j, b in enumerate(right, 1):
            current.append(min(current[-1] + 1, previous[j] + 1,
                               previous[j - 1] + (a != b)))
        previous = current
    return previous[-1]


def main():
    db = sqlite3.connect(":memory:")
    try:
        # No stemming, synonyms or diacritic removal. The corpus is ASCII.
        db.execute("CREATE VIRTUAL TABLE pages USING fts5("
                   "id UNINDEXED, body, tokenize='unicode61 remove_diacritics 0')")
    except sqlite3.OperationalError as error:
        raise SystemExit("This example requires SQLite with FTS5 enabled: "
                         + str(error)) from error
    db.executemany("INSERT INTO pages(id, body) VALUES (?, ?)", CORPUS.items())
    db.execute("CREATE VIRTUAL TABLE vocab USING fts5vocab(pages, 'row')")
    db.execute("CREATE VIRTUAL TABLE positions USING fts5vocab(pages, 'instance')")

    def documents(query):
        return [row[0] for row in db.execute(
            "SELECT id FROM pages WHERE pages MATCH ? ORDER BY id", (query,))]

    def occurrences(terms):
        # Count indexed term positions, not matching documents or substrings.
        return sum(db.execute("SELECT count(*) FROM positions WHERE term = ?",
                              (term,)).fetchone()[0] for term in terms)

    term = "canal"
    expanded = [row[0] for row in db.execute("SELECT term FROM vocab ORDER BY term")
                if edit_distance(term, row[0]) <= 1]
    # All terms come from the fixed corpus; quote each as an FTS5 string.
    fuzzy_query = " OR ".join('"' + word.replace('"', '""') + '"'
                              for word in expanded)
    exact_docs = documents('"canal"')
    phrase_docs = documents('"canal carried"')
    fuzzy_docs = documents(fuzzy_query)
    exact_count = occurrences([term])
    fuzzy_count = occurrences(expanded)

    # Fixed expectations encode the distinction the guide teaches.
    checks = [
        (exact_docs == ["A", "F"], "exact document IDs"),
        (exact_count == 3, "exact token occurrences"),
        (phrase_docs == ["A"], "phrase document IDs"),
        (expanded == ["cabal", "canai", "canal", "canals"], "expanded terms"),
        (fuzzy_docs == ["A", "B", "C", "D", "F"], "fuzzy document IDs"),
        (fuzzy_count == 6, "expanded token occurrences"),
        ("E" not in fuzzy_docs, "broken word remains missed"),
    ]
    for passed, label in checks:
        if not passed:
            raise AssertionError(label)

    print("Python", sys.version.split()[0], "/ SQLite", sqlite3.sqlite_version)
    for label, body in CORPUS.items():
        print(label + ":", body.replace("\n", " / "))
    print("Exact canal: documents=" + ",".join(exact_docs),
          "occurrences=" + str(exact_count))
    print('Phrase "canal carried": documents=' + ",".join(phrase_docs))
    print("Expanded terms:", ", ".join(expanded))
    print("Fuzzy <=1: documents=" + ",".join(fuzzy_docs),
          "occurrences=" + str(fuzzy_count))
    print("PASS: 7 checks; B recovered, D false positive, E still missed.")
    db.close()


if __name__ == "__main__":
    main()
