Skip to content

Commit

Permalink
Fix unintended overwrite of add_dict
Browse files Browse the repository at this point in the history
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#59
  • Loading branch information
Christian Brickhouse committed Aug 18, 2022
1 parent 40503a6 commit 716f95e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fave/cmudictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions tests/fave/test_cmudictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

0 comments on commit 716f95e

Please sign in to comment.