# -*- coding: utf-8 -*-
'''
Created on Dec 10, 2010

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

class MemProcessor(AbstractProcessor):
	"""
	Extracts 'dependency' triples without using external storage.
	"""

	def __init__(self):
		super(MemProcessor, self).__init__()

	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

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

		File format:
		head;relation;modifier;count 
		"""
		with codecs.open(filename, 'w', '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 = {}
