# Path: dlm/reranker/augmenter.py
# def augment(model_path, input_nbest_path, vocab_path, output_nbest_path):
# 	def get_ngrams(tokens):
# 
# Path: dlm/reranker/mosesIniReader.py
# def parseIni(ini_path):


import sys
import os
import shutil
import imp
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
from dlm.reranker import augmenter
from dlm.reranker import mosesIniReader as iniReader
#cmd = moses_root + '/bin/moses -show-weights -f ' + args.input_config + ' 2> /dev/null'
#features = U.capture(cmd).strip().split('\n')
features = iniReader.parseIni(args.input_config)

output_nbest_path = args.out_dir + '/augmented.nbest'

if args.no_aug:
	shutil.copy(args.input_nbest, output_nbest_path)
else:
	augmenter.augment(args.model_path, args.input_nbest, args.vocab_path, output_nbest_path)

L.info('Extracting stats and features')
#L.warning('The optional arguments of extractor are not used yet')
cmd = moses_root + '/bin/extractor -r ' + args.ref_paths + ' -n ' + output_nbest_path + ' --scfile ' + args.out_dir + '/statscore.data --ffile ' + args.out_dir + '/features.data'
U.capture(cmd)

with open(args.out_dir + '/init.opt', 'w') as init_opt:
	init_list = []
	for line in features:
		tokens = line.split(" ")
		try:
			float(tokens[1])
			init_list += tokens[1:]
		except ValueError:
			pass
	if not args.no_aug:
		init_list.append(args.init_value)
	dim = len(init_list)
	init_opt.write(' '.join(init_list) + '\n')
	init_opt.write(' '.join(['0' for i in range(dim)]) + '\n')

# Path: dlm/reranker/augmenter.py
# def augment(model_path, input_nbest_path, vocab_path, output_nbest_path):
# 	def get_ngrams(tokens):
# 
# Path: dlm/reranker/mosesIniReader.py
# def parseIni(ini_path):


# Path: dlm/reranker/train.py
import sys
import os
import shutil
import imp
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
from dlm.reranker import augmenter
from dlm.reranker import mosesIniReader as iniReader

#cmd = moses_root + '/bin/moses -show-weights -f ' + args.input_config + ' 2> /dev/null'
#features = U.capture(cmd).strip().split('\n')
features = iniReader.parseIni(args.input_config)

output_nbest_path = args.out_dir + '/augmented.nbest'

if args.no_aug:
	shutil.copy(args.input_nbest, output_nbest_path)
else:
	augmenter.augment(args.model_path, args.input_nbest, args.vocab_path, output_nbest_path)

L.info('Extracting stats and features')
#L.warning('The optional arguments of extractor are not used yet')
cmd = moses_root + '/bin/extractor -r ' + args.ref_paths + ' -n ' + output_nbest_path + ' --scfile ' + args.out_dir + '/statscore.data --ffile ' + args.out_dir + '/features.data'
U.capture(cmd)

with open(args.out_dir + '/init.opt', 'w') as init_opt:
	init_list = []
	for line in features:
		tokens = line.split(" ")
		try:
			float(tokens[1])
			init_list += tokens[1:]
		except ValueError:
			pass
	if not args.no_aug:
		init_list.append(args.init_value)
	dim = len(init_list)
	init_opt.write(' '.join(init_list) + '\n')
	init_opt.write(' '.join(['0' for i in range(dim)]) + '\n')

	init_opt.write(' '.join(['1' for i in range(dim)]) + '\n')