Example #1
0
    def replace_phones(self, reverse=False ):
        """
        Replace the phones by using a mapping table.

        This is mainly useful due to restrictions in some acoustic model toolkits:
        X-SAMPA can't be fully used and a "mapping" is required.
        As for example, the /2/ or /9/ can't be represented directly in an
        HTK-ASCII acoustic model. We commonly replace respectively by /eu/ and
        /oe/.

        Notice that '+' and '-' can't be used as a phone name.

        @param reverse (bool) reverse the replacement direction.

        """
        if self.repllist.get_size() == 0:
            return
        delimiters = ["-","+"]

        oldreverse = self.repllist.reverse
        self.repllist.set_reverse(reverse)

        # Replace in the tiedlist
        newtied = TiedList()

        for observed in self.tiedlist.observed:
            mapped = self.repllist.map( observed,delimiters )
            newtied.add_observed( mapped )
        for tied,observed in self.tiedlist.tied.items():
            mappedtied     = self.repllist.map( tied, delimiters)
            mappedobserved = self.repllist.map( observed, delimiters)
            newtied.add_tied(mappedtied, mappedobserved)
        self.tiedlist = newtied

        # Replace in HMMs
        for hmm in self.hmms:
            hmm.set_name( self.repllist.map( hmm.name, delimiters) )

            states = hmm.definition['states']
            if all(isinstance(state['state'], (collections.OrderedDict,collections.defaultdict)) for state in states) is False:
                for state in states:
                    if isinstance(state['state'], (collections.OrderedDict,collections.defaultdict)) is False:
                        tab = state['state'].split('_')
                        tab[1] = self.repllist.map_entry( tab[1] )
                        state['state'] = "_".join(tab)

            transition = hmm.definition['transition']
            if isinstance(transition, (collections.OrderedDict,collections.defaultdict)) is False:
                tab = transition.split('_')
                tab[1] = self.repllist.map_entry( tab[1] )
                transition = "_".join(tab)

        self.repllist.set_reverse(oldreverse)