def main(options):
    if (options.output_dir is None):
        options.output_dir = options.working_dir
    elif (not os.path.exists(options.output_dir)):
        os.makedirs(options.output_dir)
    options.ngram_size = (((2 * options.up_context_size) + (2 * options.left_context_size)) + (2 * options.right_context_size))
    if (options.mode == 'head'):
        options.ngram_size += 2
    elif (options.mode == 'label'):
        options.ngram_size += 1
    if ((options.input_words_file is None) or (options.output_words_file is None)):
        sys.stderr.write('Either input vocabulary or output vocabulary not specified: extracting vocabulary from training text.\n')
        prepare_vocabulary(options)
    numberized_file = (os.path.basename(options.corpus_stem) + '.numberized')
    train_file = numberized_file
    if options.mmap:
        train_file += '.mmap'
    extract_options = extract_syntactic_ngrams.create_parser().parse_args(['--input', options.corpus_stem, '--output', os.path.join(options.working_dir, numberized_file), '--vocab', options.input_words_file, '--output_vocab', options.output_words_file, '--right_context', str(options.right_context_size), '--left_context', str(options.left_context_size), '--up_context', str(options.up_context_size), '--mode', options.mode])
    sys.stderr.write('extracting syntactic n-grams\n')
    extract_syntactic_ngrams.main(extract_options)
    if options.validation_corpus:
        extract_options.input = open(options.validation_corpus)
        options.validation_file = os.path.join(options.working_dir, os.path.basename(options.validation_corpus))
        extract_options.output = open((options.validation_file + '.numberized'), 'w')
        sys.stderr.write('extracting syntactic n-grams (validation file)\n')
        extract_syntactic_ngrams.main(extract_options)
        extract_options.output.close()
    else:
        options.validation_file = None
    if options.mmap:
        try:
            os.remove(os.path.join(options.working_dir, train_file))
        except OSError:
            pass
        mmap_cmd = [os.path.join(options.nplm_home, 'src', 'createMmap'), '--input_file', os.path.join(options.working_dir, numberized_file), '--output_file', os.path.join(options.working_dir, train_file)]
        sys.stderr.write('creating memory-mapped file\n')
        sys.stderr.write((('executing: ' + ', '.join(mmap_cmd)) + '\n'))
        ret = subprocess.call(mmap_cmd)
        if ret:
            raise Exception('creating memory-mapped file failed')
    sys.stderr.write('training neural network\n')
    train_nplm.main(options)
    sys.stderr.write('averaging null words\n')
    ret = subprocess.call([os.path.join(sys.path[0], 'average_null_embedding.py'), options.nplm_home, os.path.join(options.output_dir, ((options.output_model + '.model.nplm.') + str(options.epochs))), os.path.join(options.working_dir, numberized_file), os.path.join(options.output_dir, (options.output_model + '.model.nplm'))])
    if ret:
        raise Exception('averaging null words failed')
def main(options):
    if (options.output_dir is None):
        options.output_dir = options.working_dir
    elif (not os.path.exists(options.output_dir)):
        os.makedirs(options.output_dir)
    options.ngram_size = (((2 * options.up_context_size) + (2 * options.left_context_size)) + (2 * options.right_context_size))
    if (options.mode == 'head'):
        options.ngram_size += 2
    elif (options.mode == 'label'):
        options.ngram_size += 1
    if ((options.input_words_file is None) or (options.output_words_file is None)):
        sys.stderr.write('Either input vocabulary or output vocabulary not specified: extracting vocabulary from training text.\n')
        prepare_vocabulary(options)
    numberized_file = (os.path.basename(options.corpus_stem) + '.numberized')
    train_file = numberized_file
    if options.mmap:
        train_file += '.mmap'
    extract_options = extract_syntactic_ngrams.create_parser().parse_args(['--input', options.corpus_stem, '--output', os.path.join(options.working_dir, numberized_file), '--vocab', options.input_words_file, '--output_vocab', options.output_words_file, '--right_context', str(options.right_context_size), '--left_context', str(options.left_context_size), '--up_context', str(options.up_context_size), '--mode', options.mode])
    sys.stderr.write('extracting syntactic n-grams\n')
    extract_syntactic_ngrams.main(extract_options)
    if options.validation_corpus:
        extract_options.input = open(options.validation_corpus)
        options.validation_file = os.path.join(options.working_dir, os.path.basename(options.validation_corpus))
        extract_options.output = open((options.validation_file + '.numberized'), 'w')
        sys.stderr.write('extracting syntactic n-grams (validation file)\n')
        extract_syntactic_ngrams.main(extract_options)
        extract_options.output.close()
    else:
        options.validation_file = None
    if options.mmap:
        try:
            os.remove(os.path.join(options.working_dir, train_file))
        except OSError:
            pass
        mmap_cmd = [os.path.join(options.nplm_home, 'src', 'createMmap'), '--input_file', os.path.join(options.working_dir, numberized_file), '--output_file', os.path.join(options.working_dir, train_file)]
        sys.stderr.write('creating memory-mapped file\n')
        sys.stderr.write((('executing: ' + ', '.join(mmap_cmd)) + '\n'))
        ret = subprocess.call(mmap_cmd)
        if ret:
            raise Exception('creating memory-mapped file failed')
    sys.stderr.write('training neural network\n')
    train_nplm.main(options)
    sys.stderr.write('averaging null words\n')
    ret = subprocess.call([os.path.join(sys.path[0], 'average_null_embedding.py'), options.nplm_home, os.path.join(options.output_dir, ((options.output_model + '.model.nplm.') + str(options.epochs))), os.path.join(options.working_dir, numberized_file), os.path.join(options.output_dir, (options.output_model + '.model.nplm'))])
    if ret:
        raise Exception('averaging null words failed')