import argparse import re from pathlib import Path def words(text): return set(re.findall(r"[a-z0-9]+", text.lower())) def main(): parser = argparse.ArgumentParser(description="Search notes with line citations") parser.add_argument("notes") parser.add_argument("question", nargs="+") args = parser.parse_args() file = Path(args.notes) try: lines = file.read_text(encoding="utf-8-sig").splitlines() except (OSError, UnicodeError): parser.exit(2, "Cannot read the notes as UTF-8 text.\n") query = words(" ".join(args.question)) ranked = [] for number, line in enumerate(lines, 1): tokens = words(line) score = len(query & tokens) / max(1, len(query | tokens)) if score > 0: ranked.append((score, number, line)) ranked.sort(key=lambda row: (-row[0], row[1])) if not ranked: print("No supporting passage found in these notes.") for score, number, line in ranked[:3]: print(f"[{file.name}:{number}] {line}") if __name__ == "__main__": main()