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 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_iteration_in_function(self): sentence = Sentence([BasicWord('I'), BeVerb.AM, Punctuation.PERIOD]) self.assertTrue(BeVerb.AM in sentence) self.assertFalse(Punctuation.COMMA in sentence) self.assertIn(BasicWord('I'), sentence) self.assertNotIn(BeVerb.WERE, sentence)
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_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 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_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_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_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_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 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_all_words(self): sentence_one = [BasicWord('hi'), BasicWord('there')] sentence_two = [BasicWord('ho'), BasicWord('there')] paragraph = Paragraph([Sentence(sentence_one), Sentence(sentence_two)]) all_words = sentence_one + sentence_two for index, word in enumerate(paragraph.all_words()): self.assertEqual(word, all_words[index])
def test_indexed_all_words(self): sentence_one = [BasicWord('hi'), BasicWord('there'), BasicWord('guy')] sentence_two = [BasicWord('ho'), BasicWord('there')] paragraph = Paragraph([Sentence(sentence_one), Sentence(sentence_two)]) all_sentences = [sentence_one, sentence_two] for s_index, w_index, word in paragraph.indexed_all_words(): self.assertEqual(word, all_sentences[s_index][w_index])
def setUp(self): self.sentence_list = [ Sentence([BasicWord('a'), Verb('b'), BasicWord('c')]), Sentence([BasicWord('d'), Verb('e')]) ] self.tags = Tags([StatusTag.RAW, StatusTag.HAS_PLURALS]) self.paragraph = Paragraph(self.sentence_list, self.tags)
def test_iteration(self): sentence_list = [Sentence([BasicWord('hi')]), Sentence([BasicWord('ho')])] tags = Tags([StatusTag.RAW]) paragraph = Paragraph(sentence_list, tags) for index, sentence in enumerate(paragraph): self.assertEqual(sentence, sentence_list[index]) self.assertTrue(sentence_list[0] in paragraph) self.assertFalse(Sentence([BasicWord('hope')]) in paragraph)
def test_to_obj_with_tagged_BasicWord_in_sentence(self): paragraph = Paragraph([ Sentence([ BasicWord('x', Tags([tag for tag in WordTag])), BasicWord('y') ]) ]) as_dict = Serializer.to_dict(paragraph) self.assertEqual(Serializer.to_obj(as_dict), paragraph)
def get_word( submission_str: str, word: AbstractWord) -> Tuple[AbstractWord, Optional[Tuple[int, int]]]: location = find_word_group(word, submission_str) if location is None: return BasicWord('MISSING'), None sub_str = submission_str[slice(*location)] new_word = BasicWord(sub_str) return new_word, location
def test_from_word_lists(self): word_lists = [[BasicWord('hi')], [BasicWord('ho')]] tags = Tags([StatusTag.RAW]) paragraph = Paragraph.from_word_lists(word_lists, tags) self.assertEqual(paragraph.sentence_list(), [Sentence(lst) for lst in word_lists]) self.assertEqual(paragraph.tags, tags) paragraph = Paragraph.from_word_lists(word_lists) self.assertEqual(paragraph.sentence_list(), [Sentence(lst) for lst in word_lists]) self.assertEqual(paragraph.tags, Tags())
def test_assign_objects_one_object_particle(self): verb_group = VerbGroup(verb=Verb('pick'), preposition=None, objects=1, particle=BasicWord.particle('up')) self.assertEqual( assign_objects(verb_group, [IT]), [Verb('pick'), IT, BasicWord.particle('up')]) self.assertEqual( assign_objects(verb_group, [Noun('dog')]), [Verb('pick'), BasicWord.particle('up'), Noun('dog')])
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_init(self): sentence_list = [Sentence([BasicWord('hi')]), Sentence([BasicWord('ho')])] tags = Tags([StatusTag.RAW]) paragraph = Paragraph(sentence_list, tags) self.assertEqual(paragraph.tags, tags) self.assertEqual(paragraph.sentence_list(), sentence_list) self.assertIsNot(paragraph.tags, tags) self.assertIsNot(paragraph.sentence_list(), sentence_list) old_sentence_list = paragraph.sentence_list() sentence_list[0] = Sentence([BasicWord('yo')]) self.assertEqual(paragraph.sentence_list(), old_sentence_list) self.assertNotEqual(paragraph.sentence_list(), sentence_list)
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_p_negative_gte_one(self): expected_list = [ Sentence([BasicWord('a'), Verb('b').negative(), BasicWord('c')]), Sentence([BasicWord('d'), Verb('e').negative()]) ] to_test = assign_random_negatives(self.paragraph, p_negative=1.0) self.assertEqual(to_test.sentence_list(), expected_list) self.assertEqual(to_test.tags, self.tags.add(StatusTag.HAS_NEGATIVES)) to_test = assign_random_negatives(self.paragraph, p_negative=1.1) self.assertEqual(to_test.sentence_list(), expected_list) self.assertEqual(to_test.tags, self.tags.add(StatusTag.HAS_NEGATIVES))
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_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 test_compare_by_sentence_allows_periods_to_replace_exclamation_points( self): answer = Paragraph([ Sentence([BasicWord('a'), Punctuation.PERIOD]), Sentence([BasicWord('b'), Punctuation.PERIOD]) ]) submission = 'a! b!' comparitor = ParagraphComparison(answer, submission) hints = comparitor.compare_by_sentences() expected = { 'error_count': 0, 'hint_paragraph': submission, 'missing_sentences': 0 } self.assertEqual(hints, expected)
def test_compare_by_words_no_errors(self): answer = Paragraph([ Sentence([BasicWord('a'), Punctuation.PERIOD]), Sentence([BasicWord('b'), Punctuation.PERIOD]) ]) submission = 'a. b.' hint_paragraph = 'a. b.' comparitor = ParagraphComparison(answer, submission) hints = comparitor.compare_by_words() expected = { 'error_count': 0, 'hint_paragraph': hint_paragraph, 'missing_sentences': 0 } self.assertEqual(hints, expected)
def test_compare_by_sentence_answer_has_less_sentences(self): answer = Paragraph([ Sentence([BasicWord('a'), Punctuation.PERIOD]), Sentence([BasicWord('b'), Punctuation.PERIOD]) ]) submission = 'a. c. b.' hint_paragraph = 'a. <bold>c.</bold> <bold>b.</bold>' comparitor = ParagraphComparison(answer, submission) hints = comparitor.compare_by_sentences() expected = { 'error_count': 2, 'hint_paragraph': hint_paragraph, 'missing_sentences': -1 } self.assertEqual(hints, expected)
def test_init(self): answer_paragraph = Paragraph([Sentence([BasicWord('a')])]) submission_str = 'b' comparitor = ParagraphComparison(answer_paragraph, submission_str) self.assertEqual(comparitor.answer, answer_paragraph) self.assertEqual(comparitor.submission, 'b')