Skip to content

Commit 132ee96

Browse files
committed
correct fst networkx transitions
1 parent 1bb9c7a commit 132ee96

File tree

11 files changed

+36
-28
lines changed

11 files changed

+36
-28
lines changed

pyformlang/cfg/tests/test_terminal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def test_creation(self):
2424
assert epsilon.to_text() == "epsilon"
2525
assert Terminal("C").to_text() == '"TER:C"'
2626
assert repr(Epsilon()) == "epsilon"
27+
assert str(terminal0) == "0"
28+
assert repr(terminal0) == "Terminal(0)"
2729

2830
def test_eq(self):
2931
assert "epsilon" == Epsilon()

pyformlang/cfg/tests/test_variable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ def test_creation(self):
2020
assert str(variable0) == str(variable3)
2121
assert str(variable0) != str(variable1)
2222
assert "A" == Variable("A")
23+
assert str(variable1) == "1"
24+
assert repr(variable1) == "Variable(1)"

pyformlang/finite_automaton/finite_automaton.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,10 @@ def __try_add(set_to_add_to: Set[Any], element_to_add: Any) -> bool:
700700
@staticmethod
701701
def __add_start_state_to_graph(graph: MultiDiGraph, state: State) -> None:
702702
""" Adds a starting node to a given graph """
703-
graph.add_node("starting_" + str(state.value),
703+
graph.add_node("starting_" + str(state),
704704
label="",
705705
shape=None,
706706
height=.0,
707707
width=.0)
708-
graph.add_edge("starting_" + str(state.value),
708+
graph.add_edge("starting_" + str(state),
709709
state.value)

pyformlang/finite_automaton/tests/test_state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_repr(self):
2222
assert str(state1) == "ABC"
2323
state2 = State(1)
2424
assert str(state2) == "1"
25+
assert repr(state1) == "State(ABC)"
2526

2627
def test_eq(self):
2728
""" Tests the equality of states

pyformlang/finite_automaton/tests/test_symbol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_repr(self):
2222
assert str(symbol1) == "ABC"
2323
symbol2 = Symbol(1)
2424
assert str(symbol2) == "1"
25+
assert repr(symbol2) == "Symbol(1)"
2526

2627
def test_eq(self):
2728
""" Tests equality of symbols

pyformlang/fst/fst.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from typing import Dict, List, Set, Tuple, Iterator, Iterable, Hashable
44
from copy import deepcopy
5-
from json import dumps, loads
65

76
from networkx import MultiDiGraph
87
from networkx.drawing.nx_pydot import write_dot
@@ -417,26 +416,26 @@ def to_networkx(self) -> MultiDiGraph:
417416
"""
418417
graph = MultiDiGraph()
419418
for state in self._states:
420-
graph.add_node(state,
419+
graph.add_node(state.value,
421420
is_start=state in self.start_states,
422421
is_final=state in self.final_states,
423422
peripheries=2 if state in self.final_states else 1,
424-
label=state)
423+
label=state.value)
425424
if state in self.start_states:
426425
graph.add_node("starting_" + str(state),
427426
label="",
428427
shape=None,
429428
height=.0,
430429
width=.0)
431430
graph.add_edge("starting_" + str(state),
432-
state)
433-
for s_from, input_symbol in self._delta:
434-
for s_to, output_symbols in self._delta[(s_from, input_symbol)]:
435-
graph.add_edge(
436-
s_from,
437-
s_to,
438-
label=(dumps(input_symbol) + " -> " +
439-
dumps(output_symbols)))
431+
state.value)
432+
for (s_from, input_symbol), (s_to, output_symbols) in self:
433+
input_symbol = input_symbol.value
434+
output_symbols = tuple(map(lambda x: x.value, output_symbols))
435+
graph.add_edge(
436+
s_from.value,
437+
s_to.value,
438+
label=(input_symbol, output_symbols))
440439
return graph
441440

442441
@classmethod
@@ -465,10 +464,8 @@ def from_networkx(cls, graph: MultiDiGraph) -> "FST":
465464
for s_to in graph[s_from]:
466465
for transition in graph[s_from][s_to].values():
467466
if "label" in transition:
468-
in_symbol, out_symbols = transition["label"].split(
469-
" -> ")
470-
in_symbol = loads(in_symbol)
471-
out_symbols = loads(out_symbols)
467+
label = transition["label"]
468+
in_symbol, out_symbols = label
472469
fst.add_transition(s_from,
473470
in_symbol,
474471
s_to,

pyformlang/fst/tests/test_fst.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ def test_paper(self):
179179
(2, "alone", 3, ["seul"])])
180180
fst.add_start_state(0)
181181
fst.add_final_state(3)
182-
assert list(fst.translate(["I", "am", "alone"])) == \
183-
[['Je', 'suis', 'seul'],
184-
['Je', 'suis', 'tout', 'seul']]
182+
translation = list(fst.translate(["I", "am", "alone"]))
183+
assert ['Je', 'suis', 'seul'] in translation
184+
assert ['Je', 'suis', 'tout', 'seul'] in translation
185+
assert len(translation) == 2
185186
fst = FST.from_networkx(fst.to_networkx())
186-
assert list(fst.translate(["I", "am", "alone"])) == \
187-
[['Je', 'suis', 'seul'],
188-
['Je', 'suis', 'tout', 'seul']]
187+
translation = list(fst.translate(["I", "am", "alone"]))
188+
assert ['Je', 'suis', 'seul'] in translation
189+
assert ['Je', 'suis', 'tout', 'seul'] in translation
190+
assert len(translation) == 2
189191
fst.write_as_dot("fst.dot")
190192
assert path.exists("fst.dot")

pyformlang/indexed_grammar/duplication_rule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,5 @@ def __eq__(self, other: Any) -> bool:
9696

9797
def __repr__(self) -> str:
9898
"""Gives a string representation of the rule, ignoring the sigmas"""
99-
return f"{self._left_term} -> \
100-
{self._right_terms[0]} {self._right_terms[1]}"
99+
return f"{self._left_term} -> " \
100+
+ f"{self._right_terms[0]} {self._right_terms[1]}"

pyformlang/indexed_grammar/tests/test_rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_production_rules(self):
4141
""" Tests the production rules """
4242
produ = ProductionRule("S", "C", "end")
4343
assert produ.terminals == {"end"}
44-
assert str(produ) == "S -> C[ end ]"
44+
assert str(produ) == "S -> C [ end ]"
4545

4646
def test_rules(self):
4747
""" Tests the rules """

pyformlang/pda/pda.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,12 +789,12 @@ def __copy__(self) -> "PDA":
789789
def __add_start_state_to_graph(graph: MultiDiGraph,
790790
state: State) -> None:
791791
""" Adds a starting node to a given graph """
792-
graph.add_node("starting_" + str(state.value),
792+
graph.add_node("starting_" + str(state),
793793
label="",
794794
shape=None,
795795
height=.0,
796796
width=.0)
797-
graph.add_edge("starting_" + str(state.value),
797+
graph.add_edge("starting_" + str(state),
798798
state.value)
799799

800800
@staticmethod

0 commit comments

Comments
 (0)