def test_interpolate_hmm(self): acmodel1 = AcModel() acmodel1.load_htk( os.path.join(HERE, "1-hmmdefs") ) acmodel2 = AcModel() acmodel2.load_htk( os.path.join(HERE,"2-hmmdefs") ) ahmm1=acmodel1.get_hmm('a') ahmm2=acmodel2.get_hmm('a') # transitions # (notice that the transition of 'a' in acmodel1 is in a macro.) a1transition = [macro["transition"] for macro in acmodel1.macros if macro.get('transition',None)][0] transitions = [ a1transition['definition'],ahmm2.definition['transition'] ] trs = self.lin.linear_transitions( transitions, [1,0]) compare(trs,a1transition['definition']) self.assertTrue(compare(trs,a1transition['definition'])) acmodel1.fill_hmms() transitions = [ ahmm1.definition['transition'],ahmm2.definition['transition'] ] trs = self.lin.linear_transitions( transitions, [1,0]) self.assertTrue(compare(trs,ahmm1.definition['transition'])) trs = self.lin.linear_transitions( transitions, [0,1]) self.assertTrue(compare(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]) compare(sts,ahmm1.definition['states'],verbose=True) self.assertTrue(compare(sts,ahmm1.definition['states'])) sts = self.lin.linear_states( states, [0,1]) self.assertTrue(compare(sts,ahmm2.definition['states']))
def test_replace_phones(self): acmodel1 = AcModel() acmodel1.load( os.path.join(MODEL_PATH,"models-fra") ) acmodel1.replace_phones( reverse=False ) acmodel1.replace_phones( reverse=True ) acmodel2 = AcModel() acmodel2.load( os.path.join(MODEL_PATH,"models-fra") ) for h1 in acmodel1.hmms: h2 = acmodel2.get_hmm( h1.name ) self.assertTrue(compare(h1.definition['transition'],h2.definition['transition'])) self.assertTrue(compare(h1.definition['states'],h2.definition['states']))
def test_fill(self): acmodel1 = AcModel() acmodel1.load_htk( os.path.join(HERE,"1-hmmdefs") ) ahmm1=acmodel1.get_hmm('a') a1transition = [macro["transition"] for macro in acmodel1.macros if macro.get('transition',None)][0] acmodel1.fill_hmms() self.assertTrue(compare(ahmm1.definition['transition'],a1transition['definition']))
def test_save_hmm(self): hmm = HMM() hmm.load( os.path.join(HERE,"N-hmm") ) hmm.save(os.path.join(HERE,"N-hmm-copy")) newhmm = HMM() newhmm.load(os.path.join(HERE,"N-hmm-copy")) os.remove(os.path.join(HERE,'N-hmm-copy')) self.assertEqual(hmm.name,newhmm.name) self.assertTrue(compare(hmm.definition,newhmm.definition))
def _test_load_save(self, acmodel): # Save temporary the loaded model into a file tmpfile = self.hmmdefs+".copy" self.acmodel.save_htk( tmpfile ) # Load the temporary file into a new model acmodelcopy = AcModel() acmodelcopy.load_htk(tmpfile) # Compare original and copy self.assertEqual(len(self.acmodel.hmms),len(acmodelcopy.hmms)) for hmm,hmmcopy in zip(self.acmodel.hmms,acmodelcopy.hmms): self.assertEqual(hmm.name,hmmcopy.name) self.assertTrue(compare(hmm.definition,hmmcopy.definition)) self.assertTrue(compare(self.acmodel.macros,acmodelcopy.macros)) os.remove(tmpfile)
def test_proto(self): h1 = HtkIO() h1.write_hmm_proto( 25, os.path.join(HERE,"proto_from_htkio") ) h2 = HMM() h2.create_proto( 25 ) h2.save( os.path.join(HERE,"proto_from_hmm") ) m1 = HMM() m1.load( os.path.join(HERE,"proto_from_htkio") ) m2 = HMM() m2.load( os.path.join(HERE,"proto_from_hmm") ) self.assertTrue(compare(m1.definition['transition'],m2.definition['transition'])) self.assertTrue(compare(m1.definition['states'],m2.definition['states'])) os.remove( os.path.join(HERE,"proto_from_hmm") ) os.remove( os.path.join(HERE,"proto_from_htkio") )
def test_initializer_without_corpus(self): corpus = TrainingCorpus() workdir = os.path.join(HERE,"working") os.mkdir( workdir ) shutil.copy( os.path.join(HERE, "protos","vFloors"), workdir ) initial = HTKModelInitializer( corpus, workdir ) corpus.datatrainer.protodir = os.path.join(HERE, "protos") initial.create_model() hmm1 = HMM() hmm2 = HMM() hmm1.load( os.path.join( workdir, "@@.hmm") ) hmm2.load( os.path.join(HERE, "protos", "@@.hmm") ) self.assertTrue(compare(hmm1.definition,hmm2.definition)) hmm1.load( os.path.join( workdir, "sil.hmm") ) hmm2.load( os.path.join(HERE, "protos", "sil.hmm") ) corpus.datatrainer.fix_proto(protofilename=os.path.join(HERE, "protos", "proto.hmm")) hmm2.load( os.path.join(HERE, "protos", "proto.hmm") ) hmm1.load( os.path.join(workdir, "gb.hmm") ) self.assertTrue(compare(hmm1.definition,hmm2.definition)) hmm1.load( os.path.join(workdir, "dummy.hmm") ) self.assertTrue(compare(hmm1.definition,hmm2.definition)) acmodel = AcModel() acmodel.load_htk( os.path.join( workdir,"hmmdefs") ) shutil.rmtree( workdir ) os.remove( os.path.join(HERE, "protos", "proto.hmm") )
def testARPA(self): fn1 = os.path.join(TEMP,"model1.arpa") fn2 = os.path.join(TEMP,"model2.arpa") model = NgramsModel(3) model.count( self.corpusfile ) probas = model.probabilities("logml") arpaio = ArpaIO() arpaio.set( probas ) arpaio.save( fn1 ) slm1 = SLM() slm1.load_from_arpa( fn1 ) slm1.save_as_arpa( fn2 ) slm2 = SLM() slm2.load_from_arpa( fn2 ) m1 = slm1.model m2 = slm2.model self.assertTrue( compare(m1,m2) )