<s> import os <EOL> import glob <EOL> import cPickle as pickle <EOL> from collections import defaultdict , OrderedDict <EOL> import string <EOL> import logging <EOL> logger = logging . getLogger ( __file__ ) <EOL> import kaldi_argparse <EOL> def get_phones60 ( tloc , filter = lambda x : True ) : <EOL> trans = [ ] <EOL> phones = set ( ) <EOL> for tset in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> trans . extend ( glob . glob ( tloc + '<STR_LIT:/>' + tset + '<STR_LIT>' ) ) <EOL> transcriptions = { } <EOL> for transcription_file in trans : <EOL> file_parts = transcription_file . split ( '<STR_LIT:/>' ) <EOL> speaker = file_parts [ - <NUM_LIT:2> ] <EOL> utt = file_parts [ - <NUM_LIT:1> ] [ : - <NUM_LIT:4> ] <EOL> utt_id = speaker + '<STR_LIT:_>' + utt <EOL> transcript = [ ] <EOL> with open ( transcription_file , '<STR_LIT:r>' ) as f : <EOL> for line in f : <EOL> line = line . strip ( ) . split ( ) <EOL> if len ( line ) < <NUM_LIT:3> : <EOL> break <EOL> phone = line [ <NUM_LIT:2> ] <EOL> transcript . append ( phone ) <EOL> phones . update ( transcript ) <EOL> assert utt_id not in transcriptions <EOL> transcriptions [ utt_id ] = transcript <EOL> return transcriptions , phones <EOL> def get_parser ( ) : <EOL> parser = kaldi_argparse . KaldiArgumentParser ( <EOL> description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_standard_arguments ( ) <EOL> return parser <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> parser = get_parser ( ) <EOL> args = parser . parse_args ( ) <EOL> transcriptions , phones = get_phones60 ( args . timit_path ) <EOL> with open ( os . path . join ( args . dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) as tf : <EOL> for tk in sorted ( transcriptions . keys ( ) ) : <EOL> tf . write ( "<STR_LIT>" % ( tk , '<STR_LIT:U+0020>' . join ( transcriptions [ tk ] ) ) ) <EOL> symbols = OrderedDict ( ) <EOL> def symbols_append ( s ) : <EOL> assert s not in symbols <EOL> symbols [ s ] = len ( symbols ) <EOL> for l in sorted ( phones ) : <EOL> symbols_append ( l ) <EOL> symbols_append ( '<STR_LIT>' ) <EOL> with open ( os . path . join ( args . dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) as ow : <EOL> for s , i in symbols . items ( ) : <EOL> ow . write ( '<STR_LIT>' % ( s , i ) ) </s>
