# Path: util/tools.py
# def readNnData(inputfile, n_top = 50):    # format: key data1 data2 data1 data2
#     data = [ x.strip().split(' ', 1) for x in open(inputfile).readlines()]
#     key = [x[0] for x in data]
#     data_list = [x[1] if len(x)>1 else '' for x in data]
# 
#     keyid2data = {}
#     for index in range(len(key)):
#         data1_data2 = data_list[index].split()
#         length = 2*n_top if len(data1_data2) > 2*n_top else len(data1_data2)
#         data1 = [data1_data2[x] for x in range(0,length,2)]
#         data2 = [float(data1_data2[x]) for x in range(1,length,2)]
#         assert(len(data1) == len(data2)),"data1: %d, data2:%d" % (len(data1),len(data2))
#         keyid2data[key[index]] = zip(data1, data2)
#     return keyid2data
#     # return (key, keyid2data)
# 
# def readImageClickture(inputfile, n_top_query):   # format: img_id /t query /t click ...
#     data = [ x.strip().split('\t', 1) for x in open(inputfile).readlines()]
#     img = [x[0] for x in data]
#     query_click = [x[1] for x in data]
# 
#     img2query_clc = {}
#     for index in range(len(img)):
#         querylist = query_click[index].split('\t')
#         length = 2*n_top_query if len(querylist) > 2*n_top_query else len(querylist)
#         query = [querylist[x] for x in range(0,length,2)]
#         click = [int(querylist[x]) for x in range(1,length,2)]
#         assert(len(query) == len(click))
#         img2query_clc[img[index]] = zip(query, click)
# 
#     return img2query_clc
# 
# Path: util/irc_query.py
# def claSimQuery2WeightedQ(query, weightedQuery):
#     words = query.split()
#     score = 0
#     for word in words:
#         score = score + weightedQuery.get(word, 0)
#     if len(words) == 0:
#         return 0
#     else:
#         return 1.0*score/ len(words)
# 
# def getQuerySimer(name):
#     mapping = {"O":OverlapSimer}  #you can add other query simer here
#     return mapping[name]()


import os
import math
from util.tools import readNnData, readImageClickture
from util.irc_query import claSimQuery2WeightedQ, getQuerySimer


class Image2Text:
    def __init__(self, nnimage_file, imgClick_file, qrysim='O', n_top_img = 50, n_top_query = 30):

        # load nn info with distances of images
        self.img2nnimgdis = readNnData(nnimage_file, n_top_img)  # format: img_id img_id dis ...
        print ("[%s] %d images and these top %d nearest neighbours with distance loaded from %s" % 
            (self.__class__.__name__, len(self.img2nnimgdis) , n_top_img, nnimage_file))

        # load query info with click of images  
        self.img2query_clc = readImageClickture(imgClick_file, n_top_query)  # format: img_id /t query /t click ...
        print ("[%s] %d images and these %d queris with click loaded from %s" % 
            (self.__class__.__name__, len(self.img2query_clc) , n_top_query, imgClick_file))

        self.qrySimer = getQuerySimer(qrysim)



    #........................image2text..............................
    #TODO  topimage
    def image2text(self, query, img_list, clickthres = 1):
        scorelist = []
        for img in img_list:
            score = self.image2text_one(query, img, clickthres )
            scorelist.append(score)
        return scorelist

    
    def image2text_one(self, query, image, clickthres = 1):

# Path: util/tools.py
# def readNnData(inputfile, n_top = 50):    # format: key data1 data2 data1 data2
#     data = [ x.strip().split(' ', 1) for x in open(inputfile).readlines()]
#     key = [x[0] for x in data]
#     data_list = [x[1] if len(x)>1 else '' for x in data]
# 
#     keyid2data = {}
#     for index in range(len(key)):
#         data1_data2 = data_list[index].split()
#         length = 2*n_top if len(data1_data2) > 2*n_top else len(data1_data2)
#         data1 = [data1_data2[x] for x in range(0,length,2)]
#         data2 = [float(data1_data2[x]) for x in range(1,length,2)]
#         assert(len(data1) == len(data2)),"data1: %d, data2:%d" % (len(data1),len(data2))
#         keyid2data[key[index]] = zip(data1, data2)
#     return keyid2data
#     # return (key, keyid2data)
# 
# def readImageClickture(inputfile, n_top_query):   # format: img_id /t query /t click ...
#     data = [ x.strip().split('\t', 1) for x in open(inputfile).readlines()]
#     img = [x[0] for x in data]
#     query_click = [x[1] for x in data]
# 
#     img2query_clc = {}
#     for index in range(len(img)):
#         querylist = query_click[index].split('\t')
#         length = 2*n_top_query if len(querylist) > 2*n_top_query else len(querylist)
#         query = [querylist[x] for x in range(0,length,2)]
#         click = [int(querylist[x]) for x in range(1,length,2)]
#         assert(len(query) == len(click))
#         img2query_clc[img[index]] = zip(query, click)
# 
#     return img2query_clc
# 
# Path: util/irc_query.py
# def claSimQuery2WeightedQ(query, weightedQuery):
#     words = query.split()
#     score = 0
#     for word in words:
#         score = score + weightedQuery.get(word, 0)
#     if len(words) == 0:
#         return 0
#     else:
#         return 1.0*score/ len(words)
# 
# def getQuerySimer(name):
#     mapping = {"O":OverlapSimer}  #you can add other query simer here
#     return mapping[name]()


# Path: image2text.py
import os
import math
from util.tools import readNnData, readImageClickture
from util.irc_query import claSimQuery2WeightedQ, getQuerySimer



class Image2Text:
    def __init__(self, nnimage_file, imgClick_file, qrysim='O', n_top_img = 50, n_top_query = 30):

        # load nn info with distances of images
        self.img2nnimgdis = readNnData(nnimage_file, n_top_img)  # format: img_id img_id dis ...
        print ("[%s] %d images and these top %d nearest neighbours with distance loaded from %s" % 
            (self.__class__.__name__, len(self.img2nnimgdis) , n_top_img, nnimage_file))

        # load query info with click of images  
        self.img2query_clc = readImageClickture(imgClick_file, n_top_query)  # format: img_id /t query /t click ...
        print ("[%s] %d images and these %d queris with click loaded from %s" % 
            (self.__class__.__name__, len(self.img2query_clc) , n_top_query, imgClick_file))

        self.qrySimer = getQuerySimer(qrysim)



    #........................image2text..............................
    #TODO  topimage
    def image2text(self, query, img_list, clickthres = 1):
        scorelist = []
        for img in img_list:
            score = self.image2text_one(query, img, clickthres )
            scorelist.append(score)
        return scorelist

    
    def image2text_one(self, query, image, clickthres = 1):

        score_list = []