def testARPA(self): arpaio = sppasArpaIO() with self.assertRaises(ModelsDataTypeError): arpaio.set("toto") arpaio.set([]) arpaio.set([[], 0]) fn1 = os.path.join(TEMP, "model1.arpa") fn2 = os.path.join(TEMP, "model2.arpa") model = sppasNgramsModel(3) model.count(self.corpusfile) probas = model.probabilities("logml") arpaio.set(probas) arpaio.save(fn1) slm1 = sppasSLM() slm1.load_from_arpa(fn1) slm1.save_as_arpa(fn2) slm2 = sppasSLM() slm2.load_from_arpa(fn2) m1 = slm1.model m2 = slm2.model sp = sppasCompare() self.assertTrue(sp.equals(m1, m2))
def test_fill(self): sp = sppasCompare() acmodel1 = sppasHtkIO() acmodel1.read_macros_hmms([os.path.join(DATA, "1-hmmdefs")]) ahmm1 = acmodel1.get_hmm('a') a1transition = [ macro["transition"] for macro in acmodel1.get_macros() if macro.get('transition', None) ][0] acmodel1.fill_hmms() self.assertTrue( sp.equals(ahmm1.definition['transition'], a1transition['definition']))
def test_replace_phones(self): sp = sppasCompare() acmodel1 = sppasHtkIO() acmodel1.read(os.path.join(MODEL_PATH, "models-fra")) acmodel1.replace_phones(reverse=False) acmodel1.replace_phones(reverse=True) acmodel2 = sppasHtkIO() acmodel2.read(os.path.join(MODEL_PATH, "models-fra")) for h1 in acmodel1.get_hmms(): h2 = acmodel2.get_hmm(h1.name) self.assertTrue( sp.equals(h1.definition['transition'], h2.definition['transition'])) self.assertTrue( sp.equals(h1.definition['states'], h2.definition['states']))
def test_interpolate_hmm(self): acmodel1 = sppasHtkIO() acmodel1.read_macros_hmms([os.path.join(DATA, "1-hmmdefs")]) acmodel2 = sppasHtkIO() acmodel2.read_macros_hmms([os.path.join(DATA, "2-hmmdefs")]) ahmm1 = acmodel1.get_hmm('a') ahmm2 = acmodel2.get_hmm('a') sp = sppasCompare() # transitions # (notice that the transition of 'a' in acmodel1 is in a macro.) a1transition = [ macro["transition"] for macro in acmodel1.get_macros() if macro.get('transition', None) ][0] transitions = [ a1transition['definition'], ahmm2.definition['transition'] ] trs = self.lin.linear_transitions(transitions, [1, 0]) sp.equals(trs, a1transition['definition']) self.assertTrue(sp.equals(trs, a1transition['definition'])) acmodel1.fill_hmms() transitions = [ ahmm1.definition['transition'], ahmm2.definition['transition'] ] trs = self.lin.linear_transitions(transitions, [1, 0]) self.assertTrue(sp.equals(trs, ahmm1.definition['transition'])) trs = self.lin.linear_transitions(transitions, [0, 1]) self.assertTrue(sp.equals(trs, ahmm2.definition['transition'])) # states # (notice that the state 2 of 'a' in acmodel1 is in a macro.) states = [ahmm1.definition['states'], ahmm2.definition['states']] sts = self.lin.linear_states(states, [1, 0]) self.assertTrue(sp.equals(sts, ahmm1.definition['states'])) sts = self.lin.linear_states(states, [0, 1]) self.assertTrue(sp.equals(sts, ahmm2.definition['states']))
def testARPA(self): fn1 = os.path.join(TEMP, "model1.arpa") fn2 = os.path.join(TEMP, "model2.arpa") model = sppasNgramsModel(3) model.count(self.corpusfile) probas = model.probabilities("logml") arpaio = sppasArpaIO() arpaio.set(probas) arpaio.save(fn1) slm1 = sppasSLM() slm1.load_from_arpa(fn1) slm1.save_as_arpa(fn2) slm2 = sppasSLM() slm2.load_from_arpa(fn2) m1 = slm1.model m2 = slm2.model sp = sppasCompare() self.assertTrue(sp.equals(m1, m2))
def _test_load_save(self, folder): """Test to read and write an acoustic model of the given directory.""" # Read the acoustic model (monophone) parser = sppasACMRW(folder) acmodel = parser.read() # Save temporarily the loaded model parser.set_folder(os.path.join(TEMP)) parser.write(acmodel) # Load the temporary file into a new model acmodel_copy = parser.read() sp = sppasCompare() # Compare original and copy self.assertEqual(len(acmodel.get_hmms()), len(acmodel_copy.get_hmms())) for hmm, hmmcopy in zip(acmodel.get_hmms(), acmodel_copy.get_hmms()): self.assertEqual(hmm.get_name(), hmmcopy.get_name()) self.assertTrue( sp.equals(hmm.get_definition(), hmmcopy.get_definition())) self.assertTrue( sp.equals(acmodel.get_macros(), acmodel_copy.get_macros()))