#!/usr/bin/env python3
# Author: Carlijn Assen

import pickle
import sys
import math
import time
import itertools
import numpy as np
from collections import defaultdict
from collections import Counter
from itertools import permutations


def tf_idf_score():

    docs = pickle.load(open('docs.pickle', 'rb'))
    words_index = pickle.load(open('words_index.pickle', 'rb'))
    tf_idf_tweet_dict = {}
    for line in sys.stdin:
        line = line.strip()
        line = line.split()
        w1 = line[0]
        w2 = line[1]
        if w1 in words_index or w2 in words_index:
            w1_count = 0
            w2_count = 0
            doc_count = 0
            id_s = []
            if w1 not in words_index and w2 in words_index:
                id_s += list(words_index[w2].keys())
            elif w2 not in words_index and w1 in words_index:
                id_s += list(words_index[w1].keys())
            elif w1 in words_index and w2 in words_index:
                id_s += list(words_index[w1].keys())
                id_s += list(words_index[w2].keys())
            df = 0
            for item in docs.values():
                if w1 or w2 in item[2]:
                    df += 1

            for item in id_s:
                words = docs[item][2].split()
                w1_count = w1_count + words.count(w1 or w2)
                w2_count = w2_count + words.count(w2)
                words_found = w1_count
                doc_count = doc_count + docs.count(w1 or w2) 

                if words_found == 1:
                    t = 1
                else:
                    t = 2

                tf = words_found / len(words)
                df = t / len(words)
                idf = np.log(len(docs) / (df + 1))
                tf_idf = tf * idf
                if math.isnan(tf_idf):
                    tf_idf = 0
                else:
                    tf_idf = round(tf_idf, 3)

                tf_idf_tweet_dict[tf_idf] = (docs[item][0], docs[item][1])
            for key in sorted(tf_idf_tweet_dict.keys()):
                print('The tf-idf score = {0}'.format(key))
                print('The user: {0}'.format(tf_idf_tweet_dict[key][0]))
                print('The tweet: {0}\n\n'.format(tf_idf_tweet_dict[key][1]))


tf_idf_score()

