From f50768ed039d92540f3aa8faae5a07357f28dd45 Mon Sep 17 00:00:00 2001 From: Eric Joanis Date: Tue, 2 Apr 2024 10:52:47 -0400 Subject: [PATCH] fix: g2p convert should not add newline when input is a file Fixes: #351 --- g2p/cli.py | 14 +++++--------- g2p/tests/test_cli.py | 8 +++++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/g2p/cli.py b/g2p/cli.py index 78cc40cb..e3cb37d9 100644 --- a/g2p/cli.py +++ b/g2p/cli.py @@ -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 ) @@ -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 @@ -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. diff --git a/g2p/tests/test_cli.py b/g2p/tests/test_cli.py index 221d9d02..bafa44a7 100755 --- a/g2p/tests/test_cli.py +++ b/g2p/tests/test_cli.py @@ -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"""