def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.dir_path = r"C:\Users\Michał\PycharmProjects\mgr\sgcs\sgcs\data\example gramatics"
        self.filename = 'toy opt los 65534'

        self.sut = SymbolTranslator.create(os.path.join(self.dir_path, self.filename))
    def generic_simulation(self, learning_path, testing_path, name):
        logging.info('starting %s', name)
        with open(os.path.join(r'C:\Users\Michał\PycharmProjects\mgr\runs\auto',
                               name + '.txt'), 'w+') as file:
            learning_set = SymbolTranslator.create(learning_path)
            learning_set.negative_allowed = not self.algorithm_variant.is_stochastic

            testing_set = SymbolTranslator.create(testing_path)
            testing_set.negative_allowed = True

            result, ngen, grammar_estimator, population, *_ = self.sut.perform_simulation(
                learning_set, testing_set, self.configuration)

            print(result)
            print('NGen:', ngen)
            file.write(str(result))
            file.write(str(ngen))
    def prepare_simulation(self, runner, task_no, data_path, config_path, population_path=None):
        with open(config_path) as f:
            configuration = self.configuration_serializer.from_json(json.load(f))

        is_stochastic = configuration.algorithm_variant == AlgorithmVariant.sgcs
        algorithm_variant = CykServiceVariationManager(is_stochastic)
        learning_set_path, testing_set_path = self.load_input_config(data_path)

        learning_set = SymbolTranslator.create(learning_set_path)
        learning_set.negative_allowed = configuration.statistics.negative_sentence_learning

        testing_set = SymbolTranslator.create(testing_set_path)
        testing_set.negative_allowed = True

        return (lambda conf: self._perform_simulation(
            algorithm_variant, learning_set, testing_set, conf, runner, task_no, population_path),
            configuration,
            self._mk_population_printer(learning_set)
        )
Пример #4
0
 def _load_sentences(self):
     self.emit(QtCore.SIGNAL(AsyncProgressDialog.CHANGE_STEP_EVENT), 'Opening file...')
     self.employer.symbol_translator = SymbolTranslator.create(
         self.employer.selected_filename)
     return list(self.employer.symbol_translator.get_sentences())
 def get_learned_translator(population_path):
     symbol_translator = SymbolTranslator.create(population_path)
     list(symbol_translator.get_sentences())
     return symbol_translator
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.tokenizer_mock = create_autospec(EagerTokenizer)
        self.sut = SymbolTranslator(self.tokenizer_mock)
class TestSymbolTranslator(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.tokenizer_mock = create_autospec(EagerTokenizer)
        self.sut = SymbolTranslator(self.tokenizer_mock)

    def perform_get_sentences_scenario(self, sentences):
        first_sentence = next(sentences)
        second_sentence = next(sentences)

        # Then:
        assert_that(calling(next).with_args(sentences), raises(StopIteration))
        assert_that(first_sentence, is_(equal_to(
            Sentence([Symbol(-101), Symbol(-102), Symbol(-103)], is_positive_sentence=True))))
        assert_that(second_sentence, is_(equal_to(
            Sentence([Symbol(-104), Symbol(-102), Symbol(-105)], is_positive_sentence=False))))

        assert_that(self.sut.symbol_to_word(Symbol(-101)), is_(equal_to('ala')))
        assert_that(self.sut.symbol_to_word(Symbol(-102)), is_(equal_to('ma')))
        assert_that(self.sut.symbol_to_word(Symbol(-103)), is_(equal_to('kota')))
        assert_that(self.sut.symbol_to_word(Symbol(-104)), is_(equal_to('kot')))
        assert_that(self.sut.symbol_to_word(Symbol(-105)), is_(equal_to('ale')))
        assert_that(calling(self.sut.symbol_to_word).with_args(Symbol(-106)), raises(UnknownSymbol))

        assert_that(self.sut.word_to_symbol('ala'), is_(equal_to(Symbol(-101))))
        assert_that(self.sut.word_to_symbol('ma'), is_(equal_to(Symbol(-102))))
        assert_that(self.sut.word_to_symbol('kota'), is_(equal_to(Symbol(-103))))
        assert_that(self.sut.word_to_symbol('kot'), is_(equal_to(Symbol(-104))))
        assert_that(self.sut.word_to_symbol('ale'), is_(equal_to(Symbol(-105))))
        assert_that(calling(self.sut.word_to_symbol).with_args('andrzej'), raises(UnknownWord))

    def test_should_be_able_to_get_translated_sentence_generator(self):
        self.tokenizer_mock.get_token_generator.return_value = (iter([
            [True, 'ala', 'ma', 'kota'],
            [False, 'kot', 'ma', 'ale']
        ]))

        sentences = self.sut.get_sentences()
        self.perform_get_sentences_scenario(sentences)
        self.tokenizer_mock.get_token_generator.assert_called_once_with()
        self.tokenizer_mock.get_token_generator.reset_mock()

        sentences = self.sut.get_sentences()
        self.perform_get_sentences_scenario(sentences)
        assert_that(is_not(self.tokenizer_mock.get_token_generator.called))
        self.tokenizer_mock.get_token_generator.reset_mock()

        sentences = self.sut.get_sentences()
        self.perform_get_sentences_scenario(sentences)
        assert_that(is_not(self.tokenizer_mock.get_token_generator.called))

    def test_if_negative_sentences_disabled__they_should_be_ignored(self):
        self.sut.negative_allowed = False
        self.tokenizer_mock.get_token_generator.return_value = (iter([
            [True, 'ala', 'ma', 'kota'],
            [False, 'kot', 'ma', 'ale']
        ]))

        sentences = self.sut.get_sentences()
        assert_that(list(sentences), has_length(1))