# -*- coding: utf-8 -*-
'''
Created on Jul 26, 2012

@author: boban
'''
import codecs
from architect2.processors.abstract import AbstractProcessor

class DumpingMemProcessor(AbstractProcessor):
	"""
	Extracts 'dependency' triples without using external storage as MemProcessor, but dumps data by itself.
	"""

	def __init__(self, dump_file, dump_every = 100000):
		super(DumpingMemProcessor, self).__init__()
		self.dump_counter = 0
		self.dump_threshold = dump_every
		self.dump_file = dump_file

	def process(self, triples_generator):
		"""
		Adds or 'relation' triples from the given document.
		"""
		for l,rel,r in triples_generator:
			k = (l,rel,r)
			if k in self.data:
				self.data[k] += 1
			else:
				self.data[k] = 1
			self.dump_counter += 1
			if self.dump_counter > self.dump_threshold:
				self.dump(self.dump_file)
		self.dump(self.dump_file)

	def dump(self, filename, append=True):
		"""
		Writes data to simple CSV file.

		File format:
		head;relation;modifier;count 
		"""
		mode = 'w'
		if append:
			mode = 'a'
		with codecs.open(filename, mode, 'utf8') as f:
			for k in self.data:
				l,rel, r = k
				f.write('%s;%s;%s;%d\n' % (l,rel,r, self.data[k]))
		self.data = {}
		self.dump_counter = 0
