Exemplo n.º 1
0
    def __init__(self, mapping, model):
        """
        Creates a new AlignIO instance.

        @param mapping (Mapping) a mapping table to convert the phone set

        """
        # Mapping system for the phonemes
        if mapping is None:
            mapping = Mapping()
        if isinstance(mapping, Mapping) is False:
            raise TypeError('Aligner expected a Mapping as argument.')
        self._mapping = mapping

        # The automatic alignment system
        self.aligntrack = AlignTrack( model )

        # The list of units file generator
        self._listio = ListIO()

        # The file names of tracks generator
        self._tracknames = TrackNamesGenerator()
Exemplo n.º 2
0
class AlignIO:
    """
    @author:       Brigitte Bigi
    @organization: Laboratoire Parole et Langage, Aix-en-Provence, France
    @contact:      [email protected]
    @license:      GPL, v3
    @copyright:    Copyright (C) 2011-2016  Brigitte Bigi
    @summary:      Read/Write segments from/to Tiers.

    ??? HOW TO DO: READ ALL ALTERNATIVE LABELS AND MERGE ALTERNATIVE RESULTS ???

    """
    def __init__(self, mapping, model):
        """
        Creates a new AlignIO instance.

        @param mapping (Mapping) a mapping table to convert the phone set

        """
        # Mapping system for the phonemes
        if mapping is None:
            mapping = Mapping()
        if isinstance(mapping, Mapping) is False:
            raise TypeError('Aligner expected a Mapping as argument.')
        self._mapping = mapping

        # The automatic alignment system
        self.aligntrack = AlignTrack( model )

        # The list of units file generator
        self._listio = ListIO()

        # The file names of tracks generator
        self._tracknames = TrackNamesGenerator()

    # ------------------------------------------------------------------------

    def set_aligner(self, alignername):
        """
        Fix the name of the aligner.
        The list of accepted aligner names is available in:
        >>> aligners.aligner_names()

        @param alignername (str - IN) Case-insensitive name of the aligner.

        """
        self.aligntrack.set_aligner(alignername)

    # -----------------------------------------------------------------------

    def get_aligner(self):
        """
        Return the aligner name identifier.

        """
        return self.aligntrack.get_aligner()

    # ----------------------------------------------------------------------

    def set_infersp(self, infersp):
        """
        Fix the infersp option.

        @param infersp (bool - IN) If infersp is set to True, the aligner
        will add an optional short pause at the end of each token, and the
        will infer if it is relevant.

        """
        self.aligntrack.set_infersp( infersp )

    # ------------------------------------------------------------------------

    def segment_track(self, track, diralign, segment=True):
        """
        Perform the speech segmentation of a track in a directory.

        @param track (str - int)
        @param diralign (str - IN)
        @param segment (bool - IN) If True, call an aligner to segment speech,
        else create a file with an empty alignment.

        @return A message of the aligner in case of any problem, or
        an empty string if success.

        """
        audiofilename = self._tracknames.audiofilename(diralign, track)
        phonname      = self._tracknames.phonesfilename(diralign,track)
        tokenname     = self._tracknames.tokensfilename(diralign,track)
        alignname     = self._tracknames.alignfilename(diralign,track)

        if segment is True:
            msg = self.aligntrack.segmenter(audiofilename, phonname, tokenname, alignname)
        else:
            msg = self.aligntrack.segmenter(audiofilename, None, None, alignname)
        return msg

    # ------------------------------------------------------------------------

    def read(self, dirname):
        """
        Read time-aligned tracks in a directory and return a Transcription.

        @param diralign (str - IN) Input directory to get files.
        @return Transcription

        """
        units = self._listio.read( dirname )

        trsin = TracksReader()
        trsin.set_tracksnames( self._tracknames )
        trsin.read(dirname, units)

        # map-back phonemes
        self._mapping.set_keepmiss( True )
        self._mapping.set_reverse( False )

        # Map time-aligned phonemes (even the alternatives)
        tier = trsin.Find("PhonAlign")
        for ann in tier:
            for text in ann.GetLabel().GetLabels():
                text.SetValue( self._mapping.map_entry(text.GetValue()) )

        return trsin

    # ------------------------------------------------------------------------

    def split(self, inputaudio, phontier, toktier, diralign):
        """
        Write tracks from a Transcription.
        If the given phontier is not already time-aligned in intervals,
        an automatic track-segmenter will be applied first and the TimeAligned
        version of the tokenization is returned.

        @param audioname (str - IN) Audio file name.
        @param phontier (Tier - IN) The phonetization.
        @param toktier  (Tier - IN) The tokenization, or None.
        @param diralign  (str - IN) Output directory to store files.

        @return Transcription

        """
        # Map phonemes from SAMPA to the expected ones.
        self._mapping.set_keepmiss( True )
        self._mapping.set_reverse( True )

        # Map phonetizations (even the alternatives)
        for ann in phontier:
            for text in ann.GetLabel().GetLabels():
                text.SetValue( self._mapping.map( text.GetValue() ) )

        sgmt = TrackSplitter()
        sgmt.set_tracksnames( self._tracknames )
        sgmt.set_trackalign( self.aligntrack )
        units = sgmt.split(inputaudio, phontier, toktier, diralign)
        self._listio.write(diralign, units)

        return sgmt