import os
import joblib
from embeddings.vectorizer import build_vectorizer

# Get absolute path to project root
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

RAW_PATH = os.path.join(BASE_DIR, "raw.txt")
ARTIFACTS_DIR = os.path.join(BASE_DIR, "artifacts")
OUT_PATH = os.path.join(ARTIFACTS_DIR, "vectorizer.joblib")


def load_raw_text(path):
    with open(path, "r", encoding="utf-8") as f:
        return [line.strip() for line in f if line.strip()]


def main():
    texts = load_raw_text(RAW_PATH)

    vectorizer = build_vectorizer()
    vectorizer.fit(texts)

    # Ensure artifacts directory exists
    os.makedirs(ARTIFACTS_DIR, exist_ok=True)

    joblib.dump(vectorizer, OUT_PATH)
    print(f"Vectorizer fitted and saved to {OUT_PATH}")


if __name__ == "__main__":
    main()
