# Path: shorttext/metrics/wasserstein/wordmoverdist.py
# def word_mover_distance(first_sent_tokens, second_sent_tokens, wvmodel, distancefunc=euclidean, lpFile=None):
#     """ Compute the Word Mover's distance (WMD) between the two given lists of tokens.
# 
#     Using methods of linear programming, calculate the WMD between two lists of words. A word-embedding
#     model has to be provided. WMD is returned.
# 
#     Reference: Matt J. Kusner, Yu Sun, Nicholas I. Kolkin, Kilian Q. Weinberger, "From Word Embeddings to Document Distances," *ICML* (2015).
# 
#     :param first_sent_tokens: first list of tokens.
#     :param second_sent_tokens: second list of tokens.
#     :param wvmodel: word-embedding models.
#     :param distancefunc: distance function that takes two numpy ndarray.
#     :param lpFile: deprecated, kept for backward incompatibility. (default: None)
#     :return: Word Mover's distance (WMD)
#     :type first_sent_tokens: list
#     :type second_sent_tokens: list
#     :type wvmodel: gensim.models.keyedvectors.KeyedVectors
#     :type distancefunc: function
#     :type lpFile: str
#     :rtype: float
#     """
#     linprog_result = word_mover_distance_linprog(first_sent_tokens, second_sent_tokens, wvmodel,
#                                                  distancefunc=distancefunc)
#     if lpFile is not None:
#         warnings.warn('The parameter `lpFile` (value: {}) is not used; parameter is deprecated as ' + \
#                       'the package `pulp` is no longer used. Check your code if there is a dependency on ' + \
#                       'this parameter.')
#     return linprog_result['fun']
# 
# Path: shorttext/utils/wordembed.py
# def load_word2vec_model(path, binary=True):
#     """ Load a pre-trained Word2Vec model.
# 
#     :param path: path of the file of the pre-trained Word2Vec model
#     :param binary: whether the file is in binary format (Default: True)
#     :return: a pre-trained Word2Vec model
#     :type path: str
#     :type binary: bool
#     :rtype: gensim.models.keyedvectors.KeyedVectors
#     """
#     return KeyedVectors.load_word2vec_format(path, binary=binary)


import os
import unittest
import urllib
from shorttext.metrics.wasserstein import word_mover_distance
from shorttext.utils import load_word2vec_model



class TestWMD(unittest.TestCase):
    def setUp(self):
        print("Downloading word-embedding model....")
        link = "https://shorttext-data-northernvirginia.s3.amazonaws.com/trainingdata/test_w2v_model.bin"
        filename = "test_w2v_model.bin"
        if not os.path.isfile("test_w2v_model.bin"):
            urllib.request.urlretrieve(link, filename)
        self.w2v_model = load_word2vec_model(filename, binary=True)  # load word2vec model

    def tearDown(self):
        print("Removing word-embedding model")
        if os.path.isfile("test_w2v_model.bin"):
            os.remove('test_w2v_model.bin')

    def calculate_wmd(self, tokens1, tokens2, answer):

# Path: shorttext/metrics/wasserstein/wordmoverdist.py
# def word_mover_distance(first_sent_tokens, second_sent_tokens, wvmodel, distancefunc=euclidean, lpFile=None):
#     """ Compute the Word Mover's distance (WMD) between the two given lists of tokens.
# 
#     Using methods of linear programming, calculate the WMD between two lists of words. A word-embedding
#     model has to be provided. WMD is returned.
# 
#     Reference: Matt J. Kusner, Yu Sun, Nicholas I. Kolkin, Kilian Q. Weinberger, "From Word Embeddings to Document Distances," *ICML* (2015).
# 
#     :param first_sent_tokens: first list of tokens.
#     :param second_sent_tokens: second list of tokens.
#     :param wvmodel: word-embedding models.
#     :param distancefunc: distance function that takes two numpy ndarray.
#     :param lpFile: deprecated, kept for backward incompatibility. (default: None)
#     :return: Word Mover's distance (WMD)
#     :type first_sent_tokens: list
#     :type second_sent_tokens: list
#     :type wvmodel: gensim.models.keyedvectors.KeyedVectors
#     :type distancefunc: function
#     :type lpFile: str
#     :rtype: float
#     """
#     linprog_result = word_mover_distance_linprog(first_sent_tokens, second_sent_tokens, wvmodel,
#                                                  distancefunc=distancefunc)
#     if lpFile is not None:
#         warnings.warn('The parameter `lpFile` (value: {}) is not used; parameter is deprecated as ' + \
#                       'the package `pulp` is no longer used. Check your code if there is a dependency on ' + \
#                       'this parameter.')
#     return linprog_result['fun']
# 
# Path: shorttext/utils/wordembed.py
# def load_word2vec_model(path, binary=True):
#     """ Load a pre-trained Word2Vec model.
# 
#     :param path: path of the file of the pre-trained Word2Vec model
#     :param binary: whether the file is in binary format (Default: True)
#     :return: a pre-trained Word2Vec model
#     :type path: str
#     :type binary: bool
#     :rtype: gensim.models.keyedvectors.KeyedVectors
#     """
#     return KeyedVectors.load_word2vec_format(path, binary=binary)


# Path: test/test_wmd.py
import os
import unittest
import urllib
from shorttext.metrics.wasserstein import word_mover_distance
from shorttext.utils import load_word2vec_model




class TestWMD(unittest.TestCase):
    def setUp(self):
        print("Downloading word-embedding model....")
        link = "https://shorttext-data-northernvirginia.s3.amazonaws.com/trainingdata/test_w2v_model.bin"
        filename = "test_w2v_model.bin"
        if not os.path.isfile("test_w2v_model.bin"):
            urllib.request.urlretrieve(link, filename)
        self.w2v_model = load_word2vec_model(filename, binary=True)  # load word2vec model

    def tearDown(self):
        print("Removing word-embedding model")
        if os.path.isfile("test_w2v_model.bin"):
            os.remove('test_w2v_model.bin')

    def calculate_wmd(self, tokens1, tokens2, answer):

        wdistance = word_mover_distance(tokens1, tokens2, self.w2v_model)