def test_predict_with_empty_string_options(self): # test fewest words self.assertEqual( style.LengthRanker(choose='shortest', length='words').fit([['word', 'word']], y=[0]).predict([['', 'aa'], ['aa', ''] ]).tolist(), [0, 1]) # test most words self.assertEqual( style.LengthRanker(choose='longest', length='words').fit([['word', 'word']], y=[0]).predict([['', 'aa'], ['aa', ''] ]).tolist(), [1, 0]) # test fewest characters self.assertEqual( style.LengthRanker(choose='shortest', length='characters').fit( [['word', 'word']], y=[0]).predict([['', 'aa'], ['aa', '']]).tolist(), [0, 1]) # test most characters self.assertEqual( style.LengthRanker(choose='longest', length='characters').fit( [['word', 'word']], y=[0]).predict([['', 'aa'], ['aa', '']]).tolist(), [1, 0])
def test_fit(self): classifier = style.LengthRanker() # test that fit takes ``X`` and ``y`` arguments classifier.fit(X=[['foo', 'bar'], ['baz', 'quux']], y=[0, 1]) # test that fit sets classes_ correctly self.assertEqual(classifier.classes_, [0, 1])
def test_predict(self): # test fewest words # regular case self.assertEqual( style.LengthRanker(choose='shortest', length='words').fit( [['word', 'word']], y=[0]).predict([['foo bar', 'baz'], ['foo', 'bar baz'], ['bar', 'foo baz']]).tolist(), [1, 0, 0]) # where character lengths disagree with word lengths self.assertEqual( style.LengthRanker(choose='shortest', length='words').fit( [['word', 'word']], y=[0]).predict([['a b', 'cccc'], ['d', 'e f'], ['aaaaaa', 'b cc']]).tolist(), [1, 0, 0]) # test most words # regular case self.assertEqual( style.LengthRanker(choose='longest', length='words').fit( [['word', 'word']], y=[0]).predict([['foo bar', 'baz'], ['foo', 'bar baz'], ['bar', 'foo baz']]).tolist(), [0, 1, 1]) # where character lengths disagree with word lengths self.assertEqual( style.LengthRanker(choose='longest', length='words').fit( [['word', 'word']], y=[0]).predict([['a b', 'cccc'], ['d', 'e f'], ['aaaaaa', 'b cc']]).tolist(), [0, 1, 1]) # test fewest characters # regular case self.assertEqual( style.LengthRanker(choose='shortest', length='characters').fit( [['word', 'word']], y=[0]).predict([['aaa', 'a'], ['b', 'bbb'], ['c', 'ccc']]).tolist(), [1, 0, 0]) # where character lengths disagree with word lengths self.assertEqual( style.LengthRanker(choose='shortest', length='characters').fit( [['word', 'word']], y=[0]).predict([['a b', 'cccc'], ['d', 'e f'], ['aaaaaa', 'b cc']]).tolist(), [0, 0, 1]) # test most characters # regular case self.assertEqual( style.LengthRanker(choose='longest', length='characters').fit( [['word', 'word']], y=[0]).predict([['aaa', 'a'], ['b', 'bbb'], ['c', 'ccc']]).tolist(), [0, 1, 1]) # where character lengths disagree with word lengths self.assertEqual( style.LengthRanker(choose='longest', length='characters').fit( [['word', 'word']], y=[0]).predict([['a b', 'cccc'], ['d', 'e f'], ['aaaaaa', 'b cc']]).tolist(), [1, 1, 0])
def test_predict_with_ties(self): predictions = style.LengthRanker( choose='shortest', length='characters').fit([['word', 'word', 'word']], y=[0]).predict([['', '', 'aaaa'], ['aaaa', 'aa', 'aa']]).tolist() self.assertIn(predictions[0], [0, 1]) self.assertIn(predictions[1], [1, 2])