# Path: dlm/io/nbestReader.py
# class NBestList():
# 	def __init__(self, nbest_path, mode='r', reference_list=None):
# 		U.xassert(mode == 'r' or mode == 'w', "Invalid mode: " + mode)
# 		self.mode = mode
# 		self.nbest_file = codecs.open(nbest_path, mode=mode, encoding='UTF-8')
# 		self.prev_index = -1
# 		self.curr_item = None
# 		self.curr_index = 0
# 		self.eof_flag = False
# 		self.ref_manager = None
# 		if reference_list:
# 			U.xassert(mode == 'r', "Cannot accept a reference_list in 'w' mode")
# 			self.ref_manager = RefernceManager(reference_list)
# 
# 	def __iter__(self):
# 		U.xassert(self.mode == 'r', "Iteration can only be done in 'r' mode")
# 		return self
# 	
# 	def next_item(self):
# 		U.xassert(self.mode == 'r', "next() method can only be used in 'r' mode")
# 		try:
# 			segments = self.nbest_file.next().split("|||")
# 		except StopIteration:
# 			self.close()
# 			raise StopIteration
# 		try:
# 			index = int(segments[0])
# 		except ValueError:
# 			L.error("The first segment in an n-best list must be an integer")
# 		hyp = segments[1].strip()
# 		features = segments[2].strip()
# 		score = segments[3].strip()
# 		phrase_alignments = None
# 		word_alignments = None
# 		if len(segments) > 4:
# 			phrase_alignments = segments[4].strip()
# 		if len(segments) > 5:
# 			word_alignments = segments[5].strip()
# 		return NBestItem(index, hyp, features, score, phrase_alignments, word_alignments)
# 	
# 	def next(self): # Returns a group of NBestItems with the same index
# 		if self.eof_flag == True:
# 			raise StopIteration
# 		U.xassert(self.mode == 'r', "next_group() method can only be used in 'r' mode")
# 		group = NBestGroup(self.ref_manager)
# 		group.add(self.curr_item) # add the item that was read in the last next() call
# 		try:
# 			self.curr_item = self.next_item()
# 		except StopIteration:
# 			self.eof_flag = True
# 			return group
# 		if self.curr_index != self.curr_item.index:
# 			self.curr_index = self.curr_item.index
# 			return group
# 		while self.curr_index == self.curr_item.index:
# 			group.add(self.curr_item)
# 			try:
# 				self.curr_item = self.next_item()
# 			except StopIteration:
# 				self.eof_flag = True
# 				return group
# 		self.curr_index = self.curr_item.index
# 		return group
# 	
# 	def write(self, item):
# 		U.xassert(self.mode == 'w', "write() method can only be used in 'w' mode")
# 		self.nbest_file.write(unicode(item) + "\n")
# 	
# 	def close(self):
# 		self.nbest_file.close()
# 
# Path: dlm/reranker/augmenter.py
# def augment(model_path, input_nbest_path, vocab_path, output_nbest_path):
# 	def get_ngrams(tokens):


import sys
import os
import imp
import shutil
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
import codecs
import numpy as np
from dlm.io.nbestReader import NBestList
from dlm.reranker import augmenter
parser.add_argument("-o", "--output-dir", dest="out_dir", required=True, help="Output directory")
parser.add_argument("-n", "--no-aug", dest="no_aug", action='store_true', help="Augmentation will be skipped, if this flag is set")
parser.add_argument("-c", "--clean-up", dest="clean_up", action='store_true', help="Temporary files will be removed")
parser.add_argument("-q", "--quiet", dest="quiet", action='store_true', help="Nothing will be printed in STDERR")
args = parser.parse_args()

if args.quiet:
	L.quiet = True

U.set_theano_device(args.device)


U.mkdir_p(args.out_dir)


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)

with open(args.weights, 'r') as input_weights:
	lines = input_weights.readlines()
	if len(lines) > 1:
		L.warning("Weights file has more than one line. I'll read the 1st and ignore the rest.")
	weights = np.asarray(lines[0].strip().split(" "), dtype=float)

prefix = os.path.basename(args.input_nbest)
input_aug_nbest = NBestList(output_nbest_path, mode='r')

# Path: dlm/io/nbestReader.py
# class NBestList():
# 	def __init__(self, nbest_path, mode='r', reference_list=None):
# 		U.xassert(mode == 'r' or mode == 'w', "Invalid mode: " + mode)
# 		self.mode = mode
# 		self.nbest_file = codecs.open(nbest_path, mode=mode, encoding='UTF-8')
# 		self.prev_index = -1
# 		self.curr_item = None
# 		self.curr_index = 0
# 		self.eof_flag = False
# 		self.ref_manager = None
# 		if reference_list:
# 			U.xassert(mode == 'r', "Cannot accept a reference_list in 'w' mode")
# 			self.ref_manager = RefernceManager(reference_list)
# 
# 	def __iter__(self):
# 		U.xassert(self.mode == 'r', "Iteration can only be done in 'r' mode")
# 		return self
# 	
# 	def next_item(self):
# 		U.xassert(self.mode == 'r', "next() method can only be used in 'r' mode")
# 		try:
# 			segments = self.nbest_file.next().split("|||")
# 		except StopIteration:
# 			self.close()
# 			raise StopIteration
# 		try:
# 			index = int(segments[0])
# 		except ValueError:
# 			L.error("The first segment in an n-best list must be an integer")
# 		hyp = segments[1].strip()
# 		features = segments[2].strip()
# 		score = segments[3].strip()
# 		phrase_alignments = None
# 		word_alignments = None
# 		if len(segments) > 4:
# 			phrase_alignments = segments[4].strip()
# 		if len(segments) > 5:
# 			word_alignments = segments[5].strip()
# 		return NBestItem(index, hyp, features, score, phrase_alignments, word_alignments)
# 	
# 	def next(self): # Returns a group of NBestItems with the same index
# 		if self.eof_flag == True:
# 			raise StopIteration
# 		U.xassert(self.mode == 'r', "next_group() method can only be used in 'r' mode")
# 		group = NBestGroup(self.ref_manager)
# 		group.add(self.curr_item) # add the item that was read in the last next() call
# 		try:
# 			self.curr_item = self.next_item()
# 		except StopIteration:
# 			self.eof_flag = True
# 			return group
# 		if self.curr_index != self.curr_item.index:
# 			self.curr_index = self.curr_item.index
# 			return group
# 		while self.curr_index == self.curr_item.index:
# 			group.add(self.curr_item)
# 			try:
# 				self.curr_item = self.next_item()
# 			except StopIteration:
# 				self.eof_flag = True
# 				return group
# 		self.curr_index = self.curr_item.index
# 		return group
# 	
# 	def write(self, item):
# 		U.xassert(self.mode == 'w', "write() method can only be used in 'w' mode")
# 		self.nbest_file.write(unicode(item) + "\n")
# 	
# 	def close(self):
# 		self.nbest_file.close()
# 
# Path: dlm/reranker/augmenter.py
# def augment(model_path, input_nbest_path, vocab_path, output_nbest_path):
# 	def get_ngrams(tokens):


# Path: dlm/reranker/rerank.py
import sys
import os
import imp
import shutil
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
import codecs
import numpy as np
from dlm.io.nbestReader import NBestList
from dlm.reranker import augmenter

parser.add_argument("-o", "--output-dir", dest="out_dir", required=True, help="Output directory")
parser.add_argument("-n", "--no-aug", dest="no_aug", action='store_true', help="Augmentation will be skipped, if this flag is set")
parser.add_argument("-c", "--clean-up", dest="clean_up", action='store_true', help="Temporary files will be removed")
parser.add_argument("-q", "--quiet", dest="quiet", action='store_true', help="Nothing will be printed in STDERR")
args = parser.parse_args()

if args.quiet:
	L.quiet = True

U.set_theano_device(args.device)


U.mkdir_p(args.out_dir)


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)

with open(args.weights, 'r') as input_weights:
	lines = input_weights.readlines()
	if len(lines) > 1:
		L.warning("Weights file has more than one line. I'll read the 1st and ignore the rest.")
	weights = np.asarray(lines[0].strip().split(" "), dtype=float)

prefix = os.path.basename(args.input_nbest)
input_aug_nbest = NBestList(output_nbest_path, mode='r')

output_nbest = NBestList(args.out_dir + '/' + prefix + '.reranked.nbest', mode='w')