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_error_maker_punctuation_errors_p_error_middle_value(self): random.seed(5812) sentences = [ Sentence([CapitalPronoun.I, Verb('go'), Punctuation.PERIOD]), Sentence([CapitalPronoun.HE, Verb('run'), Punctuation.EXCLAMATION]), Sentence([Noun('dog').definite().capitalize(), Punctuation.PERIOD]), Sentence([BasicWord('A'), Punctuation.PERIOD]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.punctuation_errors(0.5).get_paragraph() expected = [ Sentence([CapitalPronoun.I, Verb('go'), Punctuation.PERIOD]), Sentence([CapitalPronoun.HE, Verb('run'), Punctuation.COMMA]), Sentence([Noun('dog').definite(), Punctuation.COMMA]), Sentence([BasicWord('a'), Punctuation.PERIOD]) ] self.assertEqual(error_paragraph.sentence_list(), expected)
def test_predicate_two_objects_second_obj_is_never_pronoun(self): random.seed(456) verb_list = [ VerbGroup(verb=Verb('bring'), preposition=BasicWord.preposition('to'), objects=2, particle=None), VerbGroup(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_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_sentence_particle(self): random.seed(47) nouns = [Noun('dog')] verbs = [ VerbGroup(verb=Verb('pick'), objects=1, preposition=None, particle=BasicWord.particle('up')) ] generator = RandomSentences(verbs, nouns) subj = HE sentence = generator.sentence(subj, p_pronoun=1.0) expected = Sentence( [HE, Verb('pick'), US, BasicWord.particle('up'), PERIOD]) self.assertEqual(sentence, expected) sentence = generator.sentence(subj, p_pronoun=0.0) expected = Sentence( [HE, Verb('pick'), BasicWord.particle('up'), Noun('dog'), PERIOD]) self.assertEqual(sentence, expected)
def test_sentence_particle_preposition(self): random.seed(2743) nouns = [Noun('dog'), Noun('cat')] verbs = [ VerbGroup(verb=Verb('pick'), objects=2, preposition=BasicWord.preposition('with'), particle=BasicWord.particle('up')) ] generator = RandomSentences(verbs, nouns) subj = HE sentence = generator.sentence(subj, p_pronoun=1.0) expected = Sentence([ HE, Verb('pick'), THEM, BasicWord.particle('up'), BasicWord.preposition('with'), Noun('cat'), PERIOD ]) self.assertEqual(sentence, expected) sentence = generator.sentence(subj, p_pronoun=0.0) expected = Sentence([ HE, Verb('pick'), BasicWord.particle('up'), Noun('cat'), BasicWord.preposition('with'), Noun('dog'), EXCLAMATION ]) self.assertEqual(sentence, expected)
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_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_init_reverts_countable_nouns_and_removes_tag(self): original_sentences = [ Sentence([ Noun('x').plural(), Noun('y'), Noun.uncountable_noun('z'), Noun.proper_noun('A', plural=True), Noun.uncountable_noun('q').definite() ]) ] original_tags = Tags([StatusTag.HAS_PLURALS, StatusTag.RAW]) original_paragraph = Paragraph(original_sentences, original_tags) pa = PluralsAssignment(original_paragraph) self.assertEqual(original_paragraph.sentence_list(), original_sentences) self.assertEqual(original_paragraph.tags, original_tags) expected = [ Sentence([ Noun('x'), Noun('y'), Noun.uncountable_noun('z'), Noun.proper_noun('A', plural=True), Noun.uncountable_noun('q').definite() ]) ] self.assertEqual(pa.raw.sentence_list(), expected) self.assertEqual(pa.raw.tags, Tags([StatusTag.RAW]))
def test_is_countable_noun_false(self): self.assertFalse(is_countable_noun(BasicWord('dog'))) self.assertFalse(is_countable_noun(Noun.uncountable_noun('water'))) self.assertFalse( is_countable_noun(Noun.proper_noun('Joe', plural=False))) self.assertFalse( is_countable_noun(Noun.proper_noun('the Joes', plural=True)))
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_assign_objects_one_object_no_particle_no_preposition(self): verb_group = VerbGroup(verb=Verb('like'), preposition=None, objects=1, particle=None) self.assertEqual(assign_objects(verb_group, [Noun('dog')]), [Verb('like'), Noun('dog')])
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_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_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_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_assign_objects_two_objects_no_particle_no_preposition(self): verb_group = VerbGroup(verb=Verb('show'), preposition=None, objects=2, particle=None) self.assertEqual( assign_objects(verb_group, [Noun('dog'), Noun('cat')]), [Verb('show'), Noun('dog'), Noun('cat')])
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_create_answer_paragraph_picks_plural_when_plural_and_singular_present(self): base_sentences = [Sentence([Noun('dog'), Verb('like'), Noun('dog'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences) with_plurals = PluralsAssignment(base_paragraph).assign_plural([Noun('dog')]) expected = Grammarizer(with_plurals).grammarize_to_present_tense() for paragraph_Str in ('dogs like a dog.', 'a dog like dogs.'): answer = create_answer_paragraph(paragraph_Str, base_paragraph) self.assertEqual(answer, expected)
def test_create_answer_paragraph_makes_grammatically_correct_paragraph_if_base_paragraph_word_order_is_ok(self): base_sentences = [Sentence([Pronoun.HE, Verb('like'), Noun('dog'), Punctuation.PERIOD]), Sentence([Noun('dog'), Verb('go'), BasicWord.preposition('to'), Noun('house'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences) answer = create_answer_paragraph('', base_paragraph) with_plurals = PluralsAssignment(base_paragraph).assign_plural([]) grammatical = Grammarizer(with_plurals).grammarize_to_present_tense() self.assertEqual(answer, grammatical)
def test_create_answer_paragraph_is_in_past_tense_if_has_tag_simple_past(self): base_sentences = [Sentence([Noun('dog'), Verb('like'), Noun('cat'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences, Tags([StatusTag.SIMPLE_PAST])) paragaph_str = 'A dog liked cats,' answer = create_answer_paragraph(paragaph_str, base_paragraph) with_plurals = PluralsAssignment(base_paragraph).assign_plural([Noun('cat')]) expected = Grammarizer(with_plurals).grammarize_to_past_tense() self.assertEqual(answer, expected)
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 test_create_answer_paragraph_can_pick_plural_when_paragraph_str_has_capital_letters(self): base_sentences = [Sentence([Noun('dog'), Verb('like'), Noun('cat'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences) paragaph_str = 'DOGS like CATS' answer = create_answer_paragraph(paragaph_str, base_paragraph) with_plurals = PluralsAssignment(base_paragraph).assign_plural([Noun('dog'), Noun('cat')]) expected = Grammarizer(with_plurals).grammarize_to_present_tense() self.assertEqual(answer, expected)
def test_create_answer_paragraph_makes_paragraph_according_to_plurals_in_paragraph_str(self): base_sentences = [Sentence([Noun('dog'), Verb('like'), Noun('cat'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences) paragaph_str = 'A dog like cats,' answer = create_answer_paragraph(paragaph_str, base_paragraph) with_plurals = PluralsAssignment(base_paragraph).assign_plural([Noun('cat')]) expected = Grammarizer(with_plurals).grammarize_to_present_tense() self.assertEqual(answer, expected)
def test_compare_sentences_two_words_in_wrong_place(self): sentence = Sentence([Noun('a'), Noun('b'), Noun('c'), Noun('d')]) submission_str = 'a c d b' answer = compare_sentences(sentence, submission_str) expected = { 'hint_sentence': 'a <bold>c</bold> <bold>d</bold> b', 'error_count': 2 } self.assertEqual(answer, expected)
def test_compare_sentences_LIMITATION_two_separate_words_in_wrong_place_is_handled_incorrectly( self): sentence = Sentence([Noun('a'), Noun('b'), Noun('c'), Noun('d')]) submission_str = 'd a c b' answer = compare_sentences(sentence, submission_str) expected = { 'hint_sentence': '<bold>d</bold> a c <bold>b</bold>', 'error_count': 2 } self.assertEqual(answer, expected)
def test_compare_sentences_repeating_words_no_errors(self): sentence = Sentence([ Noun('dog').plural().capitalize(), Verb('dog'), Noun('dog').definite(), Punctuation.PERIOD ]) submission_str = 'Dogs dog the dog.' answer = compare_sentences(sentence, submission_str) expected = {'hint_sentence': submission_str, 'error_count': 0} self.assertEqual(answer, expected)
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_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_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))