import utils

from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials

data = utils.read_data()
X = utils.combined_embeddings(data.text.tolist())
y = data.sentiment


def hyperopt_train_SVC(params):
    clf = SVC(decision_function_shape='ovo', probability=True, **params)
    return cross_val_score(clf, X, y).mean()

space = {
    'C': hp.uniform('C', 0, 10),
    'kernel': hp.choice('kernel', ['linear', 'sigmoid', 'rbf']),
    'gamma': hp.uniform('gamma', 0, 10)
}

def f(params):
    acc = hyperopt_train_SVC(params)
    return {
        'loss': -acc,
        'status': STATUS_OK
    }

trials = Trials()
best = fmin(
    f,
import utils
import pickle

from os.path import isfile

from sklearn.ensemble import ExtraTreesClassifier as ETC
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split

filename = '/usr/src/app/sentiment/models/pickles/BernoulliNB.pickle'

if isfile(filename) == False:

    train, test = train_test_split(utils.read_data(), test_size=0.2)

    train_embeddings = utils.combined_embeddings(train['text'].tolist())
    test_embeddings = utils.combined_embeddings(test['text'].tolist())

    clf = ETC(n_estimators=100)
    clf.fit(train_embeddings, train['sentiment'])

    prediction = clf.predict(test_embeddings)
    report = classification_report(test['sentiment'], prediction)
    print(report)

    with open(filename, 'wb') as f:
        pickle.dump(clf, f)

else:
    print('Already Trained!')