def test_gold_04(self): s_plus = {'a', 'aa', 'aaa'} s_minus = set() gold = algorithms.Gold(s_plus, s_minus, {'a', 'b'}) dfa = gold.learn() for s in s_plus: self.assertTrue(dfa.parse_string(s)[1]) for s in s_minus: self.assertFalse(dfa.parse_string(s)[1])
def test_build_automaton_01(self): blue = {'b', 'aa', 'ab'} red = {'', 'a'} ot = utils.ObservationTable(blue, red, {'a', 'b'}) ot.put('', '', 0) ot.put('', 'a', 1) ot.put('a', '', 1) ot.put('a', 'a', 0) ot.put('b', '', 1) ot.put('b', 'a', 0) ot.put('aa', '', 0) ot.put('aa', 'a', 1) ot.put('ab', '', 1) ot.put('ab', 'a', 0) self.assertTrue(ot.is_closed()[0]) self.assertTrue(ot.is_consistent()) self.assertEqual((True, True), ot.is_closed_and_consistent()) gold = algorithms.Gold({'a', 'b'}, set(), {'a', 'b'}) gold._blue = blue gold._red = red dfa = gold._build_automaton(ot) self.assertSetEqual({'a', 'b'}, dfa.alphabet) self.assertTrue(2, len(dfa.states)) self.assertTrue(1, len(dfa.accept_states)) self.assertTrue(1, len(dfa.reject_states)) q1 = automaton.State('') q2 = automaton.State('a') expected_transitions = OrderedDict({ q1: OrderedDict({ 'a': q2, 'b': q2 }), q2: OrderedDict({ 'a': q1, 'b': q2 }) }) self.assertSetEqual(set(map(str, expected_transitions.keys())), set(map(str, dfa._transitions.keys()))) for k in expected_transitions.keys(): for a in expected_transitions[k].keys(): self.assertEqual(expected_transitions[k][a], dfa._transitions[k][a])
def test_gold_01(self): s_plus = {'bb', 'abb', 'bba', 'bbb'} s_minus = {'a', 'b', 'aa', 'bab'} gold = algorithms.Gold(s_plus, s_minus, {'a', 'b'}) expected_table = { '': {'': None, 'a': 0, 'aa': 0, 'ab': None, 'abb': 1, 'b': 0, 'ba': None, 'bab': 0, 'bb': 1, 'bba': 1, 'bbb': 1 }, 'a': {'': 0, 'a': 0, 'aa': None, 'ab': None, 'abb': None, 'b': None, 'ba': None, 'bab': None, 'bb': 1, 'bba': None, 'bbb': None }, 'b': {'': 0, 'a': None, 'aa': None, 'ab': 0, 'abb': None, 'b': 1, 'ba': 1, 'bab': None, 'bb': 1, 'bba': None, 'bbb': None } } ot = gold._build_table() self.assertDictEqual(expected_table, ot.ot)
def __init__(self, alphabet: Set[str], pos_examples: Set[str] = None, neg_examples: Set[str] = None, oracle: Oracle = None, algorithm: str = 'rpni'): """ :param alphabet: Alphabet of the target language we are trying to learn. :type alphabet: Set[str] :param pos_examples: Set of positive example strings from the target language. :type pos_examples: Set[str] :param neg_examples: Set of negative example strings, i.e. strings that do not belong in the target language. :type neg_examples: Set[str] :param oracle: Minimally adequate teacher (MAT) :type oracle: Oracle :param algorithm: The algorithm to use when attempting to learn the grammar from the example strings. The options are: gold rpni lstar nlstar :type algorithm: str """ if not isinstance(alphabet, set) or len(alphabet) == 0: raise ValueError( 'The alphabet has to be a set with at least one element') self._alphabet = alphabet self._learners = { 'gold': lambda: algorithms.Gold(pos_examples, neg_examples, self._alphabet ).learn(), 'rpni': lambda: algorithms.RPNI(pos_examples, neg_examples, self._alphabet) .learn(), 'lstar': lambda: algorithms.LSTAR(self._alphabet, oracle).learn(), 'nlstar': lambda: algorithms.NLSTAR(self._alphabet, oracle).learn() } if algorithm not in self._learners: raise ValueError('Algorithm \'{}\' unknown, the following ' 'algorithms are available:\n{}'.format( algorithms, '\n'.join(self._learners.keys()))) if algorithm in ['rpni', 'gold']: if not isinstance(pos_examples, set): raise ValueError('pos_examples should be a set') if not isinstance(neg_examples, set): raise ValueError('neg_examples should be a set') if len(pos_examples.intersection(neg_examples)) != 0: raise ValueError( 'The sets of positive and negative example ' 'strings should not contain the same string(s)') if pos_examples is None or neg_examples is None: raise ValueError( 'pos_examples and neg_examples can not be None ' 'for algorithm \'{}\''.format(algorithm)) self._alphabet = utils.determine_alphabet( pos_examples.union(neg_examples)) elif algorithm in ['lstar', 'nlstar']: if oracle is None: raise ValueError( 'oracle can not be None for algorithm \'{}\''.format( algorithm)) self._algorithm = algorithm