# 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.io.logging as L
import dlm.utils as U
import argparse
import codecs
	import scipy.stats as S
from dlm.io.nbestReader import NBestList
	from dlm.reranker import augmenter
elif args.command.startswith('feature'):
	mode = 2
	N = int(args.command[7:]) # Nth feature
	output = open(args.output_path, mode='w')
elif args.command.startswith('correl'):
	mode = 3
	N = int(args.command[6:]) # Nth feature
	U.xassert(args.oracle, "correlN command needs a file (-s) containing oracle scores")
	with open(args.oracle, mode='r') as oracles_file:
		oracles = map(float, oracles_file.read().splitlines())
	#output = open(args.output_path, mode='w')
elif args.command.startswith('augment'):
	U.set_theano_device(args.device)
	augmenter.augment(args.model_path, args.input_path, args.vocab_path, args.output_path)
else:
	L.error('Invalid command: ' + args.command)

counter = 0
features = []
for group in input_nbest:
	if mode == 0:
		for i in range(min(N, group.size())):
			output_nbest.write(group[i])
	elif mode == 1:
		output_1best.write(group[0].hyp + "\n")
	elif mode == 2:
		for i in range(group.size()):
			features = group[i].features.split()
			output.write(features[N] + "\n")
	elif mode == 3:

# 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/tools.py
import sys
import imp
	import dlm
import dlm.io.logging as L
import dlm.utils as U
import argparse
import codecs
	import scipy.stats as S
from dlm.io.nbestReader import NBestList
	from dlm.reranker import augmenter

elif args.command.startswith('feature'):
	mode = 2
	N = int(args.command[7:]) # Nth feature
	output = open(args.output_path, mode='w')
elif args.command.startswith('correl'):
	mode = 3
	N = int(args.command[6:]) # Nth feature
	U.xassert(args.oracle, "correlN command needs a file (-s) containing oracle scores")
	with open(args.oracle, mode='r') as oracles_file:
		oracles = map(float, oracles_file.read().splitlines())
	#output = open(args.output_path, mode='w')
elif args.command.startswith('augment'):
	U.set_theano_device(args.device)
	augmenter.augment(args.model_path, args.input_path, args.vocab_path, args.output_path)
else:
	L.error('Invalid command: ' + args.command)

counter = 0
features = []
for group in input_nbest:
	if mode == 0:
		for i in range(min(N, group.size())):
			output_nbest.write(group[i])
	elif mode == 1:
		output_1best.write(group[0].hyp + "\n")
	elif mode == 2:
		for i in range(group.size()):
			features = group[i].features.split()
			output.write(features[N] + "\n")
	elif mode == 3:

		for i in range(group.size()):