#!/usr/bin/python
# -*- coding: utf-8 -*-
import corpus2, wccl, codecs
import sys

def sentencer(reader):
	while True:
		s = reader.get_next_sentence()
		if s:
			yield s
		else:
			return

def sentence_to_string(sentence):
	return " ".join([unicode(x.orth()) for x in sentence.tokens()])

def print_sentence_with_tags(sentence, tagset):
	for tok in sentence.tokens():
		print tok.orth_utf8()
		for lex in tok.lexemes():
			print '   ', lex.lemma_utf8(), lex.is_disamb(), tagset.tag_to_string(lex.tag())

def load_operator(tagset, filename):
	parser = wccl.Parser(tagset)
	op_str = codecs.open(filename, 'r', 'utf8').read().strip()
	return parser.parseBoolOperator(op_str.encode('utf8'))



def main():
	print "Initialization..."
	tagset = corpus2.get_named_tagset('kipi')

	operator = load_operator(tagset,'./operator.wccl')
	print "Opening corpus"
	reader = corpus2.TokenReader.create_path_reader("poliqarp", tagset, "/home/boban/1002/pq2/1002")
	#reader = corpus2.TokenReader.create_path_reader("poliqarp", tagset, "/home/boban/big/korpusy/poliqarp2/1002/1002")

	#sentence = reader.get_next_sentence()

	#print sentence_to_string(sentence)
	#print [ len(x.lexemes()) for x in sentence.tokens()]
	#print_sentence_with_tags(sentence, tagset)



	for no,sentence in enumerate(sentencer(reader)):
		sc = wccl.SentenceContext(sentence)
		tokens = sentence.tokens()
		for pos in xrange(sentence.size()):
			sc.set_position(pos)
			if operator.apply(sc).get_value():
				left = tokens[pos]
				right_pos = sc.get_abs_position(operator.get_position("A"))
				right = tokens[ right_pos ]
				print sentence_to_string(sentence)
				print pos, left.orth_utf8()
				print right_pos, right.orth_utf8()
		print no
		if no > 10:
			sys.exit(0)
	#	if no % 10000 == 0:
	#		print no, sentence_to_string(sentence)

	#ss = wccl.StrSet()
	#ss.insert_utf8("Decyzja")

	#print operator.apply(wccl.SentenceContext(s)).get_value()
	#operator.set_strset("O", ss)
	#print operator.apply(wccl.SentenceContext(s)).get_value()





if __name__ == '__main__':
	main()
