Beispiel #1
0
    def validate(self):
        """Ensures that adjacent start/stop times are valid by adding silence"""
        i = 0
        start = 0.
        while i < len(self) - 1:

            # Get start and end times between words
            end = self[i].start()

            # Patch gap with silence
            if end - start > 1e-3:

                # Extend existing silence if possible
                if str(self[i]) == pypar.SILENCE:
                    self[i][0]._start = start
                else:
                    word = pypar.Word(
                        pypar.SILENCE,
                        [pypar.Phoneme(pypar.SILENCE, start, end)])
                    self._words.insert(i, word)
                    i += 1

            i += 1
            start = self[i].end()

        # Phoneme gap validation
        for word in self:
            word.validate()
Beispiel #2
0
    def load_textgrid(self, filename):
        """Load from textgrid file"""
        # Load file
        grid = textgrid.TextGrid.fromFile(filename)

        # Get phoneme and word representations
        phon_tier, word_tier = grid[0], grid[1]

        # Iterate over words
        words = []
        phon_idx = 0
        for word in word_tier:

            # Get all phonemes for this word
            phonemes = []
            while phon_idx < len(phon_tier) and \
                  phon_tier[phon_idx].maxTime <= word.maxTime:
                phoneme = phon_tier[phon_idx]
                mark = phoneme.mark if phoneme.mark != 'sil' else pypar.SILENCE
                phonemes.append(
                    pypar.Phoneme(mark, phoneme.minTime, phoneme.maxTime))
                phon_idx += 1

            # Add finished word
            words.append(pypar.Word(word.mark, phonemes))

        return words
Beispiel #3
0
    def parse_json(self, alignment):
        """Construct word list from json representation"""
        words = []
        for word in alignment['words']:
            try:

                # Add a word
                phonemes = [
                    pypar.Phoneme(*phoneme) for phoneme in word['phonemes']
                ]
                words.append(pypar.Word(word['alignedWord'], phonemes))

            except KeyError:

                # Add a silence
                phonemes = [
                    pypar.Phoneme(pypar.SILENCE, word['start'], word['end'])
                ]
                words.append(pypar.Word(pypar.SILENCE, phonemes))

        return words
Beispiel #4
0
    def validate(self):
        """Ensures that adjacent start/end times are valid by adding silence"""
        i = 0
        while i < len(self) - 1:

            # Get start and end times between phonemes
            start = self[i].end()
            end = self[i + 1].start()

            # Patch gap with silence
            if end - start > 1e-4:
                phoneme = pypar.Phoneme(pypar.SILENCE, start, end)
                self.phonemes.insert(i + 1, phoneme)
                i += 1

            i += 1
Beispiel #5
0
    def load_mlf(self, filename):
        """Load from mlf file"""
        # Load file from disk
        with open(filename) as file:
            # Read in phoneme alignment
            lines = [
                Line(line) for line in file.readlines()
                if self.line_is_valid(line)
            ]

            # Remove silence tokens with 0 duration
            lines = [line for line in lines if line.start < line.end]

        # Extract words and phonemes
        phonemes = []
        words = []
        for line in lines:

            # Start new word
            if line.word is not None:

                # Add word that just finished
                if phonemes:
                    words.append(pypar.Word(word, phonemes))
                    phonemes = []

                word = line.word

            # Add a phoneme
            phonemes.append(pypar.Phoneme(line.phoneme, line.start, line.end))

        # Handle last word
        if phonemes:
            words.append(pypar.Word(word, phonemes))

        return words