from lib import *
#from keras.layers.merge import Concatenate
from keras.layers import Merge
import copy
from collections import Counter
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
import math

word2topic = pickle.load(open("word2topic", "r"))
embedding = pickle.load(open("word2topic", "r"))

#embedding = pickle.load(open("embedding", "r"))
vocab = word2topic.keys()
max_words = 25#30
depth_embed = 100#370
depth_distance = 100#368#70#100

##def getEmbedding_word2vec(sentence, model):
#    
#    global max_words, depth, no_features, train_length
##    model = model[0]
#    list = np.array([])
#    for word in sentence:
#        if word in model.wv.vocab:
#             list = np.append(list, model.wv[word])
#    
#    #print list.size
#    if(list.size > depth*max_words):
#        list = list[0:depth*max_words]
#    #print sentence
#    pad = np.zeros(depth*max_words - list.size)
#    list = np.append(list, pad)
#    #print list.shape
#    return list
    
def get_topic_rep(topic, word2topic, word2vec):

    global vocab
    topics = str(topic).split(' ')
    v = np.zeros(word2topic['donald'].shape)#(np.concatenate((word2topic['donald'], word2vec.wv['donald'])).shape)
    counter = 0
#    if topics[0] in vocab:
#        v = np.append(v, word#2topic[topics[0]])
##        counter = 0
##        if
    for counter in range(len(topics)):
        if topics[counter] in vocab:
#            print topics[counter]
            try:
                v += word2topic[topics[counter]]#np.concatenate((word2topic[topics[counter]], word2vec.wv[topics[counter]]))
            except:
                v += word2topic[topics[counter]]#np.concatenate((word2topic[topics[counter]], np.zeros(word2vec.wv['donald'].shape)))
#    print counter + 1
    v /= (counter + 1) * 1.0
#    print type(v)
    return v

def custom_loss(y_true, y_pred):

    y = K.argmax(y_true, axis=1)
    print y[0:5]
##    y_true = np.array(y_true).astype('int64')
##
    print y_true[0:5]
##    length = y_true.get_shape()
##    l = tuple([length[i].value for i in range(0, len(length))])[0]

#    for i in range(y_pred.get_shape()[0].value):
#        y_pred[i] = y_pred[i][y[i]]
#         
#    y_pred = K.log(y_pred[:, K.constant(y, dtype='int64')])
    return K.mean(K.categorical_crossentropy(y_pred[np.where(K.eval(K.equal(y, 0)))[0], :], y_true[np.where(K.eval(K.equal(y, 0)))[0], :]), K.categorical_crossentropy(y_pred[np.where(K.eval(K.equal(y, 1)))[0], :], y_true[np.where(K.eval(K.equal(y, 1)))[0], :]), K.categorical_crossentropy(y_pred[np.where(K.eval(K.equal(y, 2)))[0], :], y_true[np.where(K.eval(K.equal(y, 2)))[0], :]))
#    return K.sum(K.mean(K.dot(K.equal(y, 0), y_pred)), K.mean(K.dot(K.equal(y, 1), y_pred)), K.mean(K.dot(K.equal(y, 2), y_pred))) 
    
def evaluate(y_test, thresholded_pred):

    print "accuracy", (sum(abs(y_test == thresholded_pred))) / float(len(thresholded_pred))
    print Counter(y_test)
    print Counter(thresholded_pred)
    print confusion_matrix(y_test, thresholded_pred)
    print "f1 is", f1_score(y_test, thresholded_pred, average='macro')
        
def distance_embed(sentence):

    global max_words, depth_distance, word2topic
    list = np.array([])
    for word in sentence:
        if word in vocab:
             list = np.append(list, word2topic[word])
    
    #print list.size
    if(list.size > max_words * depth_distance):
        list = list[0:max_words * depth_distance]
    #print sentence
    pad = np.zeros(max_words * depth_distance - list.size)
    list = np.append(list, pad)
    #print list.shape
    return list


def getEmbedding(sentence, model):
    
    global max_words, depth_embed, embedding#, depth_distance
    list = np.array([])
    for word in sentence:
        if word in vocab:
            try:
                list = np.append(list, model[word])
#                print "found", word
            except:
                list = np.append(list, np.zeros(model['donald'].shape))
#                print word
    #print list.size
    if(list.size > max_words * depth_embed):
        list = list[0:max_words * depth_embed]
    #print sentence
    pad = np.zeros(max_words * depth_embed - list.size)
    list = np.append(list, pad)
    #print list.shape
    return list

#def getPOS(sentence):
#    
#    global max_words#, depth
#    all_tags = CMUTweetTagger.runtagger_parse(sentence)

#    list = np.array([])
#    for i in range(len(sentence)):
#        if sentence[i] in vocab:
#             list = np.append(list, all_tags[i])
#    
#    #print list.size
#    if(list.size > max_words):
#        list = list[0:max_words]
#    #print sentence
#    pad = np.zeros(max_words - list.size)
#    list = np.append(list, pad)
#    #print list.shape
#    return list
    
#def getEmbedding(sentence):

#    global word2topic, vocab
#    max_words = 30
#    list = []#np.array([])
#    for word in sentence:
#        if word in vocab:
##            list = np.append(list, word2topic[word])
#            list.append(word2topic[word])
##    list = np.array(list)
#    #print list.size
#    if(len(list) > max_words):
#        list = list[0:max_words]
#    #print sentence
#    pad = [0] * 100# * (max_words - len(list))#np.zeros(max_words - list.size)
#    for i in range((max_words - len(list))):
#        list.append(pad)
##    list.append(pad)
#    #print list.shape
#    return list
    
#getEmbedding(df['tokenized_sents'][0])

def readData2(filename1):
    global tech, politics, music, sports
    print "reading data"
    all_topics = np.concatenate((tech, politics, music, sports))
    cwd = os.getcwd()
#    req_attributes = ['tweet_id', 'topic', 'sentiment', 'tweet']#, 'user_id']#, 'followers_count', 'statuses_count', 'description', 'friends_count', 'location']
#    user_req_attributes = ['tweet_id', 'user_id']#, 'followers_count', 'statuses_count', 'description', 'friends_count', 'location']
    tweet_req_attributes = ['tweet', 'topic']
    path = cwd + "/" + filename1;
    tweet_df = pd.read_csv(path, sep=',');
    
    tweet_df = tweet_df[tweet_req_attributes]
#    print tweet_df['sentiment']
    print tweet_df
    return tweet_df#[req_attributes]


def run_model():
    
        global tech, politics, sports, music, genre, max_words, depth_embed, depth_distance, word2topic, vocab, K