Beispiel #1
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)
Beispiel #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)
Beispiel #3
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')
Beispiel #4
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')
Beispiel #5
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)
Beispiel #6
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')
Beispiel #7
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')
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
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)
Beispiel #12
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')
Beispiel #13
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)