Example #1
0
    def test_create_answer_and_error_texts_pronoun_errors(self):
        create_test_csvs(['dog'], ['water'], ['like'], [])

        self.config_state['error_probability'] = 1.0
        self.config_state['probability_pronoun'] = 1.0
        self.config_state['pronoun_errors'] = True
        self.config_state['verb_errors'] = False
        self.config_state['punctuation_errors'] = False

        seed(34349)

        pg = ParagraphsGenerator(self.config_state)
        answer, error = pg.create_answer_and_error_texts()
        expected_answer = (
            "<bold>She</bold> likes <bold>him</bold>. <bold>He</bold> likes <bold>me</bold>! <bold>I</bold> like "
            +
            "it. It likes <bold>me</bold>. <bold>I</bold> don't like <bold>them</bold>. <bold>They</bold> like it"
            +
            ". It likes you. You like <bold>us</bold>. <bold>We</bold> like <bold>her</bold>! <bold>She</bold> li"
            +
            "kes <bold>them</bold>! <bold>They</bold> like <bold>me</bold>. <bold>I</bold> don't like <bold>her</"
            +
            "bold>! <bold>She</bold> likes <bold>us</bold>. <bold>We</bold> like you! You like <bold>me</bold>. -"
            + "- error count: 22")
        expected_error = (
            "Her likes he. Him likes I! Me like it. It likes I. Me don't like they. Them like it. It likes you. Y"
            +
            "ou like we. Us like she! Her likes they! Them like I. Me don't like she! Her likes we. Us like you! "
            + "You like I.")
        self.assertEqual(answer, expected_answer)
        self.assertEqual(error, expected_error)
Example #2
0
    def test_update_options_updates_dict(self):
        pg = ParagraphsGenerator(self.config_state)
        self.assertEqual(pg._options, self.config_state)

        pg.update_options({'paragraph_size': 10})
        self.config_state['paragraph_size'] = 10
        self.assertEqual(pg._options, self.config_state)
Example #3
0
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        try:
            self.wm_iconbitmap(os.path.join(DATA_PATH, 'go_time.ico'))
        except tk.TclError:
            pass

        self.frames = self._pack_set_variable_frames()
        self.load_config()

        try:
            self.paragraph_generator = ParagraphsGenerator(self.get_state())
        except LoaderError as e:
            self.default_word_files()
            self.revert_to_original()
            self.paragraph_generator = ParagraphsGenerator(self.get_state())
            message = (
                'On loading, caught the following error:\n{}: {}\n\n' +
                'The original word files were moved to <name>_old_(number).csv and replaced with new files.'
            )
            showerror('Bad start file',
                      message.format(e.__class__.__name__, e.args[0]))

        for frame in self.frames:
            if isinstance(frame, FileManagement):
                frame.trace_file_names(self.update_paragraph_generator)

        self.do_not_show_popup = tk.IntVar()
        self.do_not_show_popup.set(0)
Example #4
0
    def test_update_options_reloads_lists_if_any_are_None(self):
        create_single_value_test_csvs('cat')
        pg = ParagraphsGenerator(self.config_state)
        pg._verbs_list = []

        create_single_value_test_csvs('dog')
        pg.update_options({'dummy': 10})
        self.assert_single_value_word_list(pg, 'dog')
Example #5
0
 def test_update_options_empty_dict_does_not_change_dict_does_not_reload_files(
         self):
     create_single_value_test_csvs('dog')
     pg = ParagraphsGenerator(self.config_state)
     create_single_value_test_csvs('cat')
     pg.update_options({})
     self.assertEqual(pg._options, self.config_state)
     self.assert_single_value_word_list(pg, 'dog')
Example #6
0
    def test_load_csv_reloads(self):
        create_single_value_test_csvs('cat')
        pg = ParagraphsGenerator(self.config_state)
        self.assert_single_value_word_list(pg, 'cat')

        create_single_value_test_csvs('dog')
        pg.load_lists_from_file()
        self.assert_single_value_word_list(pg, 'dog')
Example #7
0
    def test_create_answer_and_error_texts_punctuation_errors(self):
        create_test_csvs(['dog'], ['water'], ['like'], [])

        self.config_state['error_probability'] = 1.0
        self.config_state['noun_errors'] = False
        self.config_state['verb_errors'] = False

        pg = ParagraphsGenerator(self.config_state)
        answer, error = pg.create_answer_and_error_texts()
        self.assertEqual(error.count(','), 15)
        self.assertTrue(answer.endswith(' -- error count: 15'))
Example #8
0
    def test_create_paragraph_tense(self):
        create_test_csvs(['dog', 'cat'], ['water'], ['like'], ['Dad'])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['probability_negative_verb'] = 0.0
        self.config_state['tense'] = 'simple_past'

        pg = ParagraphsGenerator(self.config_state)
        answer = pg.create_paragraph()
        verb_value = "liked"
        for sentence in answer:
            self.assertEqual(verb_value, sentence[1].value)
Example #9
0
 def test_update_options_does_not_reload_list_if_file_names_are_not_changed(
         self):
     create_single_value_test_csvs('dog')
     pg = ParagraphsGenerator(self.config_state)
     to_change = {
         key: None
         for key in self.config_state
         if key not in ('verbs', 'countable_nouns', 'uncountable_nouns',
                        'proper_nouns')
     }
     create_single_value_test_csvs('cat')
     pg.update_options(to_change)
     self.assert_single_value_word_list(pg, 'dog')
Example #10
0
    def test_create_answer_and_error_texts_verb_errors(self):
        create_test_csvs(['dog'], ['water'], ['like'], [])

        self.config_state['probability_negative_verb'] = 0.0
        self.config_state['error_probability'] = 1.0
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['probability_plural_noun'] = 0.0
        self.config_state['noun_errors'] = False
        self.config_state['punctuation_errors'] = False

        pg = ParagraphsGenerator(self.config_state)
        answer, error = pg.create_answer_and_error_texts()
        self.assertEqual(answer.count('<bold>likes</bold>'), 15)
        self.assertTrue(answer.endswith(' -- error count: 15'))
Example #11
0
    def test_create_answer_and_error_texts_converts_to_texts(self):
        seed(85690)
        create_test_csvs(['dog'], ['water'], ['like'], [])

        self.config_state['error_probability'] = 1.0
        self.config_state['paragraph_size'] = 2
        pg = ParagraphsGenerator(self.config_state)
        answer, error = pg.create_answer_and_error_texts()
        expected_answer = (
            '<bold>Water</bold> <bold>likes</bold> <bold>dogs</bold><bold>.</bold> '
            +
            '<bold>The dogs</bold> <bold>like</bold> <bold>the water</bold><bold>!</bold>'
            + ' -- error count: 8')
        self.assertEqual(answer, expected_answer)
        self.assertEqual(error, 'Waters likes a dogs, the dog like a water,')
Example #12
0
 def test_init_loads_in_csv_paths(self):
     pg = ParagraphsGenerator(self.config_state)
     self.assertEqual(pg._verbs_list, verbs(DELETE_ME_VERBS))
     all_nouns = (countable_nouns(DELETE_ME_COUNTABLE) +
                  uncountable_nouns(DELETE_ME_UNCOUNTABLE) +
                  proper_nouns(DELETE_ME_PROPER))
     self.assertEqual(pg._nouns_list, all_nouns)
Example #13
0
    def test_create_paragraph_chain_paragraph(self):
        seed(4569)
        create_test_csvs(['dog'], ['water'], ['like'], [])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['paragraph_type'] = 'chain'

        pg = ParagraphsGenerator(self.config_state)
        answer = pg.create_paragraph()
        for index, sentence in enumerate(answer):
            if index == 0:
                self.assertEqual(sentence[0].value, 'Water')
                self.assertEqual(sentence[2].value, 'a dog')
            elif index % 2 == 0:
                self.assertEqual(sentence[0].value, 'The water')
                self.assertEqual(sentence[2].value, 'the dog')
            else:
                self.assertEqual(sentence[0].value, 'The dog')
                self.assertEqual(sentence[2].value, 'the water')
Example #14
0
    def test_create_answer_and_error_texts_preposition_transpose_errors(self):
        create_test_csvs(['dog'], [''], ['jump, null, on'], [])

        self.config_state['probability_negative_verb'] = 0.0
        self.config_state['error_probability'] = 1.0
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['probability_plural_noun'] = 0.0
        self.config_state['noun_errors'] = False
        self.config_state['punctuation_errors'] = False
        self.config_state['verb_errors'] = False
        self.config_state['preposition_transpose_errors'] = True

        pg = ParagraphsGenerator(self.config_state)
        answer, error = pg.create_answer_and_error_texts()
        self.assertEqual(answer.count('<bold>on</bold> <bold>the dog</bold>'),
                         15)
        self.assertEqual(error.count('The dog on the dog jumps'), 14)
        self.assertTrue(answer.endswith(' -- error count: 15'))
Example #15
0
    def test_create_paragraph_probability_plural_noun(self):
        create_test_csvs(['dog', 'cat'], ['water'], ['like'], ['Abbey Normal'])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['probability_plural_noun'] = 1.0

        pg = ParagraphsGenerator(self.config_state)
        answer = pg.create_paragraph()
        noun_values = [
            'Dogs', 'The dogs', 'dogs', 'the dogs', 'Cats', 'The cats', 'cats',
            'the cats', 'Water', 'The water', 'water', 'the water',
            'Abbey Normal'
        ]
        for sentence in answer:
            self.assertIn(sentence[0].value, noun_values)
            self.assertIn(sentence[2].value, noun_values)

        pg.update_options({'probability_plural_noun': 0.0})
        answer = pg.create_paragraph()
        noun_values = [
            'A dog', 'The dog', 'a dog', 'the dog', 'A cat', 'The cat',
            'a cat', 'the cat', 'Water', 'The water', 'water', 'the water',
            'Abbey Normal'
        ]
        for sentence in answer:
            self.assertIn(sentence[0].value, noun_values)
            self.assertIn(sentence[2].value, noun_values)
Example #16
0
    def test_create_paragraph_word_lists(self):
        seed(1234)
        create_test_csvs(['dog'], ['water'], ['like'], ['Frank'])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['paragraph_size'] = 2

        pg = ParagraphsGenerator(self.config_state)

        raw_answer = pg.create_paragraph()
        answer = [[word.value for word in sentence] for sentence in raw_answer]
        self.assertEqual(answer, [['Water', "doesn't like", 'Frank', '.'],
                                  ['Frank', 'likes', 'the water', '.']])

        raw_answer = pg.create_paragraph()
        answer = [[word.value for word in sentence] for sentence in raw_answer]
        self.assertEqual(answer, [['Frank', "doesn't like", 'a dog', '.'],
                                  ['The dog', 'likes', 'Frank', '.']])

        raw_answer = pg.create_paragraph()
        answer = [[word.value for word in sentence] for sentence in raw_answer]
        self.assertEqual(answer, [['Water', 'likes', 'Frank', '!'],
                                  ['Frank', 'likes', 'a dog', '.']])
Example #17
0
    def test_create_paragraph_paragraph_size(self):
        self.config_state['paragraph_size'] = 5
        pg = ParagraphsGenerator(self.config_state)
        for _ in range(3):
            self.assertEqual(len(pg.create_paragraph()), 5)

        pg.update_options({'paragraph_size': 2})
        for _ in range(3):
            self.assertEqual(len(pg.create_paragraph()), 2)
Example #18
0
    def test_create_answer_and_error_texts_error_probability(self):
        self.config_state['error_probability'] = 0.0
        self.config_state['probability_pronoun'] = 0.0

        pg = ParagraphsGenerator(self.config_state)
        for _ in range(5):
            answer, error = pg.create_answer_and_error_texts()
            self.assertEqual(answer, error + ' -- error count: 0')

        pg.update_options({'error_probability': 1.0})
        for _ in range(5):
            answer, error = pg.create_answer_and_error_texts()
            self.assertTrue(answer.endswith(' -- error count: 60'))
            self.assertNotEqual(answer, error + ' -- error count: 60')
Example #19
0
    def test_create_paragraph_probability_pronoun(self):
        create_test_csvs(['dog'], ['water'], ['like'], [])
        self.config_state['probability_pronoun'] = 0.0

        pg = ParagraphsGenerator(self.config_state)
        for _ in range(10):
            paragraph = pg.create_paragraph()
            for sentence in paragraph:
                self.assertIsInstance(sentence[0], Noun)
                self.assertIsInstance(sentence[2], Noun)

        pg.update_options({'probability_pronoun': 1.0})
        for _ in range(10):
            paragraph = pg.create_paragraph()
            for sentence in paragraph:
                self.assertIsInstance(sentence[0], CapitalPronoun)
                self.assertIsInstance(sentence[2], Pronoun)
Example #20
0
    def test_create_paragraph_probability_negative_verb(self):
        create_test_csvs(['dog', 'cat'], ['water'], ['like'], ['Mom'])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['probability_plural_noun'] = 0.0
        self.config_state['probability_negative_verb'] = 1.0

        pg = ParagraphsGenerator(self.config_state)
        answer = pg.create_paragraph()
        verb_value = "doesn't like"
        for sentence in answer:
            self.assertEqual(verb_value, sentence[1].value)

        pg.update_options({'probability_negative_verb': 0.0})
        answer = pg.create_paragraph()
        verb_value = "likes"
        for sentence in answer:
            self.assertEqual(verb_value, sentence[1].value)
Example #21
0
    def test_create_paragraph_pool_paragraph(self):
        seed(8908)
        create_test_csvs(['dog', 'cat'], ['water'], ['like'],
                         ['My Great-Aunt Fanny'])
        self.config_state['probability_pronoun'] = 0.0
        self.config_state['paragraph_type'] = 'pool'
        self.config_state['subject_pool'] = 1

        pg = ParagraphsGenerator(self.config_state)
        answer = pg.create_paragraph()
        for sentence in answer:
            self.assertIn('cat', sentence[0].value)
            self.assertIsInstance(sentence[2], Noun)
            self.assertNotIn('cat', sentence[2].value)

        pg.update_options({'subject_pool': 2})
        answer = pg.create_paragraph()
        values = ['Water', 'The water', 'My Great-Aunt Fanny']
        for sentence in answer:
            self.assertIn(sentence[0].value, values)
            self.assertIsInstance(sentence[2], Noun)
Example #22
0
    def test_create_answer_and_error_paragraphs_num_paragraphs(self):
        seed(451)

        self.config_state['paragraph_size'] = 2
        self.config_state['num_paragraphs'] = 3
        pg = ParagraphsGenerator(self.config_state)
        answers, errors = pg.create_answer_and_error_paragraphs()
        expected_answers = [
            "You use <bold>a dog</bold>. The dog <bold>likes</bold> water. -- error count: 2",
            "Water uses <bold>a cat</bold>! The cat doesn't like Joe<bold>.</bold> -- error count: 2",
            "Joe likes <bold>cats</bold>. The cats don't like Bob. -- error count: 1"
        ]
        expected_errors = [
            "You use a dogs. The dog like water.",
            "Water uses cat! The cat doesn't like Joe,",
            "Joe likes cat. The cats don't like Bob."
        ]
        self.assertEqual(answers, expected_answers)
        self.assertEqual(errors, expected_errors)

        pg.update_options({'num_paragraphs': 5})
        answers, errors = pg.create_answer_and_error_paragraphs()
        self.assertEqual(len(answers), 5)
        self.assertEqual(len(errors), 5)
Example #23
0
class MainFrame(tk.Tk):
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        try:
            self.wm_iconbitmap(os.path.join(DATA_PATH, 'go_time.ico'))
        except tk.TclError:
            pass

        self.frames = self._pack_set_variable_frames()
        self.load_config()

        try:
            self.paragraph_generator = ParagraphsGenerator(self.get_state())
        except LoaderError as e:
            self.default_word_files()
            self.revert_to_original()
            self.paragraph_generator = ParagraphsGenerator(self.get_state())
            message = (
                'On loading, caught the following error:\n{}: {}\n\n' +
                'The original word files were moved to <name>_old_(number).csv and replaced with new files.'
            )
            showerror('Bad start file',
                      message.format(e.__class__.__name__, e.args[0]))

        for frame in self.frames:
            if isinstance(frame, FileManagement):
                frame.trace_file_names(self.update_paragraph_generator)

        self.do_not_show_popup = tk.IntVar()
        self.do_not_show_popup.set(0)

    def _pack_set_variable_frames(self):
        error_details = ErrorDetails(master=self)
        error_details.set_bg('light cyan')

        paragraph_type = ParagraphType(master=self)
        paragraph_type.set_bg('honeydew2')

        grammar_details = GrammarDetails(master=self)
        grammar_details.set_bg('snow2')

        file_management = FileManagement(master=self)
        file_management.set_bg('light blue')

        action_frame = Actions(master=self)
        actions = [(action_frame.save_settings, self.set_config),
                   (action_frame.export_settings, self.export_config_file),
                   (action_frame.reload_config, self.load_config),
                   (action_frame.load_config_file, self.load_config_from_file),
                   (action_frame.default_word_files, self.default_word_files),
                   (action_frame.factory_reset, self.revert_to_original),
                   (action_frame.make_pdfs, self.create_texts),
                   (action_frame.read_me, self.read_me)]
        for btn, command in actions:
            btn.config(command=command)

        expand_out = {'sticky': tk.N + tk.S + tk.E + tk.W}
        action_frame.grid(row=0, column=0, columnspan=2)
        error_details.grid(row=1, column=0, rowspan=2, **expand_out)
        paragraph_type.grid(row=1, column=1, **expand_out)
        grammar_details.grid(row=2, column=1, **expand_out)
        file_management.grid(row=3, column=0, columnspan=2, **expand_out)

        return error_details, file_management, grammar_details, paragraph_type, action_frame

    @catch_errors('Bad file')
    def update_paragraph_generator(self, *call_back_args):
        self.paragraph_generator.update_options(self.get_state())

    @catch_errors('Bad file')
    def reload_files(self):
        self.paragraph_generator.load_lists_from_file()

    def default_word_files(self):
        home = self.get_state()['home_directory']
        create_default_word_files(home)

    @catch_errors('bad config')
    def load_config(self):
        loader = ConfigLoader()
        self._load_new_state(loader)

    @catch_errors('bad config file')
    def load_config_from_file(self):
        loader = ConfigLoader()

        filename = askopenfilename(
            initialdir=self.get_state()['home_directory'],
            title='select .cfg file',
            defaultextension='.cfg')
        if not filename:
            return None

        loader.set_state_from_file(filename)
        self._load_new_state(loader)

    def _load_new_state(self, loader):
        try:
            for frame in self.frames:
                loader.set_up_frame(frame)
        except ConfigFileError as error:
            self.revert_to_original()
            raise ConfigFileError(error.args[0])

    def export_config_file(self):
        filename = asksaveasfilename(
            initialdir=self.get_state()['home_directory'],
            title='select .cfg file',
            initialfile='exported_config.cfg',
            defaultextension='.cfg')
        if not filename:
            return None
        save_config_to_filename(self.get_state(), filename)

    def set_config(self):
        save_config(self.get_state())

    def get_state(self):
        answer = {}
        for frame in self.frames:
            answer.update(frame.get_values())
        return answer

    @catch_errors('Uh-oh!')
    def create_texts(self):
        state = self.get_state()
        file_prefix = state['file_prefix']
        font_size = state['font_size']

        self.paragraph_generator.update_options(state)
        self.paragraph_generator.load_lists_from_file()
        answer, error = self.paragraph_generator.create_answer_and_error_paragraphs(
        )
        create_pdf(state['save_directory'],
                   answer,
                   error,
                   error_font_size=font_size,
                   named_prefix=file_prefix)

        if not self.do_not_show_popup.get():
            CancelableMessagePopup(
                'success', 'Your files are located at:\n{}'.format(
                    self.get_state()['save_directory']),
                self.do_not_show_popup)

    def revert_to_original(self):
        loader = ConfigLoader()
        loader.revert_to_default()
        self.load_config()

    def read_me(self):
        popup = tk.Toplevel(self)

        scrollbar = tk.Scrollbar(popup)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        text_frame = ReadMeText(popup)

        text_frame.config(wrap=tk.WORD)
        text_frame.pack(expand=True, fill=tk.BOTH)

        text_frame.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=text_frame.yview)
Example #24
0
    def test_update_options_reloads_lists_if_any_filenames_change(self):
        create_single_value_test_csvs('cat')
        pg = ParagraphsGenerator(self.config_state)

        create_single_value_test_csvs('dog')
        pg._options['verbs'] = 'oops'
        pg.update_options({'verbs': DELETE_ME_VERBS})
        self.assert_single_value_word_list(pg, 'dog')

        create_single_value_test_csvs('cat')
        pg._options['countable_nouns'] = 'oops'
        pg.update_options({'countable_nouns': DELETE_ME_COUNTABLE})
        self.assert_single_value_word_list(pg, 'cat')

        create_single_value_test_csvs('dog')
        pg._options['uncountable_nouns'] = 'oops'
        pg.update_options({'uncountable_nouns': DELETE_ME_UNCOUNTABLE})
        self.assert_single_value_word_list(pg, 'dog')