Package: pypar
Python phoneme alignment representationpip install pyparWord and phoneme alignment representation for speech tasks. This repo does
not perform forced word or phoneme alignment, but provides an interface
for working with the resulting alignment of a forced aligner, such aspyfoal, or a manual alignment.Table of contentsUsageCreating alignmentsAccessing words and phonemesSaving alignmentsApplication programming interface (API)pypar.Alignmentpypar.Alignment.__init__pypar.Alignment.__add__pypar.Alignment.__eq__pypar.Alignment.__getitem__pypar.Alignment.__len__pypar.Alignment.__str__pypar.Alignment.durationpypar.Alignment.endpypar.Alignment.findpypar.Alignment.framewise_phoneme_indicespypar.Alignment.phonemespypar.Alignment.phoneme_at_timepypar.Alignment.phoneme_boundspypar.Alignment.savepypar.Alignment.startpypar.Alignment.updatepypar.Alignment.wordspypar.Alignment.word_boundspypar.Phonemepypar.Phoneme.__init__pypar.Phoneme.__eq__pypar.Phoneme.__str__pypar.Phoneme.durationpypar.Phoneme.endpypar.Phoneme.startpypar.Wordpypar.Word.__init__pypar.Word.__eq__pypar.Word.__getitem__pypar.Word.__len__pypar.Word.__str__pypar.Word.durationpypar.Word.endpypar.Word.phoneme_at_timepypar.Word.startTestsUsageCreating alignmentsIf you already have the alignment saved to ajson,mlf, orTextGridfile, pass the name of the file. Valid examples of each format can be found intest/assets/.alignment=pypar.Alignment(file)Alignments can be created manually fromWordandPhonemeobjects. Start and
end times are given in seconds.# Create a word from phonemesword=pypar.Word('THE',[pypar.Phoneme('DH',0.,.03),pypar.Phoneme('AH0',.03,.06)])# Create a silencesilence=pypar.Word(pypar.SILENCE,pypar.Phoneme(pypar.SILENCE,.06,.16))# Make an alignmentalignment=pypar.Alignment([word,silence])You can create a new alignment from existing alignments via slicing and
concatenation.# Slicefirst_two_words=alignment[:2]# Concatenatealignment_with_repeat=first_two_words+alignmentAccessing words and phonemesTo retrieve a list of words in the alignment, usealignment.words().
To retrieve a list of phonemes, usealignment.phonemes(). TheAlignment,Word, andPhonemeobjects all define.start(),.end(), and.duration()methods, which return the start time, end time, and duration,
respectively. All times are given in units of seconds. These objects also
define equality checks via==, casting to string withstr(), and iteration
as follows.# Iterate over wordsforwordinalignment:# Access start and end timesassertword.duration()==word.end()-word.start()# Iterate over phonemes in wordforphonemeinword:# Access string representationassertisinstance(str(phoneme),str)To access a word or phoneme at a specific time, pass the time in seconds toalignment.word_at_timeoralignment.phoneme_at_time.To retrieve the frame indices of the start and end of a word or phoneme, pass
the audio sampling rate and hopsize (in samples) toalignment.word_boundsoralignment.phoneme_bounds.Saving alignmentsTo save an alignment to disk, usealignment.save(file), wherefileis the
desired filename.pyparcurrently supports saving as ajsonorTextGridfile.Application programming interface (API)pypar.Alignmentpypar.Alignment.__init__def__init__(self,alignment:Union[str,bytes,os.PathLike,List[pypar.Word],dict])->None:"""Create alignmentArgumentsalignmentThe filename, list of words, or json dict of the alignment"""pypar.Alignment.__add__def__add__(self,other):"""Add alignments by concatenationArgumentsotherThe alignment to compare toReturnsThe concatenated alignment"""pypar.Alignment.__eq__def__eq__(self,other)->bool:"""Equality comparison for alignmentsArgumentsotherThe alignment to compare toReturnsWhether the alignments are equal"""pypar.Alignment.__getitem__def__getitem__(self,idx:Union[int,slice])->pypar.Word:"""Retrieve the idxth wordArgumentsidxThe index of the word to retrieveReturnsThe word at index idx"""pypar.Alignment.__len__def__len__(self)->int:"""Retrieve the number of wordsReturnsThe number of words in the alignment"""pypar.Alignment.__str__def__str__(self)->str:"""Retrieve the textReturnsThe words in the alignment separated by spaces"""pypar.Alignment.durationdefduration(self)->float:"""Retrieve the duration of the alignment in secondsReturnsThe duration in seconds"""pypar.Alignment.enddefend(self)->float:"""Retrieve the end time of the alignment in secondsReturnsThe end time in seconds"""pypar.Alignment.framewise_phoneme_indicesdefframewise_phoneme_indices(self,phoneme_map:Dict[str,int],hopsize:float,times:Optional[List[float]]=None)->List[int]:"""Convert alignment to phoneme indices at regular temporal intervalArgumentsphoneme_mapMapping from phonemes to indiceshopsizeTemporal interval between frames in secondstimesSpecified times in seconds to sample phonemes"""pypar.Alignment.finddeffind(self,words:str)->int:"""Find the words in the alignmentArgumentswordsThe words to findReturnsThe index of the start of the words or -1 if not found"""pypar.Alignment.phonemesdefphonemes(self)->List[pypar.Phoneme]:"""Retrieve the phonemes in the alignmentReturnsThe phonemes in the alignment"""pypar.Alignment.phoneme_at_timedefphoneme_at_time(self,time:float)->Optional[pypar.Phoneme]:"""Retrieve the phoneme spoken at specified timeArgumentstimeTime in secondsReturnsThe phoneme at the given time (or None if time is out of bounds)"""pypar.Alignment.phoneme_boundsdefphoneme_bounds(self,sample_rate:int,hopsize:int=1)->List[Tuple[int,int]]:"""Retrieve the start and end frame index of each phonemeArgumentssample_rateThe audio sampling ratehopsizeThe number of samples between successive framesReturnsThe start and end indices of the phonemes"""pypar.Alignment.savedefsave(self,filename:Union[str,bytes,os.PathLike])->None:"""Save alignment to jsonArgumentsfilenameThe location on disk to save the phoneme alignment json"""pypar.Alignment.startdefstart(self)->float:"""Retrieve the start time of the alignment in secondsReturnsThe start time in seconds"""pypar.Alignment.updatedefupdate(self,idx:int=0,durations:Optional[List[float]]=None,start:Optional[float]=None)->None:"""Update alignment starting from phoneme index idxArgumentsidxThe index of the first phoneme whose duration is being updateddurationsThe new phoneme durations, starting from idxstartThe start time of the alignment"""pypar.Alignment.wordsdefwords(self)->List[pypar.Word]:"""Retrieve the words in the alignmentReturnsThe words in the alignment"""pypar.Alignment.word_boundsdefword_at_time(self,time:float)->Optional[pypar.Word]:"""Retrieve the word spoken at specified timeArgumentstimeTime in secondsReturnsThe word spoken at the specified time"""pypar.Phonemepypar.Phoneme.__init__def__init__(self,phoneme:str,start:float,end:float)->None:"""Create phonemeArgumentsphonemeThe phonemestartThe start time in secondsendThe end time in seconds"""pypar.Phoneme.__eq__def__eq__(self,other)->bool:"""Equality comparison for phonemesArgumentsotherThe phoneme to compare toReturnsWhether the phonemes are equal"""pypar.Phoneme.__str__def__str__(self)->str:"""Retrieve the phoneme textReturnsThe phoneme"""pypar.Phoneme.durationdefduration(self)->float:"""Retrieve the phoneme durationReturnsThe duration in seconds"""pypar.Phoneme.enddefend(self)->float:"""Retrieve the end time of the phoneme in secondsReturnsThe end time in seconds"""pypar.Phoneme.startdefstart(self)->float:"""Retrieve the start time of the phoneme in secondsReturnsThe start time in seconds"""pypar.Wordpypar.Word.__init__def__init__(self,word:str,phonemes:List[pypar.Phoneme])->None:"""Create wordArgumentswordThe wordphonemesThe phonemes in the word"""pypar.Word.__eq__def__eq__(self,other)->bool:"""Equality comparison for wordsArgumentsotherThe word to compare toReturnsWhether the words are the same"""pypar.Word.__getitem__def__getitem__(self,idx:int)->pypar.Phoneme:"""Retrieve the idxth phonemeArgumentsidxThe index of the phoneme to retrieveReturnsThe phoneme at index idx"""pypar.Word.__len__def__len__(self)->int:"""Retrieve the number of phonemesReturnsThe number of phonemes"""pypar.Word.__str__def__str__(self)->str:"""Retrieve the word textReturnsThe word text"""pypar.Word.durationdefduration(self)->float:"""Retrieve the word duration in secondsReturnsThe duration in seconds"""pypar.Word.enddefend(self)->float:"""Retrieve the end time of the word in secondsReturnsThe end time in seconds"""pypar.Word.phoneme_at_timedefphoneme_at_time(self,time:float)->Optional[pypar.Phoneme]:"""Retrieve the phoneme at the specified timeArgumentstimeTime in secondsReturnsThe phoneme at the given time (or None if time is out of bounds)"""pypar.Word.startdefstart(self)->float:"""Retrieve the start time of the word in secondsReturnsThe start time in seconds"""TestsTests can be run as follows.pip install pytest
pytest