import csv
import os
import re

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
RAW_PATH = os.path.join(BASE_DIR, "raw.txt")
OUT_PATH = os.path.join(BASE_DIR, "data", "questioning.csv")

QUESTION_WORDS = [
    "what", "why", "how", "when", "where",
    "who", "whom", "which", "is", "are",
    "do", "does", "did", "can", "could",
    "should", "would"
]


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 label_text(text):
    t = text.lower()

    if t.endswith("?"):
        return 1

    for qw in QUESTION_WORDS:
        if re.match(rf"^{qw}\b", t):
            return 1

    return 0

def load_extra_questions(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)

    # add synthetic questioning samples
    extra_path = os.path.join(BASE_DIR, "data", "questioning_extra.txt")
    extra_questions = load_extra_questions(extra_path)

    with open(OUT_PATH, "w", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow(["text", "label"])

        for text in texts:
            writer.writerow([text, label_text(text)])

        # force class-1 presence
        for q in extra_questions:
            writer.writerow([q, 1])

    print(f"Questioning dataset written to {OUT_PATH}")


if __name__ == "__main__":
    main()
