# -*- coding: utf-8 -*-
'''
Created on Sep 21, 2010

@author: Bartosz Broda, bartosz.broda@gmail.com
'''
import codecs

import nltk.tokenize as tokenize

from architect2 import contexts
import triples

def words_lower_triples_from_filename(filename, lsize = 3, rsize = 3, token_filter = lambda token: False):
	r"""
	Returns generator of lower case triples when the data is read from file. 
	Note, that filtering is done in triples_gen, so it has to be made case-insensitive explicitly.
	
	>>> def mock_open(f, m='r', e=None):
	...     import tempfile
	...     tf = tempfile.TemporaryFile()
	...     tf.write("A b C d")
	...     tf.seek(0)
	...     return tf
	... 
	>>> orig_open = codecs.open
	>>> codecs.open = mock_open
	>>> gen = words_lower_triples_from_filename('dummy_filename',2,2,)
	>>> gen.next() == ('a', u'UNORDERED', 'b')
	True
	>>> gen.next() == ('a', u'UNORDERED', 'c')
	True
	>>> gen.next() == ('b', u'UNORDERED', 'a')
	True
	>>> gen.next() == ('b', u'UNORDERED', 'c')
	True
	>>> gen.next() == ('b', u'UNORDERED', 'd')
	True
	>>> gen.next() == ('c', u'UNORDERED', 'a')
	True
	>>> gen.next() == ('c', u'UNORDERED', 'b')
	True
	>>> gen.next() == ('c', u'UNORDERED', 'd')
	True
	>>> gen.next() == ('d', u'UNORDERED', 'b')
	True
	>>> gen.next() == ('d', u'UNORDERED', 'c')
	True
	>>> gen.next()
	Traceback (most recent call last):
	...
	StopIteration
	>>> codecs.open = orig_open
	"""
	document = codecs.open(filename, 'r', 'utf8').read().strip().lower()
	g = contexts.window.window_contexts_generator( tokenize.WordPunctTokenizer().tokenize(document), size_left = lsize, size_right = rsize )
	return triples_to_lower( triples.gen_unordered_triples(g, token_filter, token_filter ) )

def triples_to_lower(triples_gen):
	r"""
	Converts resulting triples to lower case.
	Note, that filtering is done in triples_gen, so it has to be made case-insensitive explicitly. 
	
	>>> tokens = ['A', 'b', 'C', 'd']
	>>> center_cond = lambda t : t == 'b'
	>>> right_cond = lambda t : t == 'C'
	>>> inner_gen = triples.gen_unordered_triples(contexts.window.window_contexts_generator(tokens, 3,2), center_cond, right_cond)
	>>> gen = triples_to_lower(inner_gen)
	>>> gen.next() 
	('a', u'UNORDERED', 'b')
	>>> gen.next() == ('c', u'UNORDERED', 'a')
	True
	>>> gen.next() == ('c', u'UNORDERED', 'b')
	True
	>>> gen.next() == ('c', u'UNORDERED', 'd')
	True
	>>> gen.next() == ('d', u'UNORDERED', 'a')
	True
	>>> gen.next() == ('d', u'UNORDERED', 'b')
	True
	>>> gen.next()
	Traceback (most recent call last):
	...
	StopIteration
	"""
	for (left,relation,right) in triples_gen:
		yield (left.lower(), relation, right.lower())

if __name__ == '__main__':
	import doctest
	doctest.testmod()