def test_hash(self): self.assertEqual(hash(Noun('bob')), hash("hash of Noun('bob', '', 'bob', Tags([]))")) self.assertEqual( hash(Noun('bob').definite()), hash("hash of Noun('the bob', '', 'bob', Tags([WordTag.DEFINITE]))" ))
def test_generate_paragraph_plural_countable_noun(self): raw_paragraph = [[Noun('cat'), Verb('grab'), Noun('cat'), EXCLAMATION]] grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0, probability_plural_noun=1.0) paragraph = grammarizer.generate_paragraph() expected = [[Noun('Cats', '', 'cat', tags=self.plural), Verb('grab'), Noun('the cats', '', 'cat', tags=self.definite_plural), EXCLAMATION]] self.assertEqual(paragraph, expected)
def test_proper_noun_singular_class_method(self): test_1 = Noun.proper_noun('Joe') test_2 = Noun.proper_noun('Joe', plural=False) expected = Noun('Joe', '', 'Joe', Tags([WordTag.PROPER])) self.assertEqual(test_1, expected) self.assertEqual(test_2, expected)
def test_indefinite_no_vowel_start(self): self.assertEqual( Noun('hour').indefinite(), Noun('a hour', '', 'hour', tags=self.indefinite)) self.assertEqual( Noun('happy hour').indefinite(), Noun('a happy hour', '', 'happy hour', tags=self.indefinite))
def test_definite_adds_definite_tag(self): tags = Tags([WordTag.PLURAL, WordTag.PAST]) expected_tags = Tags([WordTag.PLURAL, WordTag.PAST, WordTag.DEFINITE]) noun = Noun('x', tags=tags) self.assertEqual(noun.definite(), Noun('the x', '', 'x', tags=expected_tags))
def test_indefinite_vowel_start(self): self.assertEqual( Noun('elephant').indefinite(), Noun('an elephant', '', 'elephant', tags=self.indefinite)) self.assertEqual( Noun('old man').indefinite(), Noun('an old man', '', 'old man', tags=self.indefinite))
def test_definite(self): self.assertEqual( Noun('hour').definite(), Noun('the hour', '', 'hour', tags=self.definite)) self.assertEqual( Noun('ABCs', '', 'ABC').definite(), Noun('the ABCs', '', 'ABC', tags=self.definite))
def test_random_sentences_sentence_with_phrasal_verb_one_obj_and_preposition( self): random.seed(456123) verb_list = [{ 'verb': Verb('put'), 'preposition': BasicWord.preposition('with'), 'objects': 1, 'particle': BasicWord.particle('up') }] generator = RandomSentences(verb_list, self.countable + self.uncountable) answer = generator.sentence() expected = [ Noun('pig'), Verb('put'), BasicWord.particle('up'), BasicWord.preposition('with'), YOU, PERIOD ] self.assertEqual(answer, expected) answer = generator.sentence() expected = [ Noun('water'), Verb('put'), BasicWord.particle('up'), BasicWord.preposition('with'), Noun('pig'), EXCLAMATION ] self.assertEqual(answer, expected)
def test_get_non_proper_nouns_proper_nouns(self): raw_paragraph = [ [Noun.proper_noun('NASA'), Verb('grab'), Noun.proper_noun('Joe'), EXCLAMATION], [Noun.proper_noun('the Things', plural=True), Verb('cut', 'cut'), Noun.proper_noun('Bob'), PERIOD], [Pronoun.IT, Verb('have', 'had'), Pronoun.THEM, PERIOD] ] self.assertEqual(get_non_proper_nouns(raw_paragraph), [])
def test_assign_preposition(self): random.seed(1234) verb_list = [{ 'verb': Verb('jump'), 'preposition': BasicWord.preposition('on'), 'objects': 1, 'particle': None }] generator = RandomSentences(verb_list, self.countable + self.uncountable) answer = generator.sentence() self.assertEqual(answer, [ Noun('sand'), Verb('jump'), BasicWord.preposition('on'), US, PERIOD ]) answer = generator.sentence() expected = [ Noun('cat'), Verb('jump'), BasicWord.preposition('on'), Noun('frog'), PERIOD ] self.assertEqual(answer, expected)
def test_equality_true_false_by_base_noun(self): test = Noun('a', 'b', 'c', tags=self.proper) equal = Noun('a', 'b', 'c', tags=self.proper) not_equal = Noun('a', 'b', 'x', tags=self.proper) self.assertEqual(test, equal) self.assertNotEqual(test, not_equal)
def test_random_sentences_sentence_with_phrasal_verb_two_obj_and_preposition( self): random.seed(789) verb_list = [{ 'verb': Verb('throw'), 'preposition': BasicWord.preposition('for'), 'objects': 2, 'particle': BasicWord.particle('away') }] generator = RandomSentences(verb_list, self.countable + self.uncountable) answer = generator.sentence() expected = [ Noun('dog'), Verb('throw'), US, BasicWord.particle('away'), BasicWord.preposition('for'), Noun('water'), EXCLAMATION ] self.assertEqual(answer, expected) answer = generator.sentence() expected = [ Noun('rice'), Verb('throw'), BasicWord.particle('away'), Noun('water'), BasicWord.preposition('for'), Noun('frog'), PERIOD ] self.assertEqual(answer, expected)
def test_two_objects_second_obj_is_never_pronoun(self): random.seed(456) verb_list = [ { 'verb': Verb('bring'), 'preposition': BasicWord.preposition('to'), 'objects': 2, 'particle': None }, { 'verb': Verb('give'), 'preposition': None, 'objects': 2, 'particle': None }, ] generator = RandomSentences(verb_list, self.countable + self.uncountable) answer = generator.predicate(1.0) self.assertEqual(answer, [Verb('give'), HER, Noun('dog'), EXCLAMATION]) answer = generator.predicate(1.0) self.assertEqual(answer, [ Verb('bring'), ME, BasicWord.preposition('to'), Noun('sand'), PERIOD ]) answer = generator.predicate(1.0) self.assertEqual( answer, [Verb('give'), HIM, Noun('milk'), EXCLAMATION])
def test_plural_with_articles_no_irregular_plural(self): articles = ('a ', 'A ', 'an ', 'An ', 'the ', 'The ') for article in articles: base_value = article + 'thing' self.assertEqual( Noun(base_value).plural(), Noun(base_value + 's', '', base_value, tags=self.plural))
def test_create_pool_paragraph_repeats_very_very_edge_case(self): random.seed(20) verb_list = [{ 'verb': Verb('give'), 'preposition': None, 'objects': 2, 'particle': None }] repeats = RandomParagraph(0.0, verb_list, [Noun('cat'), Noun('water')]) answer = repeats.create_pool_paragraph(2, 2) expected = [[ Noun('water'), Verb('give'), Noun('cat'), Noun('water'), PERIOD ], [Noun('cat'), Verb('give'), Noun('cat'), Noun('water'), PERIOD]] self.assertEqual(answer, expected) no_prepositions = [ verb_dict for verb_dict in self.verbs if verb_dict['preposition'] is None ] no_repeats = RandomParagraph(0.0, no_prepositions, [Noun('cat'), Noun('water')]) answer = no_repeats.create_pool_paragraph(2, 100) for sentence in answer: self.assertEqual(len(sentence), 4) self.assertNotEqual(sentence[0], sentence[2])
def test_create_pool_paragraph_output(self): random.seed(3) paragraph = self.rp.create_pool_paragraph(2, 5) expected = [[Noun('pig'), Verb('eat'), Noun('sand'), PERIOD], [ Noun('pig'), Verb('give'), Noun('cat'), BasicWord.preposition('to'), Noun('dog'), PERIOD ], [ Noun('sand'), Verb('jump'), BasicWord.preposition('over'), Noun('milk'), EXCLAMATION ], [Noun('sand'), Verb('eat'), HIM, PERIOD], [ Noun('sand'), Verb('jump'), BasicWord.preposition('over'), Noun('milk'), EXCLAMATION ]] self.assertEqual(paragraph, expected)
def test_plural_with_articles_irregular_plural(self): articles = ('a ', 'A ', 'an ', 'An ', 'the ', 'The ') for article in articles: base_value = article + 'child' plural_value = article + 'children' self.assertEqual( Noun(base_value, 'children').plural(), Noun(plural_value, 'children', base_value, tags=self.plural))
def test_indefinite_all_non_vowels(self): vowels = 'aeiouAEIOU' for consonant in string.ascii_letters: if consonant not in vowels: self.assertEqual( Noun(consonant).indefinite(), Noun('a ' + consonant, '', consonant, tags=self.indefinite))
def test_assign_objects_one_object_no_particle_no_preposition(self): verb_dict = { 'verb': Verb('like'), 'preposition': None, 'objects': 1, 'particle': None } self.assertEqual(assign_objects(verb_dict, [Noun('dog')]), [Verb('like'), Noun('dog')])
def test_bold(self): noun = Noun('thing', tags=self.plural_proper) expected = Noun('<bold>thing</bold>', '', 'thing', tags=self.plural_proper) self.assertEqual(noun.bold(), expected) self.assertEqual(noun.bold().bold().bold(), expected)
def assert_single_value_word_list(self, paragraph_generator, dummy_word): self.assertEqual(paragraph_generator._verbs_list[0]['verb'], Verb(dummy_word)) self.assertEqual(paragraph_generator._nouns_list[0], Noun(dummy_word)) self.assertEqual( paragraph_generator._nouns_list[1], Noun.uncountable_noun('uncountable {}'.format(dummy_word))) self.assertEqual(paragraph_generator._nouns_list[2], Noun.proper_noun(dummy_word.capitalize()))
def test_is_word_in_sentence_other(self): sentence = [BasicWord('tom'), Verb('dick'), Noun('harry')] self.assertTrue(is_word_in_sentence(BasicWord('tom'), sentence)) self.assertTrue(is_word_in_sentence(Verb('dick'), sentence)) self.assertTrue(is_word_in_sentence(Noun('harry'), sentence)) self.assertFalse(is_word_in_sentence(Noun('tom'), sentence)) self.assertFalse(is_word_in_sentence(BasicWord('dick'), sentence)) self.assertFalse(is_word_in_sentence(Verb('harry'), sentence))
def test_requires_third_person_no_subject(self): answer = [ Verb('STOP'), BasicWord('in'), Noun('name').definite(), BasicWord('of'), Noun('love'), period, period, period ] self.assertFalse(requires_third_person(answer))
def test_repr(self): self.assertEqual(repr(Noun('bob')), "Noun('bob', '', 'bob', Tags([]))") self.assertEqual( repr(Noun.uncountable_noun('bob')), "Noun('bob', '', 'bob', Tags([WordTag.UNCOUNTABLE]))") self.assertEqual( repr(Noun.proper_noun('Bob', plural=True)), "Noun('Bob', '', 'Bob', Tags([WordTag.PLURAL, WordTag.PROPER]))") self.assertEqual(repr(Noun('a', 'b', 'c', Tags([WordTag.PLURAL]))), "Noun('a', 'b', 'c', Tags([WordTag.PLURAL]))")
def test_assign_objects_two_objects_no_particle_no_preposition(self): verb_dict = { 'verb': Verb('show'), 'preposition': None, 'objects': 2, 'particle': None } self.assertEqual( assign_objects(verb_dict, [Noun('dog'), Noun('cat')]), [Verb('show'), Noun('dog'), Noun('cat')])
def test_to_basic_noun_removes_tags(self): original = Noun.proper_noun('the Things', plural=True) self.assertTrue(original.has_tags(WordTag.PROPER, WordTag.PLURAL)) without_tags = Noun('the Things') self.assertEqual(original.to_basic_noun(), without_tags) original = Noun.uncountable_noun('water') self.assertTrue(original.has_tags(WordTag.UNCOUNTABLE)) without_tags = Noun('water') self.assertEqual(original.to_basic_noun(), without_tags)
def test_find_subject_on_standard_predicate(self): predicate = [ Verb('give'), Noun('frog'), BasicWord.preposition('to'), Noun('pig'), period ] self.assertEqual(find_subject(predicate), -1)
def test_indefinite_only_has_indefinite_tag(self): uncountable = Noun.uncountable_noun('water') self.assertEqual(uncountable.tags, Tags([WordTag.UNCOUNTABLE])) proper = Noun.proper_noun('Joes', plural=True) self.assertEqual(proper.tags, self.plural_proper) self.assertEqual(uncountable.indefinite(), Noun('a water', '', 'water', tags=self.indefinite)) self.assertEqual(proper.indefinite(), Noun('a Joes', '', 'Joes', tags=self.indefinite))
def test_capitalize_de_capitalize_regression_test(self): for value in ('BMW', 'dog', 'Practice Book'): noun = Noun(value) plural = noun.plural() definite = noun.definite() indefinite = noun.indefinite() for test_noun in [noun, plural, definite, indefinite]: self.assertEqual(test_noun, test_noun.capitalize().de_capitalize())
def test_subject_p_pronoun_zero(self): random.seed(10) answer = self.generator.subject(0) self.assertEqual(answer, Noun('milk')) answer = self.generator.subject(-1) self.assertEqual(answer, Noun('dog')) answer = self.generator.subject(-10) self.assertEqual(answer, Noun('sand'))