def test_calculate_distance_is_empty(self): language_detector = LanguageDetector((3, ), 10) empty_sentence = () patches_sentence = ('th', 'er', 'on', 'le', 'ing', 'and') expected = 0 actual_first = language_detector._calculate_distance( empty_sentence, patches_sentence) actual_second = language_detector._calculate_distance( patches_sentence, empty_sentence) self.assertEqual(expected, actual_first) self.assertEqual(expected, actual_second)
def test_calculate_distance_incorrect_input(self): language_detector = LanguageDetector((3, ), 10) patches_ngrams = ((1, 2), (3, 4), (7, 8), (9, 10), (5, 6), (13, 14)) expected = -1 bad_inputs = [[], {}, '', 1, -1, 9.22, None, True, (None, )] for bad_input in bad_inputs: actual_first = language_detector._calculate_distance( bad_input, patches_ngrams) actual_second = language_detector._calculate_distance( patches_ngrams, bad_input) self.assertEqual(expected, actual_first) self.assertEqual(expected, actual_second)
def test_calculate_distance_ideal(self): language_detector = LanguageDetector((2, 3), 10) first_n_grams = ((1, 2), (3, 4), (7, 8), (9, 10), (5, 6), (13, 14)) second_n_grams = ((1, 2), (5, 6), (7, 8), (3, 4), (11, 12), (15, 16)) expected = 17 actual = language_detector._calculate_distance(first_n_grams, second_n_grams) self.assertEqual(expected, actual)