Skip to content

Commit

Permalink
fix: g2p convert should not add newline when input is a file
Browse files Browse the repository at this point in the history
Fixes: #351
  • Loading branch information
joanise committed Apr 3, 2024
1 parent 232ec0f commit f50768e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 5 additions & 9 deletions g2p/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,8 @@ def convert( # noqa: C901
# This isn't that DRY - copied from g2p/mappings/langs/__init__.py
mappings_legal_pairs = []
mapping_config = MappingConfig.load_mapping_config_from_path(config)
for index in range(len(mapping_config.mappings)):
mappings_legal_pairs.append(
(
mapping_config.mappings[index].in_lang,
mapping_config.mappings[index].out_lang,
)
)
for index, mapping in enumerate(mapping_config.mappings):
mappings_legal_pairs.append((mapping.in_lang, mapping.out_lang))
mapping_config.mappings[index] = Mapping.load_mapping_from_path(
config, index
)
Expand All @@ -536,7 +531,8 @@ def convert( # noqa: C901
raise click.UsageError(
f"Path between '{in_lang}' and '{out_lang}' does not exist"
)
if os.path.exists(input_text) and input_text.endswith("txt"):
input_text_is_a_file = os.path.exists(input_text) and input_text.endswith("txt")
if input_text_is_a_file:
with open(input_text, encoding="utf8") as f:
input_text = f.read()
# Determine which tokenizer to use, if any
Expand All @@ -563,7 +559,7 @@ def convert( # noqa: C901
if len(outputs) > 1:
click.echo(pprint.pformat(outputs, indent=4))
else:
click.echo(tg.output_string)
click.echo(tg.output_string, nl=not input_text_is_a_file)


# Note: with -m eng-ipa, we actually check all the mappings from lang-ipa to eng-ipa.
Expand Down
8 changes: 5 additions & 3 deletions g2p/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,13 @@ def test_show_mappings(self):
self.assertNotEqual(results.exit_code, 0)

def test_convert_from_file(self):
results = self.runner.invoke(
convert, [os.path.join(DATA_DIR, "fra_simple.txt"), "fra", "fra-ipa"]
)
input_file = os.path.join(DATA_DIR, "fra_simple.txt")
results = self.runner.invoke(convert, [input_file, "fra", "fra-ipa"])
self.assertEqual(results.exit_code, 0)
self.assertIn("fʁɑ̃sɛ", results.output)
with open(input_file, "r", encoding="utf8") as f:
lines_in = len(list(f))
self.assertEqual(lines_in, len(results.output.splitlines()))

def test_convert_errors(self):
"""Exercise code handling error situations in g2p convert"""
Expand Down

0 comments on commit f50768e

Please sign in to comment.