From 716f95e78c6e5092e8204ebab6b16911f876af36 Mon Sep 17 00:00:00 2001 From: Christian Brickhouse Date: Wed, 17 Aug 2022 22:43:25 -0700 Subject: [PATCH] Fix unintended overwrite of add_dict An error in cmudictionary.py caused add_dict to be overwritten and change type when a transcribed word was not in the cmu dictionary and had not yet been added to the output dictionary. This led to an obvious type error and a not obvious failure to add custom dictionaries. This commit fixes the issue by adding the given word as a key to add_dict and adds a test to prevent regressions. Resolves JoFrhwld/FAVE#59 --- fave/cmudictionary.py | 2 +- tests/fave/test_cmudictionary.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/fave/cmudictionary.py b/fave/cmudictionary.py index 1eb68f2..07f2da5 100644 --- a/fave/cmudictionary.py +++ b/fave/cmudictionary.py @@ -214,7 +214,7 @@ def add_dictionary_entries(self, infile, path='.'): if t not in cmu_dict[word]: cmu_dict[word].append(t) if word not in add_dict: - add_dict = [] + add_dict[word] = [] if t not in add_dict[word]: add_dict[word].append(t) diff --git a/tests/fave/test_cmudictionary.py b/tests/fave/test_cmudictionary.py index f80ee90..54ad4c1 100644 --- a/tests/fave/test_cmudictionary.py +++ b/tests/fave/test_cmudictionary.py @@ -29,6 +29,16 @@ def test_dictionary_init(tmp_path): assert p.read_text() == CMU_EXCERPT def test_add_dictionary_entries(tmp_path): + """ + TODO list: + * generalize the old_word new_word tests so that they + can be handled by a provider, for an example see + tests/fave/align/test_transcriptprocessor.py + + * reduce code redundancy in the dict_obj setup probably + using pytest fixtures in a conftest.py file, see + https://docs.pytest.org/en/6.2.x/fixture.html#conftest-py-sharing-fixtures-across-multiple-files + """ d = tmp_path / "sub" d.mkdir() @@ -47,3 +57,11 @@ def test_add_dictionary_entries(tmp_path): added_entries_file = d / dict_obj.DICT_ADDITIONS assert new_word.replace("\t", " ") in added_entries_file.read_text() + + old_word = "TEST\tT EH1 S T \n" + old_word_file = d / "old_word_file.dict" + old_word_file.write_text(old_word) + + dict_obj.add_dictionary_entries(old_word_file, path=d) + + assert old_word.replace("\t", " ") in added_entries_file.read_text()