def maketest(n, testcases): babbler = Babbler(n) babbler.add_file(testcases) starters = str(sorted(babbler.get_starters())) stoppers = str(sorted(babbler.get_stoppers())) ngrams = str(sorted(babbler.get_all_ngrams())) next_states = {} for state in sorted(babbler.get_all_ngrams()): next_states[state] = babbler.get_successors(state) next_states = str(next_states).replace('], ', '],\n\t') return maketestcode(f'TestMarkovBabbler{n}', n, testcases, starters, stoppers, ngrams, next_states)
def main(n, filename): babbler = Babbler(n) babbler.add_file(filename) st = '' startpct = percents(babbler.get_starters()) for starter in startpct.keys(): name = getnode(starter) st += f'start -> {name} [label = {startpct[starter]:0.2f}];\n' links = '' tovisit = startpct.keys() visited = set() while len(tovisit) > 0: state = tovisit.pop() if state in visited: continue visited.add(state) name = getnode(state) if not babbler.has_successor(state): links += f'{name} -> end [label = 1.0]\n' continue successorpct = percents(babbler.get_successors(state)) for nextnode in successorpct.keys(): if nextnode not in visited: tovisit.append(nextnode) nextname = getnode(nextnode) links += f'{name} -> {nextname} [label = {successorpct[nextnode]:0.2f}];\n' nodes = '' global names for state, name in names.items(): nodes += f'{name} [label={state}]\n' # TODO: convert visited into nodes with labels # figure out transition labels graph = """ digraph finite_state_machine { rankdir=LR; #size="8,5" node [shape = doublecircle]; start end; node [shape = circle]; %s %s %s } """ % (nodes, st, links) print(graph)
class TestMarkovBabbler3(unittest.TestCase): def setUp(self): warnings.simplefilter("ignore") self.babbler = Babbler(3) self.babbler.add_file('tests/test2.txt') def test_starters(self): expected = ['a b c', 'a b c', 'a b c', 'a b c', 'x y z', 'x y z'] actual = self.babbler.get_starters() self.assertEqual(expected, sorted(actual)) def test_stoppers(self): expected = ['b c !', 'b c ?', 'c d .', 'c e .', 'd e .', 'y z .'] actual = self.babbler.get_stoppers() self.assertEqual(expected, sorted(actual)) def test_ngrams(self): expected = [ 'a b c', 'b c !', 'b c ?', 'b c d', 'b c e', 'b c x', 'c d .', 'c d e', 'c e .', 'c x y', 'd e .', 'x y z', 'y z .', 'y z a', 'y z z', 'z a b', 'z z a' ] actual = self.babbler.get_all_ngrams() self.assertEqual(expected, sorted(actual)) def test_next_states(self): expected = { 'a b c': ['d', 'e', 'd', 'x', '!', '?'], 'b c !': ['EOL'], 'b c ?': ['EOL'], 'b c d': ['.', 'e'], 'b c e': ['.'], 'b c x': ['y'], 'c d .': ['EOL'], 'c d e': ['.'], 'c e .': ['EOL'], 'c x y': ['z'], 'd e .': ['EOL'], 'x y z': ['.', 'a', 'z'], 'y z .': ['EOL'], 'y z a': ['b'], 'y z z': ['a'], 'z a b': ['c', 'c'], 'z z a': ['b'] } for state in self.babbler.get_all_ngrams(): actual = sorted(self.babbler.get_successors(state)) correct = sorted(expected[state]) self.assertEqual( actual, correct, f"state '{state}' next should be {correct} but instead was {actual}" )
def setUp(self): warnings.simplefilter("ignore") self.babbler = Babbler(2) self.babbler.add_file('tests/test2.txt')
def main(n, filename): babbler = Babbler(n) babbler.add_file(filename) st = '' startpct = percents(babbler.get_starters()) for starter in startpct.keys(): name = getnode(starter) st += f'start -> {name} [label = {startpct[starter]:0.2f} fontsize="36"];\n' links = '' tovisit = list(startpct.keys()) visited = set() while len(tovisit) > 0: state = tovisit.pop(0) eprint(f'state {state}') if state in visited: continue visited.add(state) name = getnode(state) if not babbler.has_successor(state): links += f'{name} -> end [label = 1.0 fontsize="36"];\n' continue eprint(f'{state} successors: {babbler.get_successors(state)}') successors = [(word, fix(state, word)) for word in babbler.get_successors(state)] eprint(f'successors of {state}: {successors}') successorpct = percents(successors) eprint(f'successorpct {successorpct}') for (word, nextstate), weight in successorpct.items(): if word == 'EOL': links += f'{name} -> end [label = "{weight:.2f}" fontsize="36"];\n' continue nextname = getnode(nextstate) if nextname not in visited: tovisit.append(nextstate) links += f'{name} -> {nextname} [label = "{word}\\n{weight:0.2f}" fontsize="36"];\n' nodes = '' global names for state, name in names.items(): nodes += f'{name} [label="{state}"];\n' graph = """ digraph finite_state_machine { rankdir=LR; size="8,5" node [shape = doublecircle fontsize="36"]; start end; node [shape = circle fontsize="36"]; %s %s %s } """ % (nodes, st, links) print(graph)