import numpy as np
import data_handle.DataProcessing
import AttBiLSTM_FK
import AttHBiLSTM_FK
import EAttHBiLSTM_FK

if(__name__ == "__main__"):
    batchSize = 331
    numClasses = 2
    numDimensions = 200
    keepratio_cnn_ = 0.5
    input_keep_prob_ = 0.5
    output_keep_prob_ = 0.5
    cross_multiple = 5
    iterations = 2000

    wordModel = data_handle.DataProcessing.DataProcessing('D:/SemEval2019-Task6/workspace/wordModel/glove.twitter.27B.200d.txt',
                                                          'D:/SemEval2019-Task6/workspace/data/train/offenseval-training-v1.tsv',
                                                          'D:/SemEval2019-Task6/workspace/wordModel/emoji2vec200.txt',
                                                          'D:/SemEval2019-Task6/workspace/wordModel/SwearWord2vec.txt',
                                                          numDimensions, eab=2)
    # wordModel = data_handle.DataProcessing.DataProcessing('/home/yjzhang_xu/workspace/WordVecModel/dataset/glove.twitter.27B.200d.txt',
    #                                                       '/home/yjzhang_xu/workspace/SE2019T6WS/data/train/offenseval-training-v1.tsv',
    #                                                       '/home/yjzhang_xu/workspace/WordVecModel/dataset/emoji2vec200.txt',
    #                                                       '/home/yjzhang_xu/workspace/WordVecModel/dataset/SwearWord2vec.txt',
    #                                                       numDimensions, eab=2)

    for i in range(cross_multiple):
        # AttBiLSTM_FK.AttBiLSTMRun(wordModel, numClasses, input_keep_prob_, output_keep_prob_, i, iterations)
        # with open("../trained_model/log_attbilstm.txt", "a", encoding="utf-8") as f:
        #     f.write("\r\n")
        #     f.close()
        # AttHBiLSTM_FK.AttHBiLSTMRun(wordModel, numClasses, input_keep_prob_, output_keep_prob_, i, iterations)
        # with open("../trained_model/log_atthbilstm.txt", "a", encoding="utf-8") as f:
        #     f.write("\r\n")
        #     f.close()
        EAttHBiLSTM_FK.EAttHBiLSTMRun(wordModel, numClasses, input_keep_prob_, output_keep_prob_, i, iterations)
        with open("../trained_model/log_eatthbilstm.txt", "a", encoding="utf-8") as f:
            f.write("\r\n")
            f.close()

    # for i in range(cross_multiple):
    #     pred = []
    #     j = 0
    #     while j < 7:
    #         pre, max_macro_f1 = AttHBiLSTM_FK.AttHBiLSTMRun(wordModel, numClasses, input_keep_prob_, output_keep_prob_, i, epoch, j)
    #         if max_macro_f1 > 0.52:
    #             j += 1
    #             pred.append(pre)
    #     for k in range(6):
    #         pred[0] += pred[k+1]
    #     pred[0] /= 7
    #
    #     Tnot, Fnot, Toff, Foff = 0., 0., 0., 0.
    #     _, testBatchLabels = wordModel.getTrainBatch3(verify=True, cross_deviation=i, model_deviation=0)
    #     pc = np.argmax(pred[0], 1)
    #     for k in range(len(pc)):
    #         if testBatchLabels[k][0] == 0 and pc[k] == 1:
    #             Tnot += 1
    #         elif testBatchLabels[k][0] == 0 and pc[k] == 0:
    #             Fnot += 1
    #         elif testBatchLabels[k][0] == 1 and pc[k] == 0:
    #             Toff += 1
    #         elif testBatchLabels[k][0] == 1 and pc[k] == 1:
    #             Foff += 1
    #
    #     if Tnot == 0 or Fnot == 0 or Toff == 0 or Foff == 0:
    #         print("Tnot:" + str(Tnot) + "  Fnot:" + str(Fnot) + "  Toff:" + str(Toff) + "   Foff:" + str(Foff))
    #         with open("../trained_model/log_atthbilstm.txt", "a", encoding="utf-8") as f:
    #             f.write("Tnot:" + str(Tnot) + "  Fnot:" + str(Fnot) + "  Toff:" + str(Toff) + "   Foff:" + str(Foff) + "\r\n")
    #             f.close()
    #     else:
    #         macro_precision_not = Tnot / (Tnot + Fnot)
    #         macro_precision_off = Toff / (Toff + Foff)
    #         macro_recall_not = Tnot / (Tnot + Foff)
    #         macro_recall_off = Toff / (Toff + Fnot)
    #         macro_f1_not = (2 * macro_precision_not * macro_recall_not) / (macro_precision_not + macro_recall_not)
    #         macro_f1_off = (2 * macro_precision_off * macro_recall_off) / (macro_precision_off + macro_recall_off)
    #         mean_macro_accuracy = (Tnot + Toff) / (Tnot + Fnot + Toff + Foff)
    #         mean_macro_precision = (macro_precision_not + macro_precision_off) / 2
    #         mean_macro_recall = (macro_recall_not + macro_recall_off) / 2
    #         mean_macro_f1 = (macro_f1_not + macro_f1_off) / 2
    #
    #         print("deviation {}/{}   ".format(i + 1, cross_multiple),
    #               "F1 {}   ".format(mean_macro_f1),
    #               "accuracy {}   ".format(mean_macro_accuracy),
    #               "precision {}   ".format(mean_macro_precision),
    #               "recall {}   ".format(mean_macro_recall))
    #         with open("../trained_model/log_atthbilstm.txt", "a", encoding="utf-8") as f:
    #             f.write("deviation " + str(i + 1) + "/" + str(cross_multiple) +
    #                     "   F1 " + str(mean_macro_f1) +
    #                     "   accuracy " + str(mean_macro_accuracy) +
    #                     "   precision " + str(mean_macro_precision) +
    #                     "   recall " + str(mean_macro_recall) + "\r\n")
    #             f.close()
