<reponame>UKPLab/ijcai2019-relis
from summariser.utils.corpus_reader import CorpusReader
from resources import PROCESSED_PATH
from summariser.utils.reader import readSummaries
from summariser.utils.read_features import readFeatures
from summariser.utils.misc import aggregateScores
from scipy.stats.stats import pearsonr
from scipy.stats.stats import spearmanr
import itertools
import numpy as np


def correlation(features, sizes):
    names = []
    for f, s in zip(features, sizes):
        if s>1:
            for i in range(s):
                names.append(f+str(i))
        else:
            names.append(f)
    names.append('rouge_reward')

    dataset = 'DUC2001' ## DUC2001, DUC2002, DUC2004
    sample_num = 9999
    bin_num = 20
    cv_fold_num = 10

    ### read documents and ref. summaries
    reader = CorpusReader(PROCESSED_PATH)
    data = reader.get_data(dataset)

    topic_cnt = 0

    summaries = []
    groups = []
    models_list = []
    docs_list = []
    targets = []

    ### read data
    for topic,docs,models in data:

        print('read DATA {}, TOPIC {}'.format(dataset,topic))
        summs, ref_values_dic = readSummaries(dataset,topic,'rouge',sample_num)
        print('num of summaries read: {}'.format(len(summaries)))
        ref_rewards = aggregateScores(ref_values_dic)
        models_list.append(models)
        docs_list.append(docs)
        summaries.extend(summs)
        groups.extend([topic]*len(summs))
        targets.extend(ref_rewards)
        topic_cnt += 1

    allFeatures = readFeatures(features, dataset, np.array(summaries), groups, set(groups))
    allFeatures = np.c_[allFeatures,np.array(targets)]
    correlations = {}
    threshold_correlation = {}
    for col1, col2 in itertools.combinations(range(len(names)), 2):
        pcc = pearsonr(allFeatures[:, col1], allFeatures[:, col2])[0]
        correlations[names[col1]+' '+names[col2]+': pcc = '] = pcc
        # other way for ease of reading
        correlations[names[col2]+' '+names[col1]+': pcc = '] = pcc
        if pcc < -0.8:
            threshold_correlation[names[col1]+' '+names[col2]+': pcc = '] = pcc
            threshold_correlation[names[col2]+' '+names[col1]+': pcc = '] = pcc
    #for key in sorted(correlations.keys()):
    #    print(key+str(correlations[key]))
    print("Pairs with pcc >.9")
    for key in sorted(threshold_correlation.keys()):
        print(key+str(threshold_correlation[key]))

if __name__ == '__main__':
    f = ['js_12gram', 'tf', 'cf', 'heuristic', 'ner_recall',
         'ner_density', 'rouge1', 'rouge2', 'infersent_heuristic', 'ner_token_recall',
         'ner_token_density', 'ner_js', 'ner_token_js', 'tfidf', 'infersent_mean',
         'infersent_max', 'lsa', 'redundancy_1', 'redundancy_2']
    correlation(f, [1]*len(f))
from summariser.utils.corpus_reader import CorpusReader
from resources import PROCESSED_PATH
from summariser.utils.reader import readSummaries
from summariser.utils.read_features import readFeatures
from summariser.utils.misc import aggregateScores
from scipy.stats.stats import pearsonr
from scipy.stats.stats import spearmanr
import itertools
import numpy as np


def correlation(features, sizes):
    names = []
    for f, s in zip(features, sizes):
        if s>1:
            for i in range(s):
                names.append(f+str(i))
        else:
            names.append(f)
    names.append('rouge_reward')

    dataset = 'DUC2001' ## DUC2001, DUC2002, DUC2004
    sample_num = 9999
    bin_num = 20
    cv_fold_num = 10

    ### read documents and ref. summaries
    reader = CorpusReader(PROCESSED_PATH)
    data = reader.get_data(dataset)

    topic_cnt = 0

    summaries = []
    groups = []
    models_list = []
    docs_list = []
    targets = []

    ### read data
    for topic,docs,models in data:

        print('read DATA {}, TOPIC {}'.format(dataset,topic))
        summs, ref_values_dic = readSummaries(dataset,topic,'rouge',sample_num)
        print('num of summaries read: {}'.format(len(summaries)))
        ref_rewards = aggregateScores(ref_values_dic)
        models_list.append(models)
        docs_list.append(docs)
        summaries.extend(summs)
        groups.extend([topic]*len(summs))
        targets.extend(ref_rewards)
        topic_cnt += 1

    allFeatures = readFeatures(features, dataset, np.array(summaries), groups, set(groups))
    allFeatures = np.c_[allFeatures,np.array(targets)]
    correlations = {}
    threshold_correlation = {}
    for col1, col2 in itertools.combinations(range(len(names)), 2):
        pcc = pearsonr(allFeatures[:, col1], allFeatures[:, col2])[0]
        correlations[names[col1]+' '+names[col2]+': pcc = '] = pcc
        # other way for ease of reading
        correlations[names[col2]+' '+names[col1]+': pcc = '] = pcc
        if pcc < -0.8:
            threshold_correlation[names[col1]+' '+names[col2]+': pcc = '] = pcc
            threshold_correlation[names[col2]+' '+names[col1]+': pcc = '] = pcc
    #for key in sorted(correlations.keys()):
    #    print(key+str(correlations[key]))
    print("Pairs with pcc >.9")
    for key in sorted(threshold_correlation.keys()):
        print(key+str(threshold_correlation[key]))

if __name__ == '__main__':
    f = ['js_12gram', 'tf', 'cf', 'heuristic', 'ner_recall',
         'ner_density', 'rouge1', 'rouge2', 'infersent_heuristic', 'ner_token_recall',
         'ner_token_density', 'ner_js', 'ner_token_js', 'tfidf', 'infersent_mean',
         'infersent_max', 'lsa', 'redundancy_1', 'redundancy_2']
    correlation(f, [1]*len(f))
## DUC2001, DUC2002, DUC2004 ### read documents and ref. summaries ### read data # other way for ease of reading #for key in sorted(correlations.keys()): #    print(key+str(correlations[key]))