desc stringlengths 3 26.7k | decl stringlengths 11 7.89k | bodies stringlengths 8 553k |
|---|---|---|
'Test loading pre-KeyedVectors word2vec model'
| def testLoadPreKeyedVectorModel(self):
| if (sys.version_info[:2] == (3, 4)):
model_file_suffix = '_py3_4'
elif (sys.version_info < (3,)):
model_file_suffix = '_py2'
else:
model_file_suffix = '_py3'
model_file = ('word2vec_pre_kv%s' % model_file_suffix)
model = word2vec.Word2Vec.load(datapath(model_file))
self.a... |
'Test loading pre-KeyedVectors word2vec model saved in word2vec format'
| def testLoadPreKeyedVectorModelCFormat(self):
| model = keyedvectors.KeyedVectors.load_word2vec_format(datapath('word2vec_pre_kv_c'))
self.assertTrue((model.syn0.shape[0] == len(model.vocab)))
|
'Test storing/loading the entire model in word2vec format.'
| def testPersistenceWord2VecFormat(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.init_sims()
model.wv.save_word2vec_format(testfile(), binary=True)
binary_model_kv = keyedvectors.KeyedVectors.load_word2vec_format(testfile(), binary=True)
binary_model_kv.init_sims(replace=False)
self.assertTrue(np.allclose(model['human']... |
'Test storing/loading the entire model in word2vec non-binary format.'
| def testPersistenceWord2VecFormatNonBinary(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.init_sims()
model.wv.save_word2vec_format(testfile(), binary=False)
text_model = keyedvectors.KeyedVectors.load_word2vec_format(testfile(), binary=False)
text_model.init_sims(False)
self.assertTrue(np.allclose(model['human'], text_model['hu... |
'Test storing/loading the entire model and vocabulary in word2vec format.'
| def testPersistenceWord2VecFormatWithVocab(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.init_sims()
testvocab = os.path.join(tempfile.gettempdir(), 'gensim_word2vec.vocab')
model.wv.save_word2vec_format(testfile(), testvocab, binary=True)
binary_model_with_vocab_kv = keyedvectors.KeyedVectors.load_word2vec_format(testfile(), testv... |
'Test storing/loading the entire model and vocabulary in word2vec format.'
| def testPersistenceKeyedVectorsFormatWithVocab(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.init_sims()
testvocab = os.path.join(tempfile.gettempdir(), 'gensim_word2vec.vocab')
model.wv.save_word2vec_format(testfile(), testvocab, binary=True)
kv_binary_model_with_vocab = keyedvectors.KeyedVectors.load_word2vec_format(testfile(), testv... |
'Test storing/loading the entire model and vocabulary in word2vec format chained with
saving and loading via `save` and `load` methods`.
It was possible prior to 1.0.0 release, now raises Exception'
| def testPersistenceWord2VecFormatCombinationWithStandardPersistence(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.init_sims()
testvocab = os.path.join(tempfile.gettempdir(), 'gensim_word2vec.vocab')
model.wv.save_word2vec_format(testfile(), testvocab, binary=True)
binary_model_with_vocab_kv = keyedvectors.KeyedVectors.load_word2vec_format(testfile(), testv... |
'Test storing/loading the entire model.'
| def testLargeMmap(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
model.save(testfile(), sep_limit=0)
self.models_equal(model, word2vec.Word2Vec.load(testfile()))
self.models_equal(model, word2vec.Word2Vec.load(testfile(), mmap='r'))
|
'Test word2vec vocabulary building.'
| def testVocab(self):
| corpus = LeeCorpus()
total_words = sum((len(sentence) for sentence in corpus))
model = word2vec.Word2Vec(min_count=1, hs=1, negative=0)
model.build_vocab(corpus)
self.assertTrue((len(model.wv.vocab) == 6981))
self.assertEqual(sum((v.count for v in model.wv.vocab.values())), total_words)
np.a... |
'Test word2vec training.'
| def testTraining(self):
| model = word2vec.Word2Vec(size=2, min_count=1, hs=1, negative=0)
model.build_vocab(sentences)
self.assertTrue((model.wv.syn0.shape == (len(model.wv.vocab), 2)))
self.assertTrue((model.syn1.shape == (len(model.wv.vocab), 2)))
model.train(sentences, total_examples=model.corpus_count, epochs=model.iter... |
'Test word2vec scoring.'
| def testScoring(self):
| model = word2vec.Word2Vec(sentences, size=2, min_count=1, hs=1, negative=0)
scores = model.score(sentences, len(sentences))
self.assertEqual(len(scores), len(sentences))
|
'Test word2vec training doesn\'t change locked vectors.'
| def testLocking(self):
| corpus = LeeCorpus()
for sg in range(2):
model = word2vec.Word2Vec(size=4, hs=1, negative=5, min_count=1, sg=sg, window=5)
model.build_vocab(corpus)
locked0 = np.copy(model.wv.syn0[0])
unlocked1 = np.copy(model.wv.syn0[1])
model.syn0_lockf[0] = 0.0
model.train(cor... |
'Test Word2Vec accuracy and KeyedVectors accuracy give the same result'
| def testAccuracy(self):
| model = word2vec.Word2Vec(LeeCorpus())
w2v_accuracy = model.accuracy(datapath('questions-words.txt'))
kv_accuracy = model.wv.accuracy(datapath('questions-words.txt'))
self.assertEqual(w2v_accuracy, kv_accuracy)
|
'Test Spearman and Pearson correlation coefficients give sane results on similarity datasets'
| def testEvaluateWordPairs(self):
| corpus = word2vec.LineSentence(datapath('head500.noblanks.cor.bz2'))
model = word2vec.Word2Vec(corpus, min_count=3, iter=10)
correlation = model.evaluate_word_pairs(datapath('wordsim353.tsv'))
pearson = correlation[0][0]
spearman = correlation[1][0]
oov = correlation[2]
self.assertTrue((0.1 ... |
'Even tiny models trained on LeeCorpus should pass these sanity checks'
| def model_sanity(self, model, train=True):
| if train:
model.build_vocab(list_corpus)
orig0 = np.copy(model.wv.syn0[0])
model.train(list_corpus, total_examples=model.corpus_count, epochs=model.iter)
self.assertFalse((orig0 == model.wv.syn0[1]).all())
sims = model.most_similar('war', topn=len(model.wv.index2word))
t_rank... |
'Test skipgram w/ hierarchical softmax'
| def test_sg_hs(self):
| model = word2vec.Word2Vec(sg=1, window=4, hs=1, negative=0, min_count=5, iter=10, workers=2)
self.model_sanity(model)
|
'Test skipgram w/ negative sampling'
| def test_sg_neg(self):
| model = word2vec.Word2Vec(sg=1, window=4, hs=0, negative=15, min_count=5, iter=10, workers=2)
self.model_sanity(model)
|
'Test CBOW w/ hierarchical softmax'
| def test_cbow_hs(self):
| model = word2vec.Word2Vec(sg=0, cbow_mean=1, alpha=0.05, window=8, hs=1, negative=0, min_count=5, iter=10, workers=2, batch_words=1000)
self.model_sanity(model)
|
'Test CBOW w/ negative sampling'
| def test_cbow_neg(self):
| model = word2vec.Word2Vec(sg=0, cbow_mean=1, alpha=0.05, window=5, hs=0, negative=15, min_count=5, iter=10, workers=2, sample=0)
self.model_sanity(model)
|
'Test CBOW word2vec training.'
| def testTrainingCbow(self):
| model = word2vec.Word2Vec(size=2, min_count=1, sg=0, hs=1, negative=0)
model.build_vocab(sentences)
self.assertTrue((model.wv.syn0.shape == (len(model.wv.vocab), 2)))
self.assertTrue((model.syn1.shape == (len(model.wv.vocab), 2)))
model.train(sentences, total_examples=model.corpus_count, epochs=mode... |
'Test skip-gram (negative sampling) word2vec training.'
| def testTrainingSgNegative(self):
| model = word2vec.Word2Vec(size=2, min_count=1, sg=1, hs=0, negative=2)
model.build_vocab(sentences)
self.assertTrue((model.wv.syn0.shape == (len(model.wv.vocab), 2)))
self.assertTrue((model.syn1neg.shape == (len(model.wv.vocab), 2)))
model.train(sentences, total_examples=model.corpus_count, epochs=m... |
'Test CBOW (negative sampling) word2vec training.'
| def testTrainingCbowNegative(self):
| model = word2vec.Word2Vec(size=2, min_count=1, sg=0, hs=0, negative=2)
model.build_vocab(sentences)
self.assertTrue((model.wv.syn0.shape == (len(model.wv.vocab), 2)))
self.assertTrue((model.syn1neg.shape == (len(model.wv.vocab), 2)))
model.train(sentences, total_examples=model.corpus_count, epochs=m... |
'Test similarity and n_similarity methods.'
| def testSimilarities(self):
| model = word2vec.Word2Vec(size=2, min_count=1, sg=0, hs=0, negative=2)
model.build_vocab(sentences)
model.train(sentences, total_examples=model.corpus_count, epochs=model.iter)
self.assertTrue(model.n_similarity(['graph', 'trees'], ['trees', 'graph']))
self.assertTrue((model.n_similarity(['graph'], ... |
'Test word2vec similar_by_word and similar_by_vector.'
| def testSimilarBy(self):
| model = word2vec.Word2Vec(sentences, size=2, min_count=1, hs=1, negative=0)
wordsims = model.similar_by_word('graph', topn=10)
wordsims2 = model.most_similar(positive='graph', topn=10)
vectorsims = model.similar_by_vector(model['graph'], topn=10)
vectorsims2 = model.most_similar([model['graph']], to... |
'Test word2vec parallel training.'
| def testParallel(self):
| if (word2vec.FAST_VERSION < 0):
return
corpus = utils.RepeatCorpus(LeeCorpus(), 10000)
for workers in [2, 4]:
model = word2vec.Word2Vec(corpus, workers=workers)
sims = model.most_similar('israeli')
|
'Test word2vec results identical with identical RNG seed.'
| def testRNG(self):
| model = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
model2 = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
self.models_equal(model, model2)
|
'Test word2vec model after delete_temporary_training_data'
| def testDeleteTemporaryTrainingData(self):
| for i in [0, 1]:
for j in [0, 1]:
model = word2vec.Word2Vec(sentences, size=10, min_count=0, seed=42, hs=i, negative=j)
if i:
self.assertTrue(hasattr(model, 'syn1'))
if j:
self.assertTrue(hasattr(model, 'syn1neg'))
self.assertTr... |
'Test word2vec predict_output_word method handling for negative sampling scheme'
| def testPredictOutputWord(self):
| model_with_neg = word2vec.Word2Vec(sentences, min_count=1)
predictions_with_neg = model_with_neg.predict_output_word(['system', 'human'], topn=5)
self.assertTrue((len(predictions_with_neg) == 5))
predictions_out_of_vocab = model_with_neg.predict_output_word(['some', 'random', 'words'], topn=5)
self.... |
'Test if warning is raised on non-ideal input to a word2vec model'
| @log_capture()
def testBuildVocabWarning(self, l):
| sentences = ['human', 'machine']
model = word2vec.Word2Vec()
model.build_vocab(sentences)
warning = "Each 'sentences' item should be a list of words (usually unicode strings)."
self.assertTrue((warning in str(l)))
|
'Test if warning is raised if alpha rises during subsequent calls to train()'
| @log_capture()
def testTrainWarning(self, l):
| sentences = [['human'], ['graph', 'trees']]
model = word2vec.Word2Vec(min_count=1)
model.build_vocab(sentences)
for epoch in range(10):
model.train(sentences, total_examples=model.corpus_count, epochs=model.iter)
model.alpha -= 0.002
model.min_alpha = model.alpha
if (epoc... |
'Is sentences a generator object?'
| def test_sentences_should_not_be_a_generator(self):
| gen = (s for s in sentences)
self.assertRaises(TypeError, word2vec.Word2Vec, (gen,))
|
'Test if exception is raised when loading word2vec model on instance'
| def testLoadOnClassError(self):
| self.assertRaises(AttributeError, load_on_instance)
|
'Test if reset_from() uses pre-built structures from other model'
| def test_reset_from(self):
| model = word2vec.Word2Vec(sentences, min_count=1)
other_model = word2vec.Word2Vec(new_sentences, min_count=1)
other_vocab = other_model.wv.vocab
model.reset_from(other_model)
self.assertEqual(model.wv.vocab, other_vocab)
|
'Test basic functionality with a test sentence.'
| def testNonzero(self):
| if (not PYEMD_EXT):
return
model = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
sentence1 = ['human', 'interface', 'computer']
sentence2 = ['survey', 'user', 'computer', 'system', 'response', 'time']
distance = model.wmdistance(sentence1, sentence2)
self.assertFalse((dis... |
'Check that distance is symmetric.'
| def testSymmetry(self):
| if (not PYEMD_EXT):
return
model = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
sentence1 = ['human', 'interface', 'computer']
sentence2 = ['survey', 'user', 'computer', 'system', 'response', 'time']
distance1 = model.wmdistance(sentence1, sentence2)
distance2 = model.wm... |
'Check that the distance from a sentence to itself is zero.'
| def testIdenticalSentences(self):
| if (not PYEMD_EXT):
return
model = word2vec.Word2Vec(sentences, min_count=1)
sentence = ['survey', 'user', 'computer', 'system', 'response', 'time']
distance = model.wmdistance(sentence, sentence)
self.assertEqual(0.0, distance)
|
'Does LineSentence work with a filename argument?'
| def testLineSentenceWorksWithFilename(self):
| with utils.smart_open(datapath('lee_background.cor')) as orig:
sentences = word2vec.LineSentence(datapath('lee_background.cor'))
for words in sentences:
self.assertEqual(words, utils.to_unicode(orig.readline()).split())
|
'Does LineSentence work with a compressed file object argument?'
| def testLineSentenceWorksWithCompressedFile(self):
| with utils.smart_open(datapath('head500.noblanks.cor')) as orig:
sentences = word2vec.LineSentence(bz2.BZ2File(datapath('head500.noblanks.cor.bz2')))
for words in sentences:
self.assertEqual(words, utils.to_unicode(orig.readline()).split())
|
'Does LineSentence work with a file object argument, rather than filename?'
| def testLineSentenceWorksWithNormalFile(self):
| with utils.smart_open(datapath('head500.noblanks.cor')) as orig:
with utils.smart_open(datapath('head500.noblanks.cor')) as fin:
sentences = word2vec.LineSentence(fin)
for words in sentences:
self.assertEqual(words, utils.to_unicode(orig.readline()).split())
|
'Does PathLineSentences work with a path argument?'
| def testPathLineSentences(self):
| with utils.smart_open(os.path.join(datapath('PathLineSentences'), '1.txt')) as orig1:
with utils.smart_open(os.path.join(datapath('PathLineSentences'), '2.txt.bz2')) as orig2:
sentences = word2vec.PathLineSentences(datapath('PathLineSentences'))
orig = (orig1.readlines() + orig2.read... |
'Does PathLineSentences work with a single file argument?'
| def testPathLineSentencesOneFile(self):
| test_file = os.path.join(datapath('PathLineSentences'), '1.txt')
with utils.smart_open(test_file) as orig:
sentences = word2vec.PathLineSentences(test_file)
for words in sentences:
self.assertEqual(words, utils.to_unicode(orig.readline()).split())
|
'setup lee test corpora'
| def setUp(self):
| global bg_corpus, corpus, human_sim_vector, bg_corpus2, corpus2
pre_path = os.path.join(os.path.dirname(__file__), 'test_data')
bg_corpus_file = 'lee_background.cor'
corpus_file = 'lee.cor'
sim_file = 'similarities0-1.txt'
latin1 = (lambda line: utils.to_unicode(line, encoding='latin1'))
wit... |
'availability and integrity of corpus'
| def test_corpus(self):
| documents_in_bg_corpus = 300
documents_in_corpus = 50
len_sim_vector = 1225
self.assertEqual(len(bg_corpus), documents_in_bg_corpus)
self.assertEqual(len(corpus), documents_in_corpus)
self.assertEqual(len(human_sim_vector), len_sim_vector)
|
'correlation with human data > 0.6
(this is the value which was achieved in the original paper)'
| def test_lee(self):
| global bg_corpus, corpus
dictionary = corpora.Dictionary(bg_corpus)
bg_corpus = [dictionary.doc2bow(text) for text in bg_corpus]
corpus = [dictionary.doc2bow(text) for text in corpus]
log_ent = models.LogEntropyModel(bg_corpus)
bg_corpus_ent = log_ent[bg_corpus]
lsi = models.LsiModel(bg_corp... |
'Test word2vec training.'
| def testWord2VecTraining(self):
| model = self.model_cos_sim
self.assertTrue((model.wv.syn0.shape == (len(model.wv.vocab), 100)))
self.assertTrue((model.syn1.shape == (len(model.wv.vocab), 100)))
sims = model.most_similar('graph', topn=10)
graph_vector = model.wv.syn0norm[model.wv.vocab['graph'].index]
sims2 = model.most_similar... |
'Test Keras \'Embedding\' layer returned by \'get_embedding_layer\' function for a simple word similarity task.'
| def testEmbeddingLayerCosineSim(self):
| keras_w2v_model = self.model_cos_sim
keras_w2v_model_wv = keras_w2v_model.wv
embedding_layer = keras_w2v_model_wv.get_embedding_layer()
input_a = Input(shape=(1,), dtype='int32', name='input_a')
input_b = Input(shape=(1,), dtype='int32', name='input_b')
embedding_a = embedding_layer(input_a)
... |
'Test Keras \'Embedding\' layer returned by \'get_embedding_layer\' function for a smaller version of the 20NewsGroup classification problem.'
| def testEmbeddingLayer20NewsGroup(self):
| MAX_SEQUENCE_LENGTH = 1000
texts = []
texts_w2v = []
labels = []
data = fetch_20newsgroups(subset='train', categories=['alt.atheism', 'comp.graphics', 'sci.space'])
for index in range(len(data)):
label_id = data.target[index]
file_data = data.data[index]
i = file_data.fin... |
'Test tuple input for l1 transformation'
| def test_tupleInput_l1(self):
| normalized = self.model_l1.normalize(self.doc)
expected = [(1, 0.25), (5, 0.5), (8, 0.25)]
self.assertTrue(np.allclose(normalized, expected))
|
'Test sparse csr matrix input for l1 transformation'
| def test_sparseCSRInput_l1(self):
| row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sparse_matrix = csr_matrix((data, (row, col)), shape=(3, 3))
normalized = self.model_l1.normalize(sparse_matrix)
self.assertTrue(issparse(normalized))
expected = np.array([[0.04761905, 0... |
'Test for np ndarray input for l1 transformation'
| def test_numpyndarrayInput_l1(self):
| ndarray_matrix = np.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]])
normalized = self.model_l1.normalize(ndarray_matrix)
self.assertTrue(isinstance(normalized, np.ndarray))
expected = np.array([[0.04761905, 0.0, 0.0952381], [0.0, 0.0, 0.14285714], [0.19047619, 0.23809524, 0.28571429]])
self.assertTrue(np.a... |
'Test tuple input for l2 transformation'
| def test_tupleInput_l2(self):
| normalized = self.model_l2.normalize(self.doc)
expected = [(1, 0.4082482904638631), (5, 0.8164965809277261), (8, 0.4082482904638631)]
self.assertTrue(np.allclose(normalized, expected))
|
'Test sparse csr matrix input for l2 transformation'
| def test_sparseCSRInput_l2(self):
| row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sparse_matrix = csr_matrix((data, (row, col)), shape=(3, 3))
normalized = self.model_l2.normalize(sparse_matrix)
self.assertTrue(issparse(normalized))
expected = np.array([[0.10482848, 0... |
'Test for np ndarray input for l2 transformation'
| def test_numpyndarrayInput_l2(self):
| ndarray_matrix = np.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]])
normalized = self.model_l2.normalize(ndarray_matrix)
self.assertTrue(isinstance(normalized, np.ndarray))
expected = np.array([[0.10482848, 0.0, 0.20965697], [0.0, 0.0, 0.31448545], [0.41931393, 0.52414242, 0.6289709]])
self.assertTrue(np.a... |
'Test if error messages raised on unsupported norm'
| def testInit(self):
| self.assertRaises(ValueError, normmodel.NormModel, self.corpus, 'l0')
|
'Even tiny models trained on any corpus should pass these sanity checks'
| def model_sanity(self, model):
| self.assertEqual(model.wv.syn0.shape, (len(model.wv.vocab), model.vector_size))
self.assertEqual(model.wv.syn0_all.shape, (model.num_ngram_vectors, model.vector_size))
|
'Test self.test_model successfully trained, parameters and weights correctly loaded'
| def testTraining(self):
| if (self.ft_path is None):
logger.info('FT_HOME env variable not set, skipping test')
return
(vocab_size, model_size) = (1763, 10)
trained_model = fasttext.FastText.train(self.ft_path, self.corpus_file, size=model_size, output_file=testfile())
self.assertEqual(trained_m... |
'Tests words with frequency less than `min_count` absent from vocab'
| def testMinCount(self):
| if (self.ft_path is None):
logger.info('FT_HOME env variable not set, skipping test')
return
test_model_min_count_5 = fasttext.FastText.train(self.ft_path, self.corpus_file, output_file=testfile(), size=10, min_count=5)
self.assertTrue(('forests' not in test_model_min_count... |
'Tests output vector dimensions are the same as the value for `size` param'
| def testModelSize(self):
| if (self.ft_path is None):
logger.info('FT_HOME env variable not set, skipping test')
return
test_model_size_20 = fasttext.FastText.train(self.ft_path, self.corpus_file, output_file=testfile(), size=20)
self.assertEqual(test_model_size_20.vector_size, 20)
self.assertEqu... |
'Test storing/loading the entire model.'
| def testPersistence(self):
| self.test_model.save(testfile())
loaded = fasttext.FastText.load(testfile())
self.models_equal(self.test_model, loaded)
self.test_model.save(testfile(), sep_limit=0)
self.models_equal(self.test_model, fasttext.FastText.load(testfile()))
|
'Test syn0norm/syn0_all_norm aren\'t saved in model file'
| def testNormalizedVectorsNotSaved(self):
| self.test_model.init_sims()
self.test_model.save(testfile())
loaded = fasttext.FastText.load(testfile())
self.assertTrue((loaded.wv.syn0norm is None))
self.assertTrue((loaded.wv.syn0_all_norm is None))
wv = self.test_model.wv
wv.save(testfile())
loaded_kv = keyedvectors.KeyedVectors.load... |
'Test model successfully loaded from fastText .bin file'
| def testLoadFastTextFormat(self):
| try:
model = fasttext.FastText.load_fasttext_format(self.test_model_file)
except Exception as exc:
self.fail(('Unable to load FastText model from file %s: %s' % (self.test_model_file, exc)))
(vocab_size, model_size) = (1762, 10)
self.assertEqual(model.wv.syn0.shap... |
'Test model successfully loaded from fastText (new format) .bin file'
| def testLoadFastTextNewFormat(self):
| try:
new_model = fasttext.FastText.load_fasttext_format(self.test_new_model_file)
except Exception as exc:
self.fail(('Unable to load FastText model from file %s: %s' % (self.test_new_model_file, exc)))
(vocab_size, model_size) = (1763, 10)
self.assertEqual(new_mo... |
'Test model accepts input as both `/path/to/model` or `/path/to/model.bin`'
| def testLoadFileName(self):
| self.assertTrue(fasttext.FastText.load_fasttext_format(datapath('lee_fasttext_new')))
self.assertTrue(fasttext.FastText.load_fasttext_format(datapath('lee_fasttext_new.bin')))
|
'Test loading model with non-ascii words in vocab'
| def testLoadModelWithNonAsciiVocab(self):
| model = fasttext.FastText.load_fasttext_format(datapath('non_ascii_fasttext'))
self.assertTrue((u'kter\xfd' in model))
try:
vector = model[u'kter\xfd']
except UnicodeDecodeError:
self.fail('Unable to access vector for utf8 encoded non-ascii word')
|
'Test loading model with words in user-specified encoding'
| def testLoadModelNonUtf8Encoding(self):
| model = fasttext.FastText.load_fasttext_format(datapath('cp852_fasttext'), encoding='cp852')
self.assertTrue((u'kter\xfd' in model))
try:
vector = model[u'kter\xfd']
except KeyError:
self.fail('Unable to access vector for cp-852 word')
|
'Test n_similarity for in-vocab and out-of-vocab words'
| def testNSimilarity(self):
| self.assertTrue(numpy.allclose(self.test_model.n_similarity(['the', 'and'], ['and', 'the']), 1.0))
self.assertEqual(self.test_model.n_similarity(['the'], ['and']), self.test_model.n_similarity(['and'], ['the']))
self.assertTrue(numpy.allclose(self.test_model.n_similarity(['night', 'nights'], ['nights', 'nig... |
'Test similarity for in-vocab and out-of-vocab words'
| def testSimilarity(self):
| self.assertTrue(numpy.allclose(self.test_model.similarity('the', 'the'), 1.0))
self.assertEqual(self.test_model.similarity('the', 'and'), self.test_model.similarity('and', 'the'))
self.assertTrue(numpy.allclose(self.test_model.similarity('nights', 'nights'), 1.0))
self.assertEqual(self.test_model.simila... |
'Test most_similar for in-vocab and out-of-vocab words'
| def testMostSimilar(self):
| self.assertEqual(len(self.test_model.most_similar(positive=['the', 'and'], topn=5)), 5)
self.assertEqual(self.test_model.most_similar('the'), self.test_model.most_similar(positive=['the']))
self.assertEqual(len(self.test_model.most_similar(['night', 'nights'], topn=5)), 5)
self.assertEqual(self.test_mod... |
'Test most_similar_cosmul for in-vocab and out-of-vocab words'
| def testMostSimilarCosmul(self):
| self.assertEqual(len(self.test_model.most_similar_cosmul(positive=['the', 'and'], topn=5)), 5)
self.assertEqual(self.test_model.most_similar_cosmul('the'), self.test_model.most_similar_cosmul(positive=['the']))
self.assertEqual(len(self.test_model.most_similar_cosmul(['night', 'nights'], topn=5)), 5)
se... |
'Tests word vector lookup for in-vocab and out-of-vocab words'
| def testLookup(self):
| self.assertTrue(('night' in self.test_model.wv.vocab))
self.assertTrue(numpy.allclose(self.test_model['night'], self.test_model[['night']]))
self.assertFalse(('nights' in self.test_model.wv.vocab))
self.assertTrue(numpy.allclose(self.test_model['nights'], self.test_model[['nights']]))
self.assertRai... |
'Tests __contains__ for in-vocab and out-of-vocab words'
| def testContains(self):
| self.assertTrue(('night' in self.test_model.wv.vocab))
self.assertTrue(('night' in self.test_model))
self.assertFalse(('nights' in self.test_model.wv.vocab))
self.assertTrue(('nights' in self.test_model))
self.assertFalse(('a!@' in self.test_model.wv.vocab))
self.assertFalse(('a!@' in self.test_... |
'Tests wmdistance for docs with in-vocab and out-of-vocab words'
| def testWmdistance(self):
| doc = ['night', 'payment']
oov_doc = ['nights', 'forests', 'payments']
ngrams_absent_doc = ['a!@', 'b#$']
dist = self.test_model.wmdistance(doc, oov_doc)
self.assertNotEqual(float('inf'), dist)
dist = self.test_model.wmdistance(doc, ngrams_absent_doc)
self.assertEqual(float('inf'), dist)
|
'Tests doesnt_match for list of out-of-vocab words'
| def testDoesntMatch(self):
| oov_words = ['nights', 'forests', 'payments']
for word in oov_words:
self.assertFalse((word in self.test_model.wv.vocab))
try:
self.test_model.doesnt_match(oov_words)
except Exception:
self.fail('model.doesnt_match raises exception for oov words')
|
'`HashDictionary` can be saved as textfile.'
| def test_saveAsText(self):
| tmpf = get_tmpfile('dict_test.txt')
d = HashDictionary(['\xc5\xbelo\xc5\xa5ou\xc4\x8dk\xc3\xbd kon\xc3\xad\xc4\x8dek'.split(), '\xd0\x9c\xd0\xb0\xd0\xbb\xd0\xb9\xd0\xb6 \xd0\xbe\xd0\xb1\xd0\xbb\xd1\x8c\xd0\xb9\xd0\xba\xd0\xb2\xd1\x8e\xd1\x8d \xd0\xb0\xd1\x82 \xd1\x8d\xd0\xb6\xd1\x82'.split()])
d... |
'`HashDictionary` can be saved & loaded as compressed pickle.'
| def test_saveAsTextBz2(self):
| tmpf = get_tmpfile('dict_test.txt.bz2')
d = HashDictionary(['\xc5\xbelo\xc5\xa5ou\xc4\x8dk\xc3\xbd kon\xc3\xad\xc4\x8dek'.split(), '\xd0\x9c\xd0\xb0\xd0\xbb\xd0\xb9\xd0\xb6 \xd0\xbe\xd0\xb1\xd0\xbb\xd1\x8c\xd0\xb9\xd0\xba\xd0\xb2\xd1\x8e\xd1\x8d \xd0\xb0\xd1\x82 \xd1\x8d\xd0\xb6\xd1\x82'.split()])
... |
'Test p_boolean_document()'
| def testPBooleanDocument(self):
| accumulator = probability_estimation.p_boolean_document(self.corpus, self.segmented_topics)
obtained = accumulator.index_to_dict()
expected = {self.graph_id: {5}, self.user_id: {1, 3}, self.system_id: {1, 2}, self.computer_id: {0}}
self.assertEqual(expected, obtained)
|
'Test p_boolean_sliding_window()'
| def testPBooleanSlidingWindow(self):
| accumulator = probability_estimation.p_boolean_sliding_window(self.texts, self.segmented_topics, self.dictionary, 2)
self.assertEqual(1, accumulator[self.computer_id])
self.assertEqual(3, accumulator[self.user_id])
self.assertEqual(1, accumulator[self.graph_id])
self.assertEqual(4, accumulator[self.... |
'Test loading/saving LdaVowpalWabbit model.'
| def test_save_load(self):
| if (not self.vw_path):
return
lda = LdaVowpalWabbit(self.vw_path, corpus=self.corpus, passes=10, chunksize=256, id2word=self.dictionary, cleanup_files=True, alpha=0.1, eta=0.1, num_topics=len(TOPIC_WORDS), random_seed=1)
with tempfile.NamedTemporaryFile() as fhandle:
lda.save(fhandle.name)
... |
'Test updating existing LdaVowpalWabbit model.'
| def test_model_update(self):
| if (not self.vw_path):
return
lda = LdaVowpalWabbit(self.vw_path, corpus=[self.corpus[0]], passes=10, chunksize=256, id2word=self.dictionary, cleanup_files=True, alpha=0.1, eta=0.1, num_topics=len(TOPIC_WORDS), random_seed=1)
lda.update(self.corpus[1:])
result = lda.log_perplexity(self.corpus)
... |
'Test LdaVowpalWabbit perplexity is within expected range.'
| def test_perplexity(self):
| if (not self.vw_path):
return
lda = LdaVowpalWabbit(self.vw_path, corpus=self.corpus, passes=10, chunksize=256, id2word=self.dictionary, cleanup_files=True, alpha=0.1, eta=0.1, num_topics=len(TOPIC_WORDS), random_seed=1)
result = lda.log_perplexity(self.corpus)
self.assertTrue((result < (-1)))
... |
'Test LdaVowpalWabbit topic coherence.'
| def test_topic_coherence(self):
| if (not self.vw_path):
return
(corpus, dictionary) = get_corpus()
lda = LdaVowpalWabbit(self.vw_path, corpus=corpus, passes=10, chunksize=256, id2word=dictionary, cleanup_files=True, alpha=0.1, eta=0.1, num_topics=len(TOPIC_WORDS), random_seed=1)
lda.print_topics(5, 10)
topic_map = {}
fo... |
'Test corpus to Vowpal Wabbit format conversion.'
| def test_corpus_to_vw(self):
| if (not self.vw_path):
return
corpus = [[(0, 5), (7, 1), (5, 3), (0, 2)], [(7, 2), (2, 1), (3, 11)], [(1, 1)], [], [(5, 2), (0, 1)]]
expected = '\n| 0:5 7:1 5:3 0:2\n| 7:2 2:1 3:11\n| 1:1\n|\n| 5:2 0:1\n'.strip()
result = '\n'.join(ldavowpalwabbit.corpus_to_vw(corpu... |
'Test copying of VWModel to LdaModel'
| def testvwmodel2ldamodel(self):
| if (not self.vw_path):
return
tm1 = LdaVowpalWabbit(vw_path=self.vw_path, corpus=self.corpus, num_topics=2, id2word=self.dictionary)
tm2 = ldavowpalwabbit.vwmodel2ldamodel(tm1)
for document in self.corpus:
(element1_1, element1_2) = tm1[document][0]
(element2_1, element2_2) = tm2... |
'Test cosine_similarity()'
| def testCosineSimilarity(self):
| obtained = indirect_confirmation_measure.cosine_similarity(self.segmentation, self.accumulator, self.topics, self.measure, self.gamma)
expected = ((0.623 + 0.623) / 2.0)
self.assertAlmostEqual(expected, obtained[0], 4)
|
'Test storing/loading the entire model.'
| def test_persistence(self):
| model = doc2vec.Doc2Vec(DocsLeeCorpus(), min_count=1)
model.save(testfile())
self.models_equal(model, doc2vec.Doc2Vec.load(testfile()))
|
'Test storing the entire model in word2vec format.'
| def testPersistenceWord2VecFormat(self):
| model = doc2vec.Doc2Vec(DocsLeeCorpus(), min_count=1)
test_doc_word = os.path.join(tempfile.gettempdir(), 'gensim_doc2vec.dw')
model.save_word2vec_format(test_doc_word, doctag_vec=True, word_vec=True, binary=True)
binary_model_dv = keyedvectors.KeyedVectors.load_word2vec_format(test_doc_word, binary=Tru... |
'Test storing/loading the entire model.'
| def test_load_mmap(self):
| model = doc2vec.Doc2Vec(sentences, min_count=1)
model.save(testfile(), sep_limit=0)
self.models_equal(model, doc2vec.Doc2Vec.load(testfile()))
self.models_equal(model, doc2vec.Doc2Vec.load(testfile(), mmap='r'))
|
'Test doc2vec doctag alternatives'
| def test_int_doctags(self):
| corpus = DocsLeeCorpus()
model = doc2vec.Doc2Vec(min_count=1)
model.build_vocab(corpus)
self.assertEqual(len(model.docvecs.doctag_syn0), 300)
self.assertEqual(model.docvecs[0].shape, (100,))
self.assertEqual(model.docvecs[np.int64(0)].shape, (100,))
self.assertRaises(KeyError, model.__getite... |
'Test doc2vec doctag alternatives'
| def test_missing_string_doctag(self):
| corpus = list(DocsLeeCorpus(True))
corpus = (corpus[0:10] + corpus)
model = doc2vec.Doc2Vec(min_count=1)
model.build_vocab(corpus)
self.assertRaises(KeyError, model.docvecs.__getitem__, 'not_a_tag')
|
'Test doc2vec doctag alternatives'
| def test_string_doctags(self):
| corpus = list(DocsLeeCorpus(True))
corpus = (corpus[0:10] + corpus)
model = doc2vec.Doc2Vec(min_count=1)
model.build_vocab(corpus)
self.assertEqual(len(model.docvecs.doctag_syn0), 300)
self.assertEqual(model.docvecs[0].shape, (100,))
self.assertEqual(model.docvecs['_*0'].shape, (100,))
s... |
'Test similarity of out of training sentences'
| def test_similarity_unseen_docs(self):
| rome_str = ['rome', 'italy']
car_str = ['car']
corpus = list(DocsLeeCorpus(True))
model = doc2vec.Doc2Vec(min_count=1)
model.build_vocab(corpus)
self.assertTrue((model.docvecs.similarity_unseen_docs(model, rome_str, rome_str) > model.docvecs.similarity_unseen_docs(model, rome_str, car_str)))
|
'Any non-trivial model on DocsLeeCorpus can pass these sanity checks'
| def model_sanity(self, model, keep_training=True):
| fire1 = 0
fire2 = np.int64(8)
tennis1 = 6
doc0_inferred = model.infer_vector(list(DocsLeeCorpus())[0].words)
sims_to_infer = model.docvecs.most_similar([doc0_inferred], topn=len(model.docvecs))
f_rank = [docid for (docid, sim) in sims_to_infer].index(fire1)
self.assertLess(f_rank, 10)
si... |
'Test doc2vec training.'
| def test_training(self):
| corpus = DocsLeeCorpus()
model = doc2vec.Doc2Vec(size=100, min_count=2, iter=20, workers=1)
model.build_vocab(corpus)
self.assertEqual(model.docvecs.doctag_syn0.shape, (300, 100))
model.train(corpus, total_examples=model.corpus_count, epochs=model.iter)
self.model_sanity(model)
model2 = doc2... |
'Test DBOW doc2vec training.'
| def test_dbow_hs(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=0, hs=1, negative=0, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DM/mean doc2vec training.'
| def test_dmm_hs(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=1, size=24, window=4, hs=1, negative=0, alpha=0.05, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DM/sum doc2vec training.'
| def test_dms_hs(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=0, size=24, window=4, hs=1, negative=0, alpha=0.05, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DM/concatenate doc2vec training.'
| def test_dmc_hs(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_concat=1, size=24, window=4, hs=1, negative=0, alpha=0.05, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DBOW doc2vec training.'
| def test_dbow_neg(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=0, hs=0, negative=10, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DM/mean doc2vec training.'
| def test_dmm_neg(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=1, size=24, window=4, hs=0, negative=10, alpha=0.05, min_count=2, iter=20)
self.model_sanity(model)
|
'Test DM/sum doc2vec training.'
| def test_dms_neg(self):
| model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=0, size=24, window=4, hs=0, negative=10, alpha=0.05, min_count=2, iter=20)
self.model_sanity(model)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.