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_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_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_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_create_chain_paragraph_loop_safety_finally_returns_paragraph_with_repeat_words( self): random.seed(4564) verb_list = [ VerbGroup(verb=Verb('give'), preposition=None, objects=2, particle=None) ] repeats = RandomParagraph(0.0, verb_list, [Noun('joe'), Noun('bob')]) paragraph = repeats.create_chain_paragraph(3) sentences = [ Sentence( [Noun('bob'), Verb('give'), Noun('joe'), Noun('bob'), PERIOD]), Sentence( [Noun('bob'), Verb('give'), Noun('bob'), Noun('joe'), PERIOD]), Sentence( [Noun('joe'), Verb('give'), Noun('bob'), Noun('joe'), PERIOD]), ] expected = Paragraph(sentences, self.raw_tags) self.assertEqual(expected, paragraph)
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 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 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_error_maker_is_do_errors_p_error_gte_one(self): random.seed(4758) sentences = [ Sentence([Pronoun.HE, Verb('play').third_person()]), Sentence([Verb('stop')]), Sentence([Pronoun.I, Verb('go').negative()]), Sentence([Pronoun.THEY, Verb('go').past_tense()]), Sentence([Pronoun.HIM, Verb('go').past_tense().negative()]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.is_do_errors(1.0).get_paragraph() expected = [ Sentence([Pronoun.HE, BeVerb.IS, Verb('play')]), Sentence([BeVerb.BE, Verb('stop')]), Sentence([Pronoun.I, BeVerb.AM_NOT, Verb('go')]), Sentence([Pronoun.THEY, BeVerb.WERE, Verb('go')]), Sentence([Pronoun.HIM, BeVerb.WAS_NOT, Verb('go')]) ] self.assertEqual(error_paragraph.sentence_list(), expected)
def test_error_maker_pronoun_errors_p_error_gte_one(self): sentences = [ Sentence([ Pronoun.I, Pronoun.ME, Pronoun.YOU, Pronoun.HE, Pronoun.HIM, Pronoun.SHE, Pronoun.HER, Pronoun.IT, Pronoun.WE, Pronoun.US, Pronoun.THEY, Pronoun.THEM ]), Sentence([ CapitalPronoun.I, Pronoun.ME, CapitalPronoun.YOU, CapitalPronoun.HE, Pronoun.HIM, CapitalPronoun.SHE, Pronoun.HER, CapitalPronoun.IT, CapitalPronoun.WE, Pronoun.US, CapitalPronoun.THEY, Pronoun.THEM ]) ] expected = [ Sentence([ Pronoun.ME, Pronoun.I, Pronoun.YOU, Pronoun.HIM, Pronoun.HE, Pronoun.HER, Pronoun.SHE, Pronoun.IT, Pronoun.US, Pronoun.WE, Pronoun.THEM, Pronoun.THEY ]), Sentence([ CapitalPronoun.ME, Pronoun.I, CapitalPronoun.YOU, CapitalPronoun.HIM, Pronoun.HE, CapitalPronoun.HER, Pronoun.SHE, CapitalPronoun.IT, CapitalPronoun.US, Pronoun.WE, CapitalPronoun.THEM, Pronoun.THEY ]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.pronoun_errors(1.0).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), expected) error_paragraph = error_maker.pronoun_errors(1.1).get_paragraph() self.assertEqual(error_paragraph.sentence_list(), 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 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_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_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_get_be_verb_present_tense_I(self): first_person = [ Pronoun.I, Pronoun.ME, CapitalPronoun.I, CapitalPronoun.ME ] for subj in first_person: sentence = Sentence([subj, Verb('go')]) self.assertEqual(get_be_verb(sentence), BeVerb.AM) sentence = Sentence([subj, Verb('go').negative()]) self.assertEqual(get_be_verb(sentence), BeVerb.AM_NOT)
def test_error_maker_noun_errors_p_error_gte_one(self): random.seed(4758) sentences = [ Sentence([Noun('a').definite(), Noun.proper_noun('C')]), Sentence([Noun('d').indefinite(), Noun('e').plural()]), Sentence([Noun.uncountable_noun('f')]) ] paragraph = Paragraph(sentences) error_maker = ErrorMaker(paragraph) error_paragraph = error_maker.noun_errors(1.0).get_paragraph() expected = [ Sentence( [Noun('a').indefinite(), Noun.proper_noun('C').indefinite()]), Sentence([Noun('d'), Noun('e').indefinite()]), Sentence([Noun.uncountable_noun('f').indefinite()]) ] self.assertEqual(error_paragraph.sentence_list(), expected) error_paragraph = error_maker.noun_errors(1.1).get_paragraph() expected = [ Sentence([Noun('a'), Noun.proper_noun('C').definite()]), Sentence([Noun('d'), Noun('e').indefinite()]), Sentence([Noun.uncountable_noun('f').plural()]) ] self.assertEqual(error_paragraph.sentence_list(), expected)
def setUp(self): self.test_paragraph = Paragraph( [ Sentence([ CapitalPronoun.I, Verb('like'), Noun('squirrel').plural(), Punctuation.EXCLAMATION ]), Sentence([ Noun('squirrel').plural().definite().capitalize(), Verb('like'), Pronoun.ME, Punctuation.PERIOD ]) ] )
def _needs_third_person(sentence: Sentence): verb_index = sentence.get_verb() subject_index = sentence.get_subject() if verb_index == -1 or subject_index == -1: return False subject = sentence.get(subject_index) first_person = (Pronoun.I, Pronoun.ME, CapitalPronoun.I, CapitalPronoun.ME) if isinstance(subject, (Noun, AbstractPronoun)) and subject not in first_person: return not subject.has_tags(WordTag.PLURAL) return False
def test_set(self): sentences = [Sentence([BasicWord('hi'), BasicWord('there')]), Sentence([BasicWord('ho')])] tags = Tags([StatusTag.NOUN_ERRORS]) paragraph = Paragraph(sentences, tags) new_paragraph = paragraph.set(0, 1, BasicWord('new')) self.assertEqual(paragraph.sentence_list(), sentences) self.assertEqual(paragraph.tags, tags) expected = [Sentence([BasicWord('hi'), BasicWord('new')]), Sentence([BasicWord('ho')])] self.assertEqual(new_paragraph.sentence_list(), expected) self.assertEqual(new_paragraph.tags, tags)
def test_get_countable_nouns(self): sentence_list = [ Sentence( [Noun.uncountable_noun('water'), BasicWord('is'), Noun('pig')]), Sentence([Noun.proper_noun('Joe'), Noun('pig'), Noun('dog')]) ] paragraph = Paragraph(sentence_list) self.assertEqual(get_countable_nouns(paragraph), [Noun('pig'), Noun('dog')])
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_get_be_verb_past_tense_plural_subject(self): plural_subj = [ Noun('cat').plural(), Noun.proper_noun('the Joes', plural=True), Pronoun.YOU, CapitalPronoun.YOU, Pronoun.WE, Pronoun.US, CapitalPronoun.WE, CapitalPronoun.US, Pronoun.THEY, Pronoun.THEM, CapitalPronoun.THEY, CapitalPronoun.THEM ] for subj in plural_subj: sentence = Sentence([subj, Verb('go').past_tense()]) self.assertEqual(get_be_verb(sentence), BeVerb.WERE) sentence = Sentence([subj, Verb('go').negative().past_tense()]) self.assertEqual(get_be_verb(sentence), BeVerb.WERE_NOT)
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_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_random_plurals_lte_zero(self): raw_sentences = [ Sentence([Noun('a')]), Sentence([Noun('b'), Noun('c')]) ] paragraph = Paragraph(raw_sentences) pa = PluralsAssignment(paragraph) new_paragraph = pa.assign_random_plurals(0.0) self.assertEqual(new_paragraph.sentence_list(), raw_sentences) self.assertEqual(new_paragraph.tags, Tags([StatusTag.HAS_PLURALS])) new_paragraph = pa.assign_random_plurals(-0.1) self.assertEqual(new_paragraph.sentence_list(), raw_sentences) self.assertEqual(new_paragraph.tags, Tags([StatusTag.HAS_PLURALS]))
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_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_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)