Пример #1
0
 def test_grammarizer_reset_definite_nouns(self):
     paragraph = [
         [Noun('money'), Verb('grab'), Noun('tea'), EXCLAMATION],
         [Pronoun.IT, Verb('have', 'had'), Noun('watch'), PERIOD]
     ]
     grammarizer = Grammarizer(paragraph)
     grammarizer.generate_paragraph()
     for noun_dict in grammarizer.noun_info.values():
         self.assertTrue(noun_dict['definite'])
     grammarizer.reset_definite_nouns()
     for noun_dict in grammarizer.noun_info.values():
         self.assertFalse(noun_dict['definite'])
Пример #2
0
 def test_generate_paragraph_plural_countable_noun(self):
     raw_paragraph = [[Noun('cat'), Verb('grab'), Noun('cat'), EXCLAMATION]]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0, probability_plural_noun=1.0)
     paragraph = grammarizer.generate_paragraph()
     expected = [[Noun('Cats', '', 'cat', tags=self.plural), Verb('grab'),
                  Noun('the cats', '', 'cat', tags=self.definite_plural), EXCLAMATION]]
     self.assertEqual(paragraph, expected)
Пример #3
0
 def test_generate_paragraph_present_tense_third_person_negative(self):
     raw_paragraph = [
         [Noun.uncountable_noun('water'), Verb('grab'), Noun.uncountable_noun('water'), EXCLAMATION],
         [Noun('cat'), Verb('grab'), Noun('cat'), EXCLAMATION],
         [Noun.proper_noun('Joe'), Verb('grab'), Noun('cat')]
     ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=1.0, probability_plural_noun=0.0)
     paragraph = grammarizer.generate_paragraph()
     target_verb = Verb("doesn't grab", '', 'grab', tags=self.negative_third_person)
     for sentence in paragraph:
         self.assertEqual(sentence[1], target_verb)
Пример #4
0
 def test_generate_paragraph_uncountable_noun(self):
     raw_paragraph = [
         [Noun.uncountable_noun('water'), Verb('grab'), Noun.uncountable_noun('water'), PERIOD]
     ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0, probability_plural_noun=1.0)
     paragraph = grammarizer.generate_paragraph()
     expected = [[
         Noun('Water', '', 'water', tags=self.uncountable), Verb('grabs', '', 'grab', tags=self.third_person),
         Noun('the water', '', 'water', tags=self.definite_uncountable), PERIOD
     ]]
     self.assertEqual(paragraph, expected)
Пример #5
0
 def test_generate_paragraph_returns_sentences_with_capitals(self):
     paragraph = [
         [Noun('money'), Verb('grab'), Noun('tea'), EXCLAMATION],
         [Noun('tea'), Verb('ride', 'rode', ''), Noun('apple'), PERIOD],
         [Noun('apple'), Verb('surprise'), Noun('gold'), PERIOD],
         [Noun.proper_noun('the Dude'), Verb('cut', 'cut', ''), Pronoun.IT, PERIOD],
         [Pronoun.IT, Verb('have', 'had', ''), Noun('watch'), PERIOD]
     ]
     grammarizer = Grammarizer(paragraph)
     paragraph = grammarizer.generate_paragraph()
     for sentence in paragraph:
         self.assertIn(sentence[0].value[0], string.ascii_uppercase)
Пример #6
0
 def test_generate_paragraph_present_tense_not_third_person_negative(self):
     raw_paragraph = [[Noun('cat'), Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Pronoun.I, Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Pronoun.YOU, Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Pronoun.WE, Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Pronoun.THEY, Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Noun.proper_noun('Taiwanese', plural=True), Verb('grab'), Noun('cat'), EXCLAMATION]
                      ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=1.0, probability_plural_noun=1.0)
     paragraph = grammarizer.generate_paragraph()
     target_verb = Verb("don't grab", '', 'grab', tags=self.negative)
     for sentence in paragraph:
         self.assertEqual(sentence[1], target_verb)
Пример #7
0
    def test_assign_negatives_no_negative(self):
        raw_paragraph = 5 * [
            [Noun.uncountable_noun('money'), Verb('grab'), Noun('cat'), EXCLAMATION],
            [Noun('witch'), Verb('play'), Noun('dog'), PERIOD],

        ]
        grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0)
        paragraph = grammarizer.generate_paragraph()
        for sentence in paragraph:
            for word in sentence:
                if isinstance(word, Verb):
                    value = word.value
                    tests_false = value.startswith("don't ") or value.startswith("doesn't ")
                    self.assertFalse(tests_false)
Пример #8
0
 def test_generate_paragraph_past_tense_positive(self):
     raw_paragraph = [[Noun.uncountable_noun('water'), Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Noun('cat'), Verb('eat', 'ate', ''), Noun('cat'), EXCLAMATION],
                      [Pronoun.I, Verb('sing', 'sang', ''), Noun('cat'), EXCLAMATION],
                      [Pronoun.YOU, Verb('grab'), Noun('cat'), EXCLAMATION],
                      [Pronoun.WE, Verb('sing', 'sang', ''), Noun('cat'), EXCLAMATION],
                      [Pronoun.THEY, Verb('eat', 'ate', ''), Noun('cat'), EXCLAMATION],
                      ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0,
                               probability_plural_noun=1.0, present_tense=False)
     paragraph = grammarizer.generate_paragraph()
     target_verbs = [Verb('grabbed', '', 'grab', tags=self.past), Verb('ate', 'ate', 'eat', tags=self.past),
                     Verb('sang', 'sang', 'sing', tags=self.past)]
     for sentence in paragraph:
         self.assertIn(sentence[1], target_verbs)
Пример #9
0
 def test_generate_paragraph_multiple_times_resets_indefinte(self):
     raw_paragraph = [
         [Noun.uncountable_noun('money'), Verb('grab'), Noun.uncountable_noun('money'), EXCLAMATION],
         [Noun('cat'), Verb('grab'), Noun('cat'), EXCLAMATION],
     ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0, probability_plural_noun=0.0)
     paragraph_1 = grammarizer.generate_paragraph()
     paragraph_2 = grammarizer.generate_paragraph()
     answer = [
         [Noun.uncountable_noun('money').capitalize(), Verb('grabs', '', 'grab', tags=self.third_person),
          Noun('the money', base='money', tags=self.definite_uncountable), EXCLAMATION],
         [Noun('A cat', base='cat', tags=self.indefinite), Verb('grabs', '', 'grab', tags=self.third_person),
          Noun('the cat', base='cat', tags=self.definite), EXCLAMATION]
     ]
     self.assertEqual(answer, paragraph_1)
     self.assertEqual(answer, paragraph_2)
Пример #10
0
 def test_assign_negatives_some_negative(self):
     random.seed(3)
     raw_paragraph = 10 * [
         [Noun.uncountable_noun('money'), Verb('grab'), Noun('cat'), EXCLAMATION],
     ]
     grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.5)
     paragraph = grammarizer.generate_paragraph()
     negatives = [1, 4, 5, 7, 8]
     for index, sentence in enumerate(paragraph):
         for word in sentence:
             if isinstance(word, Verb):
                 value = word.value
                 is_negative = value.startswith("don't ") or value.startswith("doesn't ")
                 if index in negatives:
                     self.assertTrue(is_negative)
                 else:
                     self.assertFalse(is_negative)
Пример #11
0
    def create_paragraph(self):
        paragraph_generator = self._create_paragraph_generator()
        paragraph_size = self._options['paragraph_size']
        if self._options['paragraph_type'] == 'pool':
            subj_pool = self._options['subject_pool']
            raw_paragraph = paragraph_generator.create_pool_paragraph(
                subj_pool, paragraph_size)
        else:
            raw_paragraph = paragraph_generator.create_chain_paragraph(
                paragraph_size)

        present_tense = self._get_present_tense_bool()
        kwargs = self._get_kwargs('probability_plural_noun',
                                  'probability_negative_verb')
        grammarizer = Grammarizer(raw_paragraph,
                                  present_tense=present_tense,
                                  **kwargs)
        return grammarizer.generate_paragraph()
Пример #12
0
    def test_proper_noun_and_plural_proper_noun_do_not_change_except_capitalize(self):
        raw_paragraph = [
            [Noun.proper_noun('Joe'), Verb('grab'), Noun.proper_noun('the Guys', plural=True), PERIOD],
            [Noun.proper_noun('the Guys', plural=True), Verb('grab'), Noun.proper_noun('Joe'), PERIOD],
        ]

        expected = [
            [Noun.proper_noun('Joe'), Verb('grabs', '', 'grab', tags=self.third_person),
             Noun.proper_noun('the Guys', plural=True), PERIOD],

            [Noun('The Guys', '', 'the Guys', tags=self.plural_proper),
             Verb('grab'), Noun.proper_noun('Joe'), PERIOD],
        ]
        for probility_plural in (0.0, 0.2, 0.5, 1.0):
            grammarizer = Grammarizer(raw_paragraph, probability_negative_verb=0.0,
                                      probability_plural_noun=probility_plural)
            answer = grammarizer.generate_paragraph()
            self.assertEqual(answer, expected)
Пример #13
0
    def test_generate_paragraph_makes_nouns_indefinite_in_first_instance_and_definite_later(self):
        raw_paragraph = 5 * [
            [Noun('money'), Verb('grab'), Noun('cat'), EXCLAMATION],
            [Noun('witch'), Verb('play'), Noun('dog'), PERIOD],
        ]
        grammarizer = Grammarizer(raw_paragraph)
        paragraph = grammarizer.generate_paragraph()
        self.assertEqual(len(paragraph), 10)
        indefinite_part = paragraph[:2]
        definite_part = paragraph[2:]
        for sentence in indefinite_part:
            for word in sentence:
                self.assertFalse(word.has_tags(WordTag.DEFINITE))

        for sentence in definite_part:
            for word in sentence:
                if isinstance(word, Noun):
                    self.assertTrue(word.has_tags(WordTag.DEFINITE))