コード例 #1
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
 def test_is_final_state(self):
     f = MutableFST()
     s0 = f.get_initial_state()
     self.assertFalse(f.is_final_state(s0))
     s1 = f.add_state()
     f.set_final_state(s1)
     self.assertFalse(f.is_final_state(s0))
     self.assertTrue(f.is_final_state(s1))
     f.set_final_state(s0)
     self.assertTrue(f.is_final_state(s0))
コード例 #2
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
 def test_det_search_ab_aa(self):
     f = MutableFST()
     s0 = f.get_initial_state()
     s1 = f.add_state()
     s2 = f.add_state()
     s3 = f.add_state()
     f.add_transition(s0, "a", s1)
     f.add_transition(s1, "a", s2).add_transition(s1, "b", s3)
     f.set_final_state(s2).set_final_state(s3)
     f.add_output(s2, "aa").add_output(s3, "ab")
     self.assertEqual(f.det_search("a ab aa"), [])
コード例 #3
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
 def test_final_state(self):
     f = MutableFST()
     s0 = f.get_initial_state()
     s1 = f.add_state()
     self.assertEqual(f.get_final_states(), [])
     f.set_final_state(s1)
     self.assertEqual(f.get_final_states(), [s1])
     self.assertEqual(f.to_dict(), {"initial": s0, "finals": [s1], "transitions": {s0: {}, s1: {}}})
     s2 = f.add_state()
     f.set_final_state(s2)
     self.assertEqual(f.get_final_states(), [s1, s2])
     self.assertEqual(f.to_dict(), {"initial": s0, "finals": [s1, s2], "transitions": {s0: {}, s1: {}, s2: {}}})
コード例 #4
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
 def test_det_search_ab_babb_bb(self):
     # build some fsa
     f = MutableFST()
     s0 = f.get_initial_state()
     s1 = f.add_state()
     s2 = f.add_state()
     s3 = f.add_state()
     s4 = f.add_state()
     s5 = f.add_state()
     s6 = f.add_state()
     s7 = f.add_state()
     f.add_transition(s0, "a", s1).add_transition(s0, "b", s3)
     f.add_transition(s1, "a", s1).add_transition(s1, "b", s2)
     f.add_transition(s2, "a", s4).add_transition(s2, "b", s7)
     f.add_transition(s3, "a", s4).add_transition(s3, "b", s7)
     f.add_transition(s4, "a", s1).add_transition(s4, "b", s5)
     f.add_transition(s5, "a", s4).add_transition(s5, "b", s6)
     f.add_transition(s6, "a", s4).add_transition(s6, "b", s7)
     f.add_transition(s7, "a", s4).add_transition(s7, "b", s7)
     f.set_final_state(s2).set_final_state(s5).set_final_state(s6).set_final_state(s7)
     f.add_output(s2, "ab").add_output(s5, "ab").add_output(s6, "bb").add_output(s6, "babb").add_output(s7, "bb")
     self.assertEqual(
         f.to_dict(),
         {
             "initial": s0,
             "finals": [s2, s5, s6, s7],
             "outputs": {s2: ["ab"], s5: ["ab"], s6: ["babb", "bb"], s7: ["bb"]},
             "transitions": {
                 s0: {"a": s1, "b": s3},
                 s1: {"a": s1, "b": s2},
                 s2: {"a": s4, "b": s7},
                 s3: {"a": s4, "b": s7},
                 s4: {"a": s1, "b": s5},
                 s5: {"a": s4, "b": s6},
                 s6: {"a": s4, "b": s7},
                 s7: {"a": s4, "b": s7},
             },
         },
     )
     self.assertEqual(f.get_letters(), ["a", "b"])
     # test det_search simulation on babba
     self.assertEqual(f.get_target(s0, "b"), s3)
     self.assertEqual(f.get_target(s3, "a"), s4)
     self.assertEqual(f.get_target(s4, "b"), s5)
     self.assertEqual(f.get_target(s5, "b"), s6)
     self.assertEqual(f.get_target(s6, "a"), s4)
     # test det_search
     outputs = f.det_search("babba")
     # babba
     # 01234
     self.assertEqual(outputs, [("ab", 1, 3), ("babb", 0, 4), ("bb", 2, 4)])
     for o in outputs:
         self.assertEqual("babba"[o[1] : o[2]], o[0])
コード例 #5
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
    def test_to_dot(self):
        f = MutableFST()
        # initial state
        s0 = f.get_initial_state()
        expected_dot = """digraph FST {
rankdir = LR;
label = "";
center = 1;
ranksep = "0.4";
nodesep = "0.25";
0 [label = "0", shape = circle, style = bold, fontsize = 14]
}"""
        self.assertEqual(f.to_dot().split("\n"), expected_dot.split("\n"))
        # add state
        s1 = f.add_state()
        expected_dot = """digraph FST {
rankdir = LR;
label = "";
center = 1;
ranksep = "0.4";
nodesep = "0.25";
0 [label = "0", shape = circle, style = bold, fontsize = 14]
1 [label = "1", shape = circle, style = bold, fontsize = 14]
}"""
        self.assertEqual(f.to_dot().split("\n"), expected_dot.split("\n"))
        # add transition
        f.add_transition(s0, "a", s1)
        expected_dot = """digraph FST {
rankdir = LR;
label = "";
center = 1;
ranksep = "0.4";
nodesep = "0.25";
0 [label = "0", shape = circle, style = bold, fontsize = 14]
	0 -> 1 [label = "a", fontsize = 14];
1 [label = "1", shape = circle, style = bold, fontsize = 14]
}"""
        self.assertEqual(f.to_dot().split("\n"), expected_dot.split("\n"))
        # add final state
        f.set_final_state(s1)
        # check to_dot
        expected_dot = """digraph FST {
rankdir = LR;
label = "";
center = 1;
ranksep = "0.4";
nodesep = "0.25";
0 [label = "0", shape = circle, style = bold, fontsize = 14]
	0 -> 1 [label = "a", fontsize = 14];
1 [label = "1", shape = doublecircle, style = bold, fontsize = 14]
}"""
        self.assertEqual(f.to_dot().split("\n"), expected_dot.split("\n"))
コード例 #6
0
ファイル: test_fst.py プロジェクト: pombredanne/automaton-3
 def test_get_stats(self):
     # build some fsa
     f = MutableFST()
     s0 = f.get_initial_state()
     s1 = f.add_state()
     s2 = f.add_state()
     s3 = f.add_state()
     s4 = f.add_state()
     s5 = f.add_state()
     s6 = f.add_state()
     s7 = f.add_state()
     f.add_transition(s0, "a", s1).add_transition(s0, "b", s3)
     f.add_transition(s1, "a", s1).add_transition(s1, "b", s2)
     f.add_transition(s2, "a", s4).add_transition(s2, "b", s7)
     f.add_transition(s3, "a", s4).add_transition(s3, "b", s7)
     f.add_transition(s4, "a", s1).add_transition(s4, "b", s5)
     f.add_transition(s5, "a", s4).add_transition(s5, "b", s6)
     f.add_transition(s6, "a", s4).add_transition(s6, "b", s7)
     f.add_transition(s7, "a", s4).add_transition(s7, "b", s7)
     f.set_final_state(s2).set_final_state(s5).set_final_state(s6).set_final_state(s7)
     f.add_output(s2, "ab").add_output(s5, "ab").add_output(s6, "bb").add_output(s6, "babb").add_output(s7, "bb")
     self.assertEqual(
         f.to_dict(),
         {
             "initial": s0,
             "finals": [s2, s5, s6, s7],
             "outputs": {s2: ["ab"], s5: ["ab"], s6: ["babb", "bb"], s7: ["bb"]},
             "transitions": {
                 s0: {"a": s1, "b": s3},
                 s1: {"a": s1, "b": s2},
                 s2: {"a": s4, "b": s7},
                 s3: {"a": s4, "b": s7},
                 s4: {"a": s1, "b": s5},
                 s5: {"a": s4, "b": s6},
                 s6: {"a": s4, "b": s7},
                 s7: {"a": s4, "b": s7},
             },
         },
     )
     # test get_stats
     self.assertEqual(f.get_stats(), {"numStates": 8, "numFinalStates": 4, "numTransitions": 16})