#encoding: utf8
import codecs
from architect2.contexts import window
from architect2.triples import triples
from architect2.processors import dumpingmemprocessor

def window_contexts_generator(paragraph_list, min_par_size=0, window_size_left=3, window_size_right=3):
	for line in paragraph_list:
		par = line.strip().split('\t')[1]
		tokens = par.split()
		if len(tokens) > min_par_size:
			gen = window.window_contexts_generator(tokens, window_size_left, window_size_right)
			for tok in gen:
				yield tok

def triple_gen_wrapper(triple_gen):
	"""
	Changes the relation name (the middle triple element)
	to match one in c++ implementation
	"""

	return ((a, 'SNTWND:' + b, c) for a, b, c in triple_gen)

def main(par_file, dump_file, dump_every=100000, min_par_size=0, window_size_left=3, window_size_right=3):
	proc = dumpingmemprocessor.DumpingMemProcessor(dump_file, dump_every)
	with codecs.open(par_file, 'r', 'utf8') as f:
		proc.process(triple_gen_wrapper(triples.gen_ordered_triples(window_contexts_generator(f, min_par_size, window_size_left, window_size_right))))
	proc.dump(dump_file)

if __name__ == '__main__':
	import sys
	main(sys.argv[1], sys.argv[2], *(int(a) for a in sys.argv[3:]))
