#!/usr/bin/python
# coding=utf-8
import json
import os

from threading import Thread

import numpy as np

import pandas as pd

from utils import HOME

PATH = HOME + '/Dropbox/Twitter/'

DATASETS_PATH = PATH + 'Data/datasets/'

PROFILE_PATH = PATH + 'Data/profile/users_profile_data.csv'


class Entropy(Thread):
    def __init__(self, inputfile, outputfile):
        super(Entropy, self).__init__()
        self.inputfile = inputfile
        self.outputfile = outputfile

    def run(self):
        with open(self.inputfile, 'r') as infile, open(self.outputfile, 'wb') as outfile:
            entropies = {}
            for line in infile.readlines():
                line_json = json.loads(line)
                key = line_json.keys()[0]
                if len(line_json[key]) > 10:
                    entropies.update({key: calc_entropy(line_json[key])})
            json.dump({'entropies': entropies}, outfile, indent=4)


def calc_entropy(freq):
    rel_freq = np.array(freq) / float(sum(freq))
    return -1 * np.sum(rel_freq * np.log10(rel_freq)) / np.log10(np.size(freq))


def calc_interval_size(values, size):
    return round(np.size(values) / size, 4)


def clean_dict(intervals):
    keys = filter(lambda k: intervals[k] == 0, intervals)
    for key in keys:
        del intervals[key]