Skip to content

Commit

Permalink
test: fix missing coverage of noisedict code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Jun 1, 2022
1 parent 973611b commit 566753b
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/test_force_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
"""

import os
import shutil
import unittest
from tempfile import TemporaryDirectory

from basic_test_case import BasicTestCase
from lxml import etree
from soundswallower import get_model_path

from readalongs.align import align_audio, convert_to_xhtml, create_input_tei
from readalongs.log import LOGGER
Expand Down Expand Up @@ -52,6 +55,35 @@ def test_align_text(self):
for w, xw in zip(words, xml_words):
self.assertEqual(xw.attrib["id"], w["id"])

def test_align_switch_am(self):
"""Alignment test case with an alternate acoustic model and custom
noise dictionary."""
xml_path = os.path.join(self.data_dir, "ej-fra.xml")
wav_path = os.path.join(self.data_dir, "ej-fra.m4a")
# Try with some extra stuff in the noisedict
with TemporaryDirectory(prefix="readalongs_am_") as tempdir:
custom_am_path = os.path.join(tempdir, "en-us")
shutil.copytree(get_model_path("en-us"), custom_am_path)
with open(os.path.join(custom_am_path, "noisedict"), "at") as fh:
fh.write(";; here is a comment\n")
fh.write("[BOGUS] SIL\n")
results = align_audio(
xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path}
)
# Try with no noisedict
os.remove(os.path.join(custom_am_path, "noisedict"))
results = align_audio(
xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path}
)
# Verify that the same IDs are in the output
converted_path = os.path.join(self.data_dir, "ej-fra-converted.xml")
xml = etree.parse(converted_path).getroot()
words = results["words"]
xml_words = xml.xpath(".//w")
self.assertEqual(len(words), len(xml_words))
for w, xw in zip(words, xml_words):
self.assertEqual(xw.attrib["id"], w["id"])


class TestXHTML(BasicTestCase):
"""Test converting the output to xhtml"""
Expand All @@ -66,7 +98,8 @@ def test_convert(self):
txt = load_txt(tf.name)
self.maxDiff = None
self.assertEqual(
txt, load_txt(os.path.join(self.data_dir, "ej-fra-converted.xhtml")),
txt,
load_txt(os.path.join(self.data_dir, "ej-fra-converted.xhtml")),
)


Expand Down

0 comments on commit 566753b

Please sign in to comment.