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 generate_paragraphs(self) -> Tuple[Paragraph, Paragraph]: """ :return: answer, error """ paragraph_size = self.get('paragraph_size') probability_pronoun = self.get('probability_pronoun') generator = RandomParagraph(probability_pronoun, self.get_verbs(), self.get_nouns()) if self.get('paragraph_type') == 'chain': raw = generator.create_chain_paragraph(paragraph_size) else: raw = generator.create_pool_paragraph(self.get('pool_size'), paragraph_size) probability_plural_noun = self.get('probability_plural_noun') with_plurals = PluralsAssignment(raw).assign_random_plurals(probability_plural_noun) probability_negative_verb = self.get('probability_negative_verb') with_negatives = assign_random_negatives(with_plurals, probability_negative_verb) grammarizer = Grammarizer(with_negatives) if self.get('tense') == 'simple_present': answer = grammarizer.grammarize_to_present_tense() else: answer = grammarizer.grammarize_to_past_tense() error_maker = self._create_errors(answer) return answer, error_maker.get_paragraph()
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_create_answer_paragraph_fully_formed_paragraph_singular_now_assigned_plural(self): base_sentences = [Sentence([Noun('dog'), Verb('like'), Noun('cat'), Punctuation.PERIOD])] paragraph = Paragraph(base_sentences) with_negatives = assign_random_negatives(paragraph, 1.0) without_plurals = PluralsAssignment(with_negatives).assign_random_plurals(0.0) grammatical = Grammarizer(without_plurals).grammarize_to_present_tense() self.assertEqual(str(grammatical), "A dog doesn't like a cat.") paragraph_str = "dogs cats" answer = create_answer_paragraph(paragraph_str, grammatical) with_plurals = PluralsAssignment(with_negatives).assign_plural([Noun('dog'), Noun('cat')]) expected = Grammarizer(with_plurals).grammarize_to_present_tense() self.assertEqual(answer, expected) self.assertEqual(str(answer), "Dogs don't like cats.")
def test_assign_random_plurals_middle(self): random.seed(124) word_lists = [Sentence([Noun('a'), Noun('b'), Noun('c')])] paragraph = Paragraph(word_lists) pa = PluralsAssignment(paragraph) expected_plurals = [[Noun('c')], [Noun('b')], [Noun('a'), Noun('b')], [Noun('b'), Noun('c')], [Noun('a'), Noun('b'), Noun('c')]] for index in range(5): new_paragraph = pa.assign_random_plurals(0.5) plurals = expected_plurals[index] test_paragraph = pa.assign_plural(plurals) self.assertEqual(new_paragraph.sentence_list(), test_paragraph.sentence_list())
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_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_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_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_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_assign_random_plurals_gte_one(self): raw_sentences = [ Sentence([Noun('a')]), Sentence([Noun('b'), Noun('c')]) ] paragraph = Paragraph(raw_sentences) pa = PluralsAssignment(paragraph) expected = [ Sentence([Noun('a').plural()]), Sentence([Noun('b').plural(), Noun('c').plural()]) ] new_paragraph = pa.assign_random_plurals(1.0) self.assertEqual(new_paragraph.sentence_list(), expected) self.assertEqual(new_paragraph.tags, Tags([StatusTag.HAS_PLURALS])) new_paragraph = pa.assign_random_plurals(1.1) self.assertEqual(new_paragraph.sentence_list(), expected) self.assertEqual(new_paragraph.tags, Tags([StatusTag.HAS_PLURALS]))
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_assign_plurals(self): raw_sentences = [ Sentence([Noun('x'), Noun('y'), BasicWord('z')]), Sentence([Noun('z'), Noun('x'), Noun('y')]) ] raw_paragraph = Paragraph(raw_sentences, Tags([StatusTag.RAW])) pa = PluralsAssignment(raw_paragraph) plurals = [Noun('x'), Noun('z')] new = pa.assign_plural(plurals) expected = [ Sentence([Noun('x').plural(), Noun('y'), BasicWord('z')]), Sentence([Noun('z').plural(), Noun('x').plural(), Noun('y')]) ] self.assertEqual(new.sentence_list(), expected) self.assertEqual(new.tags, Tags([StatusTag.RAW, StatusTag.HAS_PLURALS]))
def test_assign_random_plurals_add_tag(self): paragraph = Paragraph([], Tags([StatusTag.RAW])) answer = PluralsAssignment(paragraph).assign_random_plurals(0.5) self.assertEqual(answer.tags, Tags([StatusTag.RAW, StatusTag.HAS_PLURALS]))
def test_init(self): sentences = [Sentence([BasicWord('x')])] tags = Tags([StatusTag.RAW]) pa = PluralsAssignment(Paragraph(sentences, tags)) self.assertEqual(pa.raw.sentence_list(), sentences) self.assertEqual(pa.raw.tags, tags)
def _get_plurals_paragraph(base_paragraph, paragraph_str) -> Paragraph: countable_nouns = get_countable_nouns(base_paragraph) plurals = [noun for noun in countable_nouns if noun.plural().value.lower() in paragraph_str.lower()] new_base = PluralsAssignment(base_paragraph).assign_plural(plurals) return new_base