import csv
import re
from pathlib import Path


def bigrams(text):
    text = re.sub(r"[^\u4e00-\u9fffA-Za-z0-9]", "", text.lower())
    return {text[i:i+2] for i in range(len(text) - 1)}


def search(rows, question, user, limit=3):
    query_terms = bigrams(question)
    ranked = []
    for row in rows:
        if row['current'] != '1' or row['scope'] not in ('shared', user):
            continue
        doc_terms = bigrams(row['title'] + row['body'])
        overlap = len(query_terms & doc_terms)
        if overlap:
            score = overlap / max(1, len(query_terms | doc_terms))
            citation = f"{row['source']}@{row['rev']}#{row['anchor']}"
            ranked.append((score, citation))
    return sorted(ranked, key=lambda item: (-item[0], item[1]))[:limit]


if __name__ == '__main__':
    with Path(__file__).with_name('corpus.psv').open(encoding='utf-8') as handle:
        corpus = list(csv.DictReader(handle, delimiter='|'))
    assert len(corpus) == 12
    print(search(corpus, '常规微课总长多久？', 'alpha'))
