Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from chinmayapancholi13/add_unittests
[WIP] Adding unittests
- Loading branch information
Showing
2 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
unittest.main() |