# 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()


import sys
import imp
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
import dlm.reranker.bleu as B
import codecs
from dlm.io.nbestReader import NBestList
from multiprocessing import Pool
	print "[ERROR] dlm module not found. Add CoreLM root directory to your PYTHONPATH"
	sys.exit()


parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", dest="input_path", required=True, help="Input n-best file")
parser.add_argument("-r", "--reference-files", dest="ref_paths", required=True, help="A comma-seperated list of reference files")
parser.add_argument("-o", "--output-nbest-file", dest="out_nbest_path", help="Output oracle n-best file")
parser.add_argument("-b", "--output-1best-file", dest="out_1best_path", required=True, help="Output oracle 1-best file")
parser.add_argument("-s", "--output-scores", dest="out_scores_path", help="Output oracle scores file")
parser.add_argument("-m", "--smoothing-method", dest="method", required=True, help="Smoothing method (none|epsilon|lin|nist|chen)")
parser.add_argument("-t", "--threads", dest="threads", type=int, default=14, help="Number of threads")
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

methods = {
	'none'    : B.no_smoothing,
	'epsilon' : B.add_epsilon_smoothing,
	'lin'     : B.lin_smoothing,
	'nist'    : B.nist_smoothing,
	'chen'    : B.chen_smoothing
}

ref_path_list = args.ref_paths.split(',')

input_nbest = NBestList(args.input_path, mode='r', reference_list=ref_path_list)
if args.out_nbest_path:

# 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/oracle.py
import sys
import imp
	import dlm
import dlm.utils as U
import dlm.io.logging as L
import argparse
import dlm.reranker.bleu as B
import codecs
from dlm.io.nbestReader import NBestList
from multiprocessing import Pool

	print "[ERROR] dlm module not found. Add CoreLM root directory to your PYTHONPATH"
	sys.exit()


parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", dest="input_path", required=True, help="Input n-best file")
parser.add_argument("-r", "--reference-files", dest="ref_paths", required=True, help="A comma-seperated list of reference files")
parser.add_argument("-o", "--output-nbest-file", dest="out_nbest_path", help="Output oracle n-best file")
parser.add_argument("-b", "--output-1best-file", dest="out_1best_path", required=True, help="Output oracle 1-best file")
parser.add_argument("-s", "--output-scores", dest="out_scores_path", help="Output oracle scores file")
parser.add_argument("-m", "--smoothing-method", dest="method", required=True, help="Smoothing method (none|epsilon|lin|nist|chen)")
parser.add_argument("-t", "--threads", dest="threads", type=int, default=14, help="Number of threads")
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

methods = {
	'none'    : B.no_smoothing,
	'epsilon' : B.add_epsilon_smoothing,
	'lin'     : B.lin_smoothing,
	'nist'    : B.nist_smoothing,
	'chen'    : B.chen_smoothing
}

ref_path_list = args.ref_paths.split(',')

input_nbest = NBestList(args.input_path, mode='r', reference_list=ref_path_list)
if args.out_nbest_path:

	output_nbest = NBestList(args.out_nbest_path, mode='w')