""" Test word2vec

Example:
http://nbviewer.jupyter.org/github/danielfrg/word2vec/blob/master/examples/word2vec.ipynb#

"""


import sys
import os
import unittest
import word2vec


sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')

input_ = os.path.expanduser(os.path.join(TEST_DATA_DIR, 'text'))
output_phrases = os.path.expanduser(os.path.join(TEST_DATA_DIR, 'text-phrases.txt'))
output_clusters = os.path.expanduser(os.path.join(TEST_DATA_DIR,'text-clusters.txt'))
output_bin = os.path.expanduser(os.path.join(TEST_DATA_DIR, 'vectors.bin'))
output_txt = os.path.expanduser(os.path.join(TEST_DATA_DIR,'vectors.txt'))

from wedc.domain.service.data.base import *
from wedc.domain.service.keyword_extraction.base import *

class TestDataLoaderMethods(unittest.TestCase):
    def setUp(self):
        # load data
        filename = 'san-francisco-maria.json'
        path = os.path.join(TEST_DATA_DIR, filename)
        posts = load_by_path(path)

        input_file = open(input_, 'w')
        input_file.writelines(posts)

    # """ set up model
    def test_setup_model(self):
        # word2vec.word2phrase(input_, output_phrases, verbose=True)
        word2vec.word2vec(input_, output_bin, size=300, binary=1, verbose=False)
        word2vec.word2vec(input_, output_txt, size=10, binary=0, verbose=False)
        # word2vec.word2clusters(input_, output_clusters, 10, verbose=True)
    # """

    def test_files_create(self):
        # assert os.path.exists(output_phrases)
        # assert os.path.exists(output_clusters)
        assert os.path.exists(output_bin)
        # assert os.path.exists(output_txt)
    
    def test_load_bin(self):
        model = word2vec.load(output_bin)
        vocab = model.vocab
        vectors = model.vectors
        # print vectors

        assert vectors.shape[0] == vocab.shape[0]
        assert vectors.shape[0] > 3000
        assert vectors.shape[1] == 10

    """
    def test_load_txt(self):
        model = word2vec.load(output_txt)
        vocab = model.vocab
        vectors = model.vectors

        assert vectors.shape[0] == vocab.shape[0]
        assert vectors.shape[0] > 3000
        assert vectors.shape[1] == 10
    """

    def test_prediction(self):
        model = word2vec.load(output_bin)
        indexes, metrics = model.cosine('the')
        assert indexes.shape == (10,)
        assert indexes.shape == metrics.shape

        py_response = model.generate_response(indexes, metrics).tolist()
        assert len(py_response) == 10
        assert len(py_response[0]) == 2

    """
    def test_analogy(self):
        model = word2vec.load(output_txt)
        indexes, metrics = model.analogy(pos=['the', 'the'], neg=['the'], n=20)
        assert indexes.shape == (20,)
        assert indexes.shape == metrics.shape

        py_response = model.generate_response(indexes, metrics).tolist()
        assert len(py_response) == 20
        assert len(py_response[0]) == 2
    """

    def test_word2vec(self):
        pass
        
        
        
    def tearDown(self):
        pass

if __name__ == '__main__':
    # unittest.main()
    
    def run_model_test():
        suite = unittest.TestSuite()
        suite.addTest(TestDataLoaderMethods("test_setup_model"))
        runner = unittest.TextTestRunner()
        runner.run(suite)

    def run_other_test():
        suite = unittest.TestSuite()
        suite.addTest(TestDataLoaderMethods("test_load_bin"))
        runner = unittest.TextTestRunner()
        runner.run(suite)

    run_model_test()




@@ -21,16 +21,105 @@
 xample:%0A
+http://nbviewer.jupyter.org/github/danielfrg/word2vec/blob/master/examples/doc2vec.ipynb%0A
 http://n
@@ -199,17 +199,16 @@
 ec.ipynb
-#
 %0A%0A%22%22%22%0A%0A%0A
@@ -1306,32 +1306,43 @@
 e=True)%0A        
+%0A        # 
 word2vec.word2ve
@@ -1423,32 +1423,178 @@
 word2vec(input_,
+ output_bin, cbow=0, size=300, window=10, negative=5, hs=0, threads=12, iter_=20, min_count=5, verbose=False)%0A%0A        # word2vec.word2vec(input_,
  output_txt, siz
@@ -2082,24 +2082,50 @@
 rint vectors
+%0A        print model.vocab
 %0A%0A        as
@@ -2163,32 +2163,34 @@
 shape%5B0%5D%0A       
+ #
  assert vectors.
@@ -2204,32 +2204,34 @@
 %5D %3E 3000%0A       
+ #
  assert vectors.
@@ -2386,16 +2386,43 @@
 ectors%0A%0A
+        print model.vocab%0A%0A
         
@@ -2462,32 +2462,34 @@
 shape%5B0%5D%0A       
+ #
  assert vectors.
@@ -2503,32 +2503,34 @@
 %5D %3E 3000%0A       
+ #
  assert vectors.
@@ -2544,28 +2544,28 @@
 %5D == 10%0A    
-%22%22%22%0A
+    
 %0A    def tes
@@ -2909,24 +2909,20 @@
 %5D) == 2%0A
-%0A
     
-%22%22%22
 %0A    def
@@ -3723,32 +3723,97 @@
 est.TestSuite()%0A
+        suite.addTest(TestDataLoaderMethods(%22test_setup_model%22))%0A
         suite.ad
@@ -3932,28 +3932,28 @@
 e)%0A%0A    run_
-model
+other
 _test()%0A%0A%0A%0A

update test model