import os
import re

root_dir = "/Users/university/Documents/aprender nuevas cosas/daniela"
keywords = [
    r"hotwife", r"cuckold", r"cuckolding", r"watching", r"cornudo", r"compartir", 
    r"esposo mirando", r"marido mirando", r"toro", r"tercero", r"armario", r"closet"
]

results = []

for root, dirs, files in os.walk(root_dir):
    # Skip python cache and .claude directories
    if "__pycache__" in root or ".claude" in root:
        continue
    for file in files:
        if file.endswith((".txt", ".md")) and file != "xgifer-list.md" and file != "concordancias_360.txt" and file != "indice_alfabetico.txt" and file != "compare.py" and file != "search_hotwife.py":
            file_path = os.path.join(root, file)
            try:
                with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
                    for line_num, line in enumerate(f, 1):
                        for kw in keywords:
                            if re.search(r"\b" + kw + r"\b", line, re.IGNORECASE):
                                # Check if it's actually in a context about cuckold/hotwife
                                results.append({
                                    "file": os.path.relpath(file_path, root_dir),
                                    "line": line_num,
                                    "keyword": kw,
                                    "content": line.strip()
                                })
                                break # match only once per line
            except Exception as e:
                pass

print(f"Total matches found: {len(results)}")
# Group matches by file to keep it clean
by_file = {}
for r in results:
    by_file.setdefault(r["file"], []).append(r)

for file, matches in sorted(by_file.items()):
    print(f"\nFile: {file} ({len(matches)} matches)")
    # Show first 15 matches to keep output size readable
    for m in matches[:25]:
        print(f"  Line {m['line']} (KW: {m['keyword']}): {m['content']}")
    if len(matches) > 25:
        print(f"  ... and {len(matches) - 25} more matches in this file.")
