import os
import sys

sys.stdout.reconfigure(encoding='utf-8')

keywords = ['của tà', 'gia đình', 'gia định', 'của ta']
root = r'C:\Projects\Historical'

print("Searching...")
for dirpath, dirnames, filenames in os.walk(root):
    if any(p in dirpath for p in ['venv', '.git', '__pycache__', 'node_modules']):
        continue
    for fname in filenames:
        ext = os.path.splitext(fname)[1].lower()
        if ext in ['.srt', '.vtt', '.txt', '.json', '.md', '.py', '.js', '.html']:
            fpath = os.path.join(dirpath, fname)
            try:
                with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
                    content = f.read()
                for kw in keywords:
                    if kw in content.lower():
                        count = content.lower().count(kw)
                        print(f"{fpath}: found '{kw}' ({count} times)")
            except Exception as e:
                pass
print("Done search.")
