Skip to content

Commit

Permalink
test: MRE for #283 shows swallowed edge after 1->2+2->1
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Sep 1, 2023
1 parent b413089 commit 91d46f6
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions g2p/tests/test_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,26 @@ def test_case_sensitive(self):
def test_escape_special(self):
mapping = Mapping([{"in": r"\d", "out": "digit"}])
mapping_escaped = Mapping([{"in": r"\d", "out": "b"}], escape_special=True)
mapping_input_and_output_special_escaped = Mapping([{"in": "&", "out": "&"}], escape_special=True)
mapping_specific_from_fpcc = Mapping([{"in": r"^", "out": "A"}, {"in": "o", "out": r"."}], rule_ordering="apply-longest-first", escape_special=True)
mapping_input_and_output_special_escaped = Mapping(
[{"in": "&", "out": "&"}], escape_special=True
)
mapping_specific_from_fpcc = Mapping(
[{"in": r"^", "out": "A"}, {"in": "o", "out": r"."}],
rule_ordering="apply-longest-first",
escape_special=True,
)
transducer = Transducer(mapping)
transducer_escaped = Transducer(mapping_escaped)
transducer_escaped_input_output = Transducer(mapping_input_and_output_special_escaped)
transducer_escaped_input_output = Transducer(
mapping_input_and_output_special_escaped
)
transducer_fpcc = Transducer(mapping_specific_from_fpcc)
self.assertEqual(transducer("1").output_string, "digit")
self.assertEqual(transducer(r"\d").output_string, r"\d")
self.assertEqual(transducer_escaped("1").output_string, "1")
self.assertEqual(transducer_escaped(r"\d").output_string, "b")
self.assertEqual(transducer_escaped_input_output('&').output_string, "&")
self.assertEqual(transducer_escaped_input_output("&").output_string, "&")
self.assertEqual(transducer_fpcc("^o").output_string, "A.")


def test_norm_form(self):
mapping_nfc = Mapping([{"in": "a\u0301", "out": "a"}]) # Defaults to NFC
Expand Down Expand Up @@ -343,6 +350,42 @@ def test_g2p_studio_csv(self):
)
os.unlink(tf.name)

def test_vanishing_edge(self):
"""1->2+2->1 should not eat the next edge"""
mapping = Mapping([{"in": "d{1}ef{2}", "out": "gh{1}i{2}"}])
transducer = Transducer(mapping)
# the rule itself works, with the indices doing what they're supposed to do
tg = transducer("def")
self.assertEqual(tg.output_string, "ghi")
self.assertEqual(tg.alignments(), [(0, 0), (0, 1), (1, 2), (2, 2)])
# but it swallows the next edge
tg = transducer("deft")
self.assertEqual(tg.output_string, "ghit")
# with vanishing edge bug, the following fails, getting only [(0, 0), (0, 1), (1, 2), (2, 2)]
self.assertEqual(tg.alignments(), [(0, 0), (0, 1), (1, 2), (2, 2), (3, 3)])

# make sure the fix works with "complex" cases too
tg = transducer("deftdefdef")
self.assertEqual(tg.output_string, "ghitghighi")
self.assertEqual(
tg.alignments(),
[
(0, 0),
(0, 1),
(1, 2),
(2, 2),
(3, 3),
(4, 4),
(4, 5),
(5, 6),
(6, 6),
(7, 7),
(7, 8),
(8, 9),
(9, 9),
],
)


if __name__ == "__main__":
main()

0 comments on commit 91d46f6

Please sign in to comment.