import metasentence
import language_model
import standard_kaldi

import diff_align

import json
import os
import sys


vocab = metasentence.load_vocabulary('PROTO_LANGDIR/graphdir/words.txt')

def lm_transcribe(audio_f, text_f):

    ms = metasentence.MetaSentence(open(text_f).read(), vocab)
    model_dir = language_model.getLanguageModel(ms.get_kaldi_sequence())

    print 'generated model', model_dir

    k = standard_kaldi.Kaldi(os.path.join(model_dir, 'graphdir', 'HCLG.fst'))

    trans = standard_kaldi.transcribe(k, audio_f)

    ret = diff_align.align(trans["words"], ms)

    return ret

if __name__=='__main__':
    AUDIO_FILE = sys.argv[1]
    TEXT_FILE = sys.argv[2]
    OUTPUT_FILE = sys.argv[3]

    ret = lm_transcribe(AUDIO_FILE, TEXT_FILE)
    json.dump(ret, open(OUTPUT_FILE, 'w'), indent=2)
    

import metasentence
import language_model
import standard_kaldi

import diff_align

import json
import os
import sys


vocab = metasentence.load_vocabulary('PROTO_LANGDIR/graphdir/words.txt')

def lm_transcribe(audio_f, text_f):

    ms = metasentence.MetaSentence(open(text_f).read(), vocab)
    model_dir = language_model.getLanguageModel(ms.get_kaldi_sequence())

    print 'generated model', model_dir

    k = standard_kaldi.Kaldi(os.path.join(model_dir, 'graphdir', 'HCLG.fst'))

    trans = standard_kaldi.transcribe(k, audio_f)

    ret = diff_align.align(trans["words"], ms)

    return ret

if __name__=='__main__':
    import argparse

    parser = argparse.ArgumentParser(
        description='Align a transcript to audio by generating a new language model.')
    parser.add_argument('audio_file', help='input audio file in any format supported by FFMPEG')
    parser.add_argument('text_file', help='input transcript as plain text')
    parser.add_argument('output_file', type=argparse.FileType('w'),
                       help='output json file for aligned transcript')
    parser.add_argument('--proto_langdir', default="PROTO_LANGDIR",
                       help='path to the prototype language directory')

    args = parser.parse_args()

    ret = lm_transcribe(args.audio_file, args.text_file)
    json.dump(ret, args.output_file, indent=2)
    

Use argparse for main python entrypoint args.