diff --git a/.travis.yml b/.travis.yml index 31ed28e..da993e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,6 @@ +sudo: false +dist: trusty +sudo: required language: python python: - "2.7" @@ -7,10 +10,14 @@ before_install: - ./miniconda.sh -b - export PATH=/home/travis/miniconda2/bin:$PATH - conda update --yes conda + - sudo apt-get update + - sudo apt-get install libc6 install: - conda create --yes -n shorttext-test python=$TRAVIS_PYTHON_VERSION pip numpy scipy - source activate shorttext-test - pip install unittest2 - pip install pytest + - pip install google-compute-engine + - pip install tensorflow==1.1.0 - pip install -U . -script: python shorttext_tests.py \ No newline at end of file +script: python shorttext_tests.py diff --git a/shorttext_tests.py b/shorttext_tests.py index a8601ae..83dbc83 100644 --- a/shorttext_tests.py +++ b/shorttext_tests.py @@ -1,11 +1,92 @@ +import os import unittest -class SampleTest(unittest.TestCase): +import shorttext + + +class TestVarNNEmbeddedVecClassifier(unittest.TestCase): def setUp(self): - self.sample_var = True + if not os.path.isfile("test_w2v_model"): + os.system("wget https://raw.githubusercontent.com/chinmayapancholi13/shorttext_test_data/master/test_w2v_model") # download w2v model + + self.w2v_model = shorttext.utils.load_word2vec_model("test_w2v_model", binary=False) # load word2vec model + self.trainclass_dict = shorttext.data.subjectkeywords() # load training data + + def tearDown(self): + if os.path.isfile("test_w2v_model"): + os.remove("test_w2v_model") # delete downloaded w2v model + + def testCNNWordEmbedWithoutGensim(self): + # create keras model using `CNNWordEmbed` class + keras_model = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) + + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) + + def testDoubleCNNWordEmbedWithGensim(self): + # create keras model using `DoubleCNNWordEmbed` class + keras_model = shorttext.classifiers.frameworks.DoubleCNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) + + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) + + def testDoubleCNNWordEmbedWithoutGensim(self): + # create keras model using `DoubleCNNWordEmbed` class + keras_model = shorttext.classifiers.frameworks.DoubleCNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) + + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) + + def testCNNWordEmbedWithGensim(self): + # create keras model using `CNNWordEmbed` class + keras_model = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) + + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) + + def testCLSTMWordEmbedWithoutGensim(self): + # create keras model using `CLSTMWordEmbed` class + keras_model = shorttext.classifiers.frameworks.CLSTMWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) + + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) + + def testCLSTMWordEmbedWithGensim(self): + # create keras model using `CLSTMWordEmbed` class + keras_model = shorttext.classifiers.frameworks.CLSTMWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True) + + # create and train classifier using keras model constructed above + main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100) + main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2) - def testSampleTestCase(self): - self.assertEqual(True, self.sample_var) + # compute classification score + score_vals = main_classifier.score('artificial intelligence') + self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()