# -*- coding: utf-8 -*-
'''
Regulowe wydobywanie relacji -> 
	Reguly pisane w WCCLu i uruchamiane dla wybranych par/trojek wyspecyfiowane w cfg/ops.ini

Dla zadanego korpusu wejsciowego, wydobywa relacje skladiowe (wykorzystujac podejscie regulowe) 
i tworzy z nich zrzut w postiaci tekstowej - rozszerzonej

Przklad uruchomienia:

python deepened_chunker.py \
		-q -w cfg/ops.ini \
		-d /home/pkedzia/work/corps/kpwr-disamb-1.1.4 \
		-C index_chunks.txt \
		-O
'''

import sys
import corpus2

from npsemrel.rb import wccl_op as wop
from phrase_reader import PhraseReader
from npsemrel.carrot import resource 
import syn_rel as syntx_rel
import deepened_chunker as syntx_rel_gen
from npsemrel.carrot.annotation.dumper import PhraseDumper
from managers import np_adjp_manager as npadjp_man
from managers import fst_pred_manager as fst_man
from managers import snd_pred_manager as snd_man

def make_parser():
	import argparse
	desc = 'Z zadanego korpusu wydobywa relacje skladiowe - tworzy zrzut w postaci tekstowej'
	
	parser = argparse.ArgumentParser(description = desc)
	parser.add_argument('-w', '--wccl-op-cfg-file', dest = 'wccl_op_cfg_file_path', required = True)
	parser.add_argument('-d', '--corpus-dir', dest = 'corpus_dir', required = True)
	parser.add_argument('-C', '--chunk-file', dest = 'chunk_file', required = True)
	parser.add_argument('-t', '--tagset', dest = 'tagset', default = 'nkjp')
	parser.add_argument('-f', '--first-type-predicates', dest = 'fst_type_pred', 
			default = './resources/dictionaries/dict-syn-mpar.lex')
	parser.add_argument('-g', '--first-type-predicates-gerundium', dest = 'ger_dict', 
			default = './resources/dictionaries/dict-gerundium.lex')
	parser.add_argument('-s', '--second-type-predicates', dest = 'snd_type_pred', 
			default = './resources/dictionaries/dict-pred-snd-type.lex')
	parser.add_argument('-O', '--only-continuius', dest = 'only_continuous', action = 'store_true')
	parser.add_argument('-q', '--silent', dest = 'silent', action = 'store_true')
	return parser

def show_syntax_relation(rule_point_str, syntax_relation):
	phr_len_syntax_rel = 'pary'
	if len(syntax_relation.tokens_positions()) == 3:
		phr_len_syntax_rel = 'trojki'
	rel_tokens_as_str = ', '.join(str(t) for t in syntax_relation.tokens_positions())
	print ''
	print '%s. Relacja dla %s (OD - DO): <%s>:' % (rule_point_str, phr_len_syntax_rel, rel_tokens_as_str)


def main(argv = None):
	parser = make_parser()
	args = parser.parse_args(argv)

	# ----------------------------------------------------------------------------
	corpus_dir = args.corpus_dir
	chunks_file = args.chunk_file
	tagset = corpus2.get_named_tagset(args.tagset)

	# ----------------------------------------------------------------------------
	ann_phr_reader = PhraseReader()
	syntax_rel_generator = syntx_rel_gen.SyntaxRelationGenerator()
	res = resource.Resource(tagset)

	# ----------------------------------------------------------------------------
	# wccl_ops = load_wccl_operators(args.wccl_op_cfg_file_path, tagset)

	# load dictionaries for first-type-predicate
	res.load_fst_type_predicate_dictionary(args.fst_type_pred)
	# load gerundium dictionary
	res.load_ger_dict(args.ger_dict)
	# load dictionaries for second-type-predicate
	res.load_snd_type_predicate_dictionary(args.snd_type_pred)

	# ----------------------------------------------------------------------------
	# NP:
	# Liczba fraz chunk_np : 8569
	# Liczba fraz AgP: 14019
	for np_adjp_phrase in ann_phr_reader.read_np_adjp_phrases(
			res, tagset, corpus_dir, chunks_file, 
			ann_phr_reader.NP_ANNOTATION, ann_phr_reader.NP_HEAD_ANNOTATION, 
			args.only_continuous, args.silent):

		# dump frazy:
		npadjpman = npadjp_man.NpAdjpManager(np_adjp_phrase)
		fstman = fst_man.FirstTypePredManager(np_adjp_phrase)
		sndman = snd_man.SecondTypePredManager(np_adjp_phrase)
		dump = PhraseDumper(npadjpman, fstman, sndman)
		dump.dump_phrase(tagset, 0)

		for (rule_point_str, syntax_relation) in syntax_rel_generator.generate_sytax_relations(np_adjp_phrase, res, tagset):
			show_syntax_relation(rule_point_str, syntax_relation)
	
	# ----------------------------------------------------------------------------
	# AdjP:
	# Liczba fraz chunk_adjp : 563
		# Liczba fraz AgP: 1422
	an_phr_annot = ann_phr_reader.ADJP_ANNOTATION
	an_phr_annot_head = ann_phr_reader.ADJP_HEAD_ANNOTATION
	for np_adjp_phrase in ann_phr_reader.read_np_adjp_phrases(
			res, tagset, corpus_dir, chunks_file, an_phr_annot, an_phr_annot_head, args.only_continuous, args.silent):

		# dump frazy:
		npadjpman = npadjp_man.NpAdjpManager(np_adjp_phrase)
		fstman = fst_man.FirstTypePredManager(np_adjp_phrase)
		sndman = snd_man.SecondTypePredManager(np_adjp_phrase)
		dump = PhraseDumper(npadjpman, fstman, sndman)
		dump.dump_phrase(tagset, 0)

		for (rule_point_str, syntax_relation) in syntax_rel_generator.generate_sytax_relations(np_adjp_phrase, res, tagset):
			show_syntax_relation(rule_point_str, syntax_relation)

if __name__ == '__main__':
	main()
