コード例 #1
0
 def test_indefinite_no_vowel_start(self):
     self.assertEqual(
         Noun('hour').indefinite(),
         Noun('a hour', '', 'hour', tags=self.indefinite))
     self.assertEqual(
         Noun('happy hour').indefinite(),
         Noun('a happy hour', '', 'happy hour', tags=self.indefinite))
コード例 #2
0
    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)
コード例 #3
0
    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])
コード例 #4
0
 def test_indefinite_vowel_start(self):
     self.assertEqual(
         Noun('elephant').indefinite(),
         Noun('an elephant', '', 'elephant', tags=self.indefinite))
     self.assertEqual(
         Noun('old man').indefinite(),
         Noun('an old man', '', 'old man', tags=self.indefinite))
コード例 #5
0
    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)
コード例 #6
0
    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)
コード例 #7
0
 def test_hash(self):
     self.assertEqual(hash(Noun('bob')),
                      hash("hash of Noun('bob', '', 'bob', Tags([]))"))
     self.assertEqual(
         hash(Noun('bob').definite()),
         hash("hash of Noun('the bob', '', 'bob', Tags([WordTag.DEFINITE]))"
              ))
コード例 #8
0
    def test_proper_noun_singular_class_method(self):
        test_1 = Noun.proper_noun('Joe')
        test_2 = Noun.proper_noun('Joe', plural=False)

        expected = Noun('Joe', '', 'Joe', Tags([WordTag.PROPER]))
        self.assertEqual(test_1, expected)
        self.assertEqual(test_2, expected)
コード例 #9
0
    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]))
コード例 #10
0
 def test_is_countable_noun_false(self):
     self.assertFalse(is_countable_noun(BasicWord('dog')))
     self.assertFalse(is_countable_noun(Noun.uncountable_noun('water')))
     self.assertFalse(
         is_countable_noun(Noun.proper_noun('Joe', plural=False)))
     self.assertFalse(
         is_countable_noun(Noun.proper_noun('the Joes', plural=True)))
コード例 #11
0
    def test_equality_true_false_by_base_noun(self):
        test = Noun('a', 'b', 'c', tags=self.proper)
        equal = Noun('a', 'b', 'c', tags=self.proper)
        not_equal = Noun('a', 'b', 'x', tags=self.proper)

        self.assertEqual(test, equal)
        self.assertNotEqual(test, not_equal)
コード例 #12
0
 def test_assign_objects_one_object_no_particle_no_preposition(self):
     verb_group = VerbGroup(verb=Verb('like'),
                            preposition=None,
                            objects=1,
                            particle=None)
     self.assertEqual(assign_objects(verb_group, [Noun('dog')]),
                      [Verb('like'), Noun('dog')])
コード例 #13
0
 def test_definite(self):
     self.assertEqual(
         Noun('hour').definite(),
         Noun('the hour', '', 'hour', tags=self.definite))
     self.assertEqual(
         Noun('ABCs', '', 'ABC').definite(),
         Noun('the ABCs', '', 'ABC', tags=self.definite))
コード例 #14
0
    def test_definite_adds_definite_tag(self):
        tags = Tags([WordTag.PLURAL, WordTag.PAST])
        expected_tags = Tags([WordTag.PLURAL, WordTag.PAST, WordTag.DEFINITE])

        noun = Noun('x', tags=tags)
        self.assertEqual(noun.definite(),
                         Noun('the x', '', 'x', tags=expected_tags))
コード例 #15
0
 def test_plural_with_articles_no_irregular_plural(self):
     articles = ('a ', 'A ', 'an ', 'An ', 'the ', 'The ')
     for article in articles:
         base_value = article + 'thing'
         self.assertEqual(
             Noun(base_value).plural(),
             Noun(base_value + 's', '', base_value, tags=self.plural))
コード例 #16
0
 def test_plural_with_articles_irregular_plural(self):
     articles = ('a ', 'A ', 'an ', 'An ', 'the ', 'The ')
     for article in articles:
         base_value = article + 'child'
         plural_value = article + 'children'
         self.assertEqual(
             Noun(base_value, 'children').plural(),
             Noun(plural_value, 'children', base_value, tags=self.plural))
コード例 #17
0
 def test_assign_objects_two_objects_no_particle_no_preposition(self):
     verb_group = VerbGroup(verb=Verb('show'),
                            preposition=None,
                            objects=2,
                            particle=None)
     self.assertEqual(
         assign_objects(verb_group, [Noun('dog'), Noun('cat')]),
         [Verb('show'), Noun('dog'), Noun('cat')])
コード例 #18
0
 def test_indefinite_all_non_vowels(self):
     vowels = 'aeiouAEIOU'
     for consonant in string.ascii_letters:
         if consonant not in vowels:
             self.assertEqual(
                 Noun(consonant).indefinite(),
                 Noun('a ' + consonant, '', consonant,
                      tags=self.indefinite))
コード例 #19
0
    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)
コード例 #20
0
 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)
コード例 #21
0
    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)
コード例 #22
0
    def test_bold(self):
        noun = Noun('thing', tags=self.plural_proper)
        expected = Noun('<bold>thing</bold>',
                        '',
                        'thing',
                        tags=self.plural_proper)

        self.assertEqual(noun.bold(), expected)
        self.assertEqual(noun.bold().bold().bold(), expected)
コード例 #23
0
    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)
コード例 #24
0
    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)
コード例 #25
0
 def test_compare_sentences_two_words_in_wrong_place(self):
     sentence = Sentence([Noun('a'), Noun('b'), Noun('c'), Noun('d')])
     submission_str = 'a c d b'
     answer = compare_sentences(sentence, submission_str)
     expected = {
         'hint_sentence': 'a <bold>c</bold> <bold>d</bold> b',
         'error_count': 2
     }
     self.assertEqual(answer, expected)
コード例 #26
0
 def test_compare_sentences_LIMITATION_two_separate_words_in_wrong_place_is_handled_incorrectly(
         self):
     sentence = Sentence([Noun('a'), Noun('b'), Noun('c'), Noun('d')])
     submission_str = 'd a c b'
     answer = compare_sentences(sentence, submission_str)
     expected = {
         'hint_sentence': '<bold>d</bold> a c <bold>b</bold>',
         'error_count': 2
     }
     self.assertEqual(answer, expected)
コード例 #27
0
 def test_compare_sentences_repeating_words_no_errors(self):
     sentence = Sentence([
         Noun('dog').plural().capitalize(),
         Verb('dog'),
         Noun('dog').definite(), Punctuation.PERIOD
     ])
     submission_str = 'Dogs dog the dog.'
     answer = compare_sentences(sentence, submission_str)
     expected = {'hint_sentence': submission_str, 'error_count': 0}
     self.assertEqual(answer, expected)
コード例 #28
0
 def test_repr(self):
     self.assertEqual(repr(Noun('bob')), "Noun('bob', '', 'bob', Tags([]))")
     self.assertEqual(
         repr(Noun.uncountable_noun('bob')),
         "Noun('bob', '', 'bob', Tags([WordTag.UNCOUNTABLE]))")
     self.assertEqual(
         repr(Noun.proper_noun('Bob', plural=True)),
         "Noun('Bob', '', 'Bob', Tags([WordTag.PLURAL, WordTag.PROPER]))")
     self.assertEqual(repr(Noun('a', 'b', 'c', Tags([WordTag.PLURAL]))),
                      "Noun('a', 'b', 'c', Tags([WordTag.PLURAL]))")
コード例 #29
0
    def test_capitalize_de_capitalize_regression_test(self):
        for value in ('BMW', 'dog', 'Practice Book'):
            noun = Noun(value)
            plural = noun.plural()
            definite = noun.definite()
            indefinite = noun.indefinite()

            for test_noun in [noun, plural, definite, indefinite]:
                self.assertEqual(test_noun,
                                 test_noun.capitalize().de_capitalize())
コード例 #30
0
    def test_indefinite_only_has_indefinite_tag(self):
        uncountable = Noun.uncountable_noun('water')
        self.assertEqual(uncountable.tags, Tags([WordTag.UNCOUNTABLE]))
        proper = Noun.proper_noun('Joes', plural=True)
        self.assertEqual(proper.tags, self.plural_proper)

        self.assertEqual(uncountable.indefinite(),
                         Noun('a water', '', 'water', tags=self.indefinite))
        self.assertEqual(proper.indefinite(),
                         Noun('a Joes', '', 'Joes', tags=self.indefinite))