# -- Code Cell --
import pandas as pd
train = pd.read_csv("train_data.csv")
test = pd.read_csv("test_data.csv")
train

# -- Code Cell --
text_train_task1 = train[train['is_food'] != -1][['text_en']] 
labels = train[train['is_food'] != -1]['is_food']
text_train_task1,labels

# -- Code Cell --
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
model = CatBoostClassifier(text_features=['text_en'])
X = text_train_task1
X_train, X_test, y_train, y_test = train_test_split(X,labels, test_size=0.2, stratify=labels)
model.fit(X_train, y_train)
pred = model.predict(X_test)
f1 = f1_score(y_test,pred)
f1

# -- Code Cell --
test_text_task1 = test[['text_en']]
pred = model.predict(test_text_task1)

# -- Markdown Cell --
# ### Strategie
# ##### Iau din nlp_train toate textele cu label -1 adica care au descriere eng si esp fac tfidf pe ambele, tratez eng ca label si esp ca feature, bag un ridge ca sa iau legatura semantica, si dupa fac tfidf pe querry care e esp dau predict la el cu ridge si dupa tfidf pe gallery(care e text eng) si compar asta cu predictiile de la ridge

# -- Code Cell --
nlp_train = pd.read_csv("nlp_train.csv")
nlp_test_queries = pd.read_csv("./nlp_test_queries.csv")
nlp_test_gallery = pd.read_csv("./nlp_test_gallery.csv")

# -- Code Cell --
nlp_train[nlp_train['is_food'] == -1]

# -- Code Cell --
text_task2_esp = nlp_train[nlp_train['is_food'] == -1]['text_es']
text_task2_en = nlp_train[nlp_train['is_food'] == -1]['text_en']

# -- Code Cell --
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf= TfidfVectorizer(ngram_range=(1,2))
text_task2_esp_tf = tfidf.fit_transform(text_task2_esp)
text_task2_en_tf = tfidf.transform(text_task2_en)

# -- Code Cell --
text_task2_esp_tf = text_task2_esp_tf.toarray()
text_task2_en_tf = text_task2_en_tf.toarray()

# -- Code Cell --
from sklearn.linear_model import Ridge
model = Ridge(alpha = 1)
model.fit(text_task2_esp_tf,text_task2_en_tf)

# -- Code Cell --
text_task2_querry_esp = nlp_test_queries['query_text_es']
text_task2_querry_esp_tf = tfidf.transform(text_task2_querry_esp)
text_task2_querry_esp_tf = text_task2_querry_esp_tf.toarray()
predictions = model.predict(text_task2_querry_esp_tf)

# -- Code Cell --
text_task2_gallery_eng = nlp_test_gallery['text_en']
text_task2_gallery_eng_tf = tfidf.transform(text_task2_gallery_eng)
text_task2_gallery_eng_tf = text_task2_gallery_eng_tf.toarray()

# -- Code Cell --
from sklearn.metrics.pairwise import cosine_similarity,euclidean_distances

# -- Code Cell --
import numpy as np
sims = cosine_similarity(predictions, text_task2_gallery_eng_tf)
best_indexies = np.argmax(sims, axis=1)
best_indexies

# -- Code Cell --
answer = []
for i in range(len(best_indexies)):
    answer.append(nlp_test_gallery['candidate_id'].iloc[best_indexies[i]])

# -- Code Cell --
len(pred), len(answer)

# -- Code Cell --
rows = []
for idx in range(len(answer)):
    if idx<400:
        rows.append({"subtaskID":1,"datapointID":test['datapoint_id'][idx], "answer":pred[idx]})
        rows.append({'subtaskID':2,'datapointID':nlp_test_queries['query_id'][idx], 'answer':answer[idx]})
    else:
        rows.append({'subtaskID':2,'datapointID':nlp_test_queries['query_id'][idx], 'answer':answer[idx]})

# -- Code Cell --
print("")

# -- Code Cell --
from scipy.spatial.distance import cdist
D = cdist(predictions,text_task2_gallery_eng_tf,metric='euclidean')
n_preds, _ = D.shape
preds = np.full(n_preds,-1,dtype=int)
used = set()
for i in range(n_preds):
    indexies = np.argsort(D[i])
    for idx in indexies:
        if idx not in used:
            preds[i] = idx
            used.add(idx)
            break

# -- Code Cell --
df = pd.DataFrame(rows)
df.to_csv("subs.csv",index=False)

# -- Markdown Cell --
# # NU MAI FACE NICIODATA SEARCH DE MANA LA COSINE_SIM SAU EUCLIDEAN_DISTANCE E COMPLEXITATE N^2

# -- Code Cell --
# best_indexies = []
# for idx,querry in enumerate(predictions):
#     best_index = -67
#     best_sim = -67
#     print(idx)
#     for index,gallery in enumerate(text_task2_gallery_eng_tf):
#         sim = cosine_similarity(querry.reshape(1,-1), gallery.reshape(1,-1))[0][0]
#         if sim>best_sim:
#             best_sim = sim
#             best_index= index
#     best_indexies.append(index)

# -- Code Cell --
