import json
import random
import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report

from xgboost import XGBClassifier




train_percentage = 0.01 #


# ==============================
# 1. Load Dataset
# ==============================

with open("c:\greek_bullying_dataset_realistic_4000.json", "r", encoding="utf-8") as f:
    data = json.load(f)

df = pd.DataFrame(data)

# ==============================
# 2. Label Encoding
# ==============================

df["label"] = df["label"].map({
    "non_toxic": 0,
    "toxic": 1
})

texts = df["text"]
labels = df["label"]


# ==============================
# 3. Train/Test Split
# ==============================

X_train, X_test, y_train, y_test = train_test_split(
    texts,
    labels,
    test_size=0.2,
    random_state=112,
    stratify=labels
)



# ==============================
# 4. Train percentage control
# ==============================


subset_size = int(len(X_train) * train_percentage)

X_train = X_train.iloc[:subset_size]
y_train = y_train.iloc[:subset_size]

print("Training samples:", len(X_train))

# ==============================
# 5. Vectorization
# ==============================

count_vectorizer = CountVectorizer()

X_train_count = count_vectorizer.fit_transform(X_train)
X_test_count = count_vectorizer.transform(X_test)




# ==============================
# 7. XGBoost Training
# ==============================

print("\n---- XGBoost (Count Vectorizer) ----")

xgb = XGBClassifier(n_estimators=100,max_depth=5,learning_rate=0.1,eval_metric="logloss",use_label_encoder=False)

xgb.fit(X_train_count, y_train)

pred_xgb = xgb.predict(X_test_count)

print("Accuracy:", accuracy_score(y_test, pred_xgb))

toPredict=count_vectorizer.transform(["δεν είσαι βλάκας"])

print(xgb.predict(toPredict))




