def test_create_chain_paragraph_output(self): random.seed(4567) answer = self.rp.create_chain_paragraph(4) sentences = [ Sentence([Noun('water'), Verb('eat'), Noun('rice'), EXCLAMATION]), Sentence([ Noun('rice'), Verb('give'), US, BasicWord.preposition('to'), Noun('cat'), PERIOD ]), Sentence([Noun('cat'), Verb('eat'), Noun('dog'), PERIOD]), Sentence([ Noun('dog'), Verb('jump'), BasicWord.preposition('over'), Noun('sand'), PERIOD ]), ] expected = Paragraph(sentences, self.raw_tags) self.assertEqual(answer, expected)
def test_create_pool_paragraph_output(self): random.seed(3) paragraph = self.rp.create_pool_paragraph(2, 5) sentences = [ Sentence([Noun('pig'), Verb('eat'), Noun('sand'), PERIOD]), Sentence( [Noun('pig'), Verb('give'), Noun('milk'), Noun('cat'), PERIOD]), Sentence([ Noun('pig'), Verb('jump'), BasicWord.preposition('over'), Noun('water'), PERIOD ]), Sentence([Noun('sand'), Verb('eat'), IT, PERIOD]), Sentence([ Noun('sand'), Verb('give'), Noun('water'), BasicWord.preposition('to'), Noun('milk'), EXCLAMATION ]) ] expected = Paragraph(sentences, self.raw_tags) self.assertEqual(paragraph, expected)
def test_error_maker_preposition_errors_p_error_gte_one(self): sentences = [ Sentence([ Pronoun.I, Verb('go'), BasicWord.preposition('with'), Pronoun.HIM, Punctuation.PERIOD ]), Sentence([ Pronoun.HE, Verb('run'), BasicWord.preposition('over'), Pronoun.IT, Punctuation.PERIOD ]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.preposition_errors(1.0).get_paragraph() expected = [ Sentence([ Pronoun.I, BasicWord.preposition('with'), Pronoun.HIM, Verb('go'), Punctuation.PERIOD ]), Sentence([ Pronoun.HE, BasicWord.preposition('over'), Pronoun.IT, Verb('run'), Punctuation.PERIOD ]) ] self.assertEqual(error_paragraph.sentence_list(), expected)
def setUp(self): self.countable = [Noun('dog'), Noun('cat'), Noun('pig'), Noun('frog')] self.uncountable = [ Noun('water'), Noun('rice'), Noun('milk'), Noun('sand') ] self.verbs = [ VerbGroup(verb=Verb('eat'), preposition=None, objects=1, particle=None), VerbGroup(verb=Verb('give'), preposition=None, objects=2, particle=None), VerbGroup(verb=Verb('jump'), preposition=BasicWord.preposition('over'), objects=1, particle=None), VerbGroup(verb=Verb('lend'), preposition=BasicWord.preposition('to'), objects=2, particle=None), ] self.generator = RandomSentences(self.verbs, self.countable + self.uncountable)
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_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 setUp(self): self.countable = [Noun('dog'), Noun('cat'), Noun('pig'), Noun('frog')] self.uncountable = [ Noun('water'), Noun('rice'), Noun('milk'), Noun('sand') ] self.verbs = [ VerbGroup(verb=Verb('eat'), preposition=None, objects=1, particle=None), VerbGroup(verb=Verb('give'), preposition=None, objects=2, particle=None), VerbGroup(verb=Verb('jump'), preposition=BasicWord.preposition('over'), objects=1, particle=None), VerbGroup(verb=Verb('give'), preposition=BasicWord.preposition('to'), objects=2, particle=None), ] self.rp = RandomParagraph(0.2, self.verbs, self.countable + self.uncountable) self.raw_tags = Tags([StatusTag.RAW])
def test_error_maker_order_of_errors_preposition_errors_affect_is_do_errors( self): sentences = [ Sentence([ CapitalPronoun.I, Verb('play'), Punctuation.PERIOD, BasicWord.preposition('with'), Pronoun.HIM ]), Sentence([ Noun('dog').definite(), Verb('play').third_person(), Punctuation.PERIOD, BasicWord.preposition('with'), Pronoun.HIM ]), ] error_maker = ErrorMaker(Paragraph(sentences)) is_do_preposition = error_maker.is_do_errors(1.0).preposition_errors( 1.0).get_paragraph() expected_str = 'I with him am play. the dog with him is play.' self.assertEqual(str(is_do_preposition), expected_str) preposition_is_do = error_maker.preposition_errors(1.0).is_do_errors( 1.0).get_paragraph() expected_str = 'I with him is play. the dog with him is play.' self.assertEqual(str(preposition_is_do), expected_str)
def test_assign_objects_one_object_preposition(self): verb_group = VerbGroup(verb=Verb('play'), preposition=BasicWord.preposition('with'), objects=1, particle=None) self.assertEqual( assign_objects(verb_group, [IT]), [Verb('play'), BasicWord.preposition('with'), IT]) self.assertEqual( assign_objects(verb_group, [Noun('dog')]), [Verb('play'), BasicWord.preposition('with'), Noun('dog')])
def test_assign_objects_no_objects_particle_or_preposition(self): verb_group = VerbGroup(verb=Verb('chill'), preposition=BasicWord.preposition('out'), objects=0, particle=None) self.assertEqual( assign_objects(verb_group, []), [Verb('chill'), BasicWord.preposition('out')]) verb_group = VerbGroup(verb=Verb('run'), preposition=None, objects=0, particle=BasicWord.particle('away')) self.assertEqual(assign_objects(verb_group, []), [Verb('run'), BasicWord.particle('away')])
def setUp(self): self.config_state = { 'error_probability': 1.0, 'noun_errors': True, 'pronoun_errors': True, 'verb_errors': True, 'is_do_errors': True, 'preposition_transpose_errors': True, 'punctuation_errors': True, 'tense': 'simple_present', 'probability_plural_noun': 1.0, 'probability_negative_verb': 1.0, 'probability_pronoun': 1.0, 'paragraph_type': 'chain', 'subject_pool': 1, 'paragraph_size': 1, } self.verbs = [ VerbGroup(Verb('go'), BasicWord.preposition('with'), BasicWord.particle('away'), 1), VerbGroup(Verb('eat'), None, None, 1) ] self.countable_nouns = [Noun('dog'), Noun('cat')] self.uncountable_nouns = [ Noun.uncountable_noun('water'), Noun.uncountable_noun('air').definite() ] self.static_nouns = [ Noun.proper_noun('Joe'), Noun.proper_noun('The Dude') ] self.word_lists = DummyWordLists(nouns=self.countable_nouns, verbs=self.verbs)
def test_init_verbs(self): verbs = [{ 'verb': 'take', 'irregular_past': 'took', 'objects': 2, 'particle': 'away', 'preposition': 'with' }, { 'verb': 'play', 'irregular_past': '', 'objects': 1, 'particle': '', 'preposition': '' }] lists = WordLists(verbs=verbs) expected_verbs = [ VerbGroup(verb=Verb('take', 'took'), preposition=BasicWord.preposition('with'), particle=BasicWord.particle('away'), objects=2), VerbGroup(verb=Verb('play'), preposition=None, particle=None, objects=1) ] self.assert_unordered_lists(lists.verbs, expected_verbs) self.assertEqual(lists.nouns, [])
def test_assign_objects_two_objects_preposition(self): verb_group = VerbGroup(verb=Verb('bring'), preposition=BasicWord.preposition('to'), objects=2, particle=None) self.assertEqual(assign_objects(verb_group, [HIM, IT]), [Verb('bring'), HIM, BasicWord.preposition('to'), IT]) self.assertEqual( assign_objects(verb_group, [Noun('cat'), Noun('dog')]), [ Verb('bring'), Noun('cat'), BasicWord.preposition('to'), Noun('dog') ])
def test_assign_objects_two_objects_particle_and_preposition(self): verb_group = VerbGroup(verb=Verb('throw'), preposition=BasicWord.preposition('for'), objects=2, particle=BasicWord.particle('away')) self.assertEqual(assign_objects(verb_group, [HIM, IT]), [ Verb('throw'), HIM, BasicWord.particle('away'), BasicWord.preposition('for'), IT ]) self.assertEqual( assign_objects(verb_group, [Noun('cat'), Noun('dog')]), [ Verb('throw'), BasicWord.particle('away'), Noun('cat'), BasicWord.preposition('for'), Noun('dog') ])
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_error_maker_preposition_errors_complex_case(self): sentence = Sentence([ Pronoun.I, Verb('pick'), Pronoun.IT, BasicWord.particle('up'), BasicWord.preposition('with'), Noun('toe').indefinite(), Punctuation.PERIOD ]) expected = Sentence([ Pronoun.I, BasicWord.preposition('with'), Noun('toe').indefinite(), Verb('pick'), Pronoun.IT, BasicWord.particle('up'), Punctuation.PERIOD ]) error_paragraph = ErrorMaker(Paragraph( [sentence])).preposition_errors(1.0).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), [expected])
def test_error_maker_preposition_errors_retains_capital_letters_in_first_word( self): sentences = [ Sentence([Verb('Go'), BasicWord.preposition('with'), Pronoun.HIM]), Sentence([Verb('go'), BasicWord.preposition('with'), Pronoun.HIM]), Sentence([Verb('Eat'), BasicWord.preposition('at'), Noun("Joe's")]) ] error_maker = ErrorMaker(Paragraph(sentences)) error_pargraph = error_maker.pronoun_errors(1.0).get_paragraph() capitals = [0, 2, 3] for index, sentence in enumerate(error_pargraph): first_word = sentence.get(0) if index in capitals: self.assertEqual(first_word.capitalize(), first_word) else: self.assertNotEqual(first_word.capitalize(), first_word)
def test_error_maker_preposition_errors_p_error_lte_zero(self): sentences = [ Sentence([ Pronoun.I, Verb('go'), BasicWord.preposition('with'), Pronoun.HIM ]), Sentence([ Pronoun.HE, Verb('run'), BasicWord.preposition('over'), Pronoun.IT ]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.preposition_errors(0.0).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), sentences) error_paragraph = error_maker.preposition_errors(-1.0).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), sentences)
def test_error_maker_is_do_error_does_not_affect_others(self): sentences = [ Sentence([ BasicWord.preposition('a'), Noun('a'), BeVerb.AM, Pronoun.HIM, CapitalPronoun.ME, Punctuation.COMMA ]) ] error_maker = ErrorMaker(Paragraph(sentences)) error_paragraph = error_maker.is_do_errors(1.0).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), sentences)
def test_init_with_values(self): verb = Verb('go') preposition = BasicWord.preposition('with') particle = BasicWord.particle('away') to_test = VerbGroup( verb=verb, preposition=preposition, particle=particle, objects=2) self.assertEqual(to_test.verb, verb) self.assertEqual(to_test.preposition, preposition) self.assertEqual(to_test.particle, particle) self.assertEqual(to_test.objects, 2)
def test_compare_sentences_extra_words_and_wrong_order(self): sentence = Sentence([ Noun('dog').definite().capitalize(), Verb('play').third_person(), BasicWord.preposition('with'), Noun('cat').indefinite(), Punctuation.PERIOD ]) submission_str = 'extra The dog extra with extra a cat extra plays extra.' answer = compare_sentences(sentence, submission_str) extra = '<bold>extra</bold>' expected_hint = f'{extra} The dog {extra} <bold>with</bold> {extra} <bold>a cat</bold> {extra} plays {extra}.' expected = {'hint_sentence': expected_hint, 'error_count': 7} self.assertEqual(answer, expected)
def test_create_answer_paragraph_uses_base_paragraph_for_word_order_and_punctuation(self): base_sentences = [ Sentence([Noun('dog'), Verb('play'), BasicWord.preposition('with'), Noun('dog'), Punctuation.PERIOD])] base_paragraph = Paragraph(base_sentences) combinations = ('dog dog with play', 'dog dog play with', 'dog with dog play', 'dog with play dog', 'play with dog dog', 'play dog with dog', 'with dog play dog') with_plurals = PluralsAssignment(base_paragraph).assign_plural([]) expected = Grammarizer(with_plurals).grammarize_to_present_tense() for paragraph_str in combinations: answer = create_answer_paragraph(paragraph_str, base_paragraph) self.assertEqual(answer, expected)
def test_predicate(self): random.seed(5) answer = self.generator.predicate() self.assertEqual( answer, [Verb('jump'), BasicWord.preposition('over'), Noun('dog'), PERIOD]) answer = self.generator.predicate() self.assertEqual( answer, [Verb('give'), Noun('pig'), Noun('sand'), PERIOD]) answer = self.generator.predicate() expected = [ Verb('lend'), Noun('frog'), BasicWord.preposition('to'), Noun('pig'), PERIOD ] self.assertEqual(answer, expected)
def test_to_json(self): paragraph = Paragraph([ Sentence([ Verb('go').past_tense().capitalize().bold(), Noun.uncountable_noun('water'), Punctuation.PERIOD ]), Sentence([ BasicWord.preposition('a'), Pronoun.I, CapitalPronoun.ME, BeVerb.AM, Punctuation.COMMA.bold() ]) ], Tags([StatusTag.PUNCTUATION_ERRORS])) as_dict = Serializer.to_dict(paragraph) self.assertEqual(json.dumps(as_dict), Serializer.to_json(paragraph))
def test_makes_copy_of_input_list(self): random.seed(148) for index in range(4): self.countable[index] = Noun('oops') self.uncountable[index] = Noun('oops') self.verbs[index] = VerbGroup(Verb('a'), BasicWord('b'), BasicWord('c'), 4) answer = self.generator.predicate() expected = [ Verb('lend'), Noun('dog'), BasicWord.preposition('to'), Noun('milk'), PERIOD ] self.assertEqual(answer, expected)
def test_from_json(self): paragraph = Paragraph([ Sentence([ Verb('go').past_tense().capitalize().bold(), Noun.uncountable_noun('water'), Punctuation.PERIOD ]), Sentence([ BasicWord.preposition('a'), Pronoun.I, CapitalPronoun.ME, BeVerb.AM, Punctuation.COMMA.bold() ]) ], Tags([StatusTag.RAW])) as_json = Serializer.to_json(paragraph) self.assertEqual(paragraph, Serializer.from_json(as_json))
def test_to_dict_and_back_with_all_word_types_in_multiple_sentences(self): paragraph = Paragraph([ Sentence([ Verb('go').past_tense().capitalize().bold(), Noun.uncountable_noun('water'), Punctuation.PERIOD ]), Sentence([ BasicWord.preposition('a'), Pronoun.I, CapitalPronoun.ME, BeVerb.AM, Punctuation.COMMA.bold() ]) ]) as_dict = Serializer.to_dict(paragraph) as_obj = Serializer.to_obj(as_dict) self.assertEqual(as_obj, paragraph)
def test_eq_false(self): verb = Verb('go') objects = 1 preposition = None particle = BasicWord.particle('x') to_test = VerbGroup(verb, preposition, particle, objects) new_verb = Verb('z') new_objects = 2 new_preposition = BasicWord.preposition('x') new_particle = BasicWord.particle('y') self.assertNotEqual(to_test, VerbGroup(new_verb, preposition, particle, objects)) self.assertNotEqual(to_test, VerbGroup(verb, new_preposition, particle, objects)) self.assertNotEqual(to_test, VerbGroup(verb, preposition, new_particle, objects)) self.assertNotEqual(to_test, VerbGroup(verb, preposition, particle, new_objects))
def _generate_verb_group(verb_json): """ :param verb_json: keys='verb', 'irregular_past', 'objects', 'preposition', 'particle' :return: VerbGroup """ verb = Verb(verb_json['verb'], irregular_past=verb_json['irregular_past']) objects = verb_json['objects'] preposition = BasicWord.preposition(verb_json['preposition']) particle = BasicWord.particle((verb_json['particle'])) if preposition.value == '': preposition = None if particle.value == '': particle = None return VerbGroup(verb=verb, objects=objects, preposition=preposition, particle=particle)
def test_error_maker_preposition_errors_p_error_middle_value(self): random.seed(5812) sentences = [ Sentence([Verb('go'), BasicWord.preposition('with'), Pronoun.ME]), Sentence([ Verb('go'), BasicWord.preposition('over'), BasicWord('there') ]), Sentence([Verb('go'), BasicWord.preposition('into'), Pronoun.IT]), Sentence([ Verb('go'), BasicWord.preposition('under'), BasicWord('that') ]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.preposition_errors(0.5).get_paragraph() expected = [ Sentence([Verb('go'), BasicWord.preposition('with'), Pronoun.ME]), Sentence([ BasicWord.preposition('over'), BasicWord('there'), Verb('go') ]), Sentence([BasicWord.preposition('into'), Pronoun.IT, Verb('go')]), Sentence([ Verb('go'), BasicWord.preposition('under'), BasicWord('that') ]) ] self.assertEqual(error_paragraph.sentence_list(), expected)