# Path: citation_extractor/Utils/IO.py
# NIL_ENTITY = "urn:cts:GreekLatinLit:NIL"
# def init_logger(log_file=None, loglevel=logging.DEBUG):
# 
# Path: citation_extractor/pipeline.py
# def extract_entity_mentions(text, citation_extractor, postaggers, norm=False):
# def recover_segmentation_errors(text, abbreviation_list, verbose=False):
#     def is_abbreviation(token,abbreviations):
# def get_taggers(treetagger_dir='/Applications/treetagger/cmd/', abbrev_file=None):
# def get_extractor(settings):
# def detect_language(text, return_probability=False):
# def compact_abbreviations(abbreviation_dir):
# def split_sentences(filename, outfilename=None):
# def preproc_document(doc_id, inp_dir, interm_dir, out_dir, abbreviations, taggers, split_sentences=True):
# def validate_configuration(configuration_parameters, task="all"):
#     def is_valid_configuration_ner(configuration_parameters):
#     def is_valid_configuration_relex(configuration_parameters):
#     def is_valid_configuration_ned(configuration_parameters):
# def initialize(configuration):
# def init_working_dir(path, overwrite=False):
# def find_input_documents(task, processing_dirs, doc_id):
# def do_ner(doc_id, inp_dir, interm_dir, out_dir, extractor):
# def do_relex(doc_id, input_dir, output_dir, prefix=None):
# def do_ned(doc_id, input_dir, output_dir, matcher):
# def main():


import sys
import re
import os
import codecs
	import argparse
from citation_extractor.Utils import IO
from citation_extractor.pipeline import read_ann_file

def main():
	parser = argparse.ArgumentParser(description="")
	parser.add_argument("input", type=str, help="IOB input file")
	parser.add_argument("--standoff-dir", help="Stand-off directory",type=str,required=True)
	parser.add_argument("--output-dir", help="IOB output file",type=str,required=True)
	args = parser.parse_args()

	print >> sys.stderr, "IOB Input:", args.input
	print >> sys.stderr, "Stand-off input folder: ",args.standoff_dir
	print >> sys.stderr, "IOB output dir:", args.output_dir

	fname = os.path.split(args.input)[1].split(".")[0]

	# read the correspondant .ann file with stand-off annotation
	so_entities,so_relations,so_annotations = read_ann_file("%s.txt"%fname,args.standoff_dir)

	# extract for each token the start and end
	sentences = process(args.input)
	token_start_end = get_start_end(sentences)
	
	# read IOB from file
	iob_data = IO.file_to_instances(args.input)
	# make sure that data is consistent
	assert [len(sentence) for sentence in iob_data] == [len(sentence) for sentence in token_start_end]

	so_entities = [(so_entities[ent][1],so_entities[ent][0],int(so_entities[ent][2]),int(so_entities[ent][3])) for ent in so_entities.keys()]
	updated_iob_instances = update(token_start_end,iob_data,so_entities)
	try:
		destination = "%s%s.txt"%(args.output_dir,fname)

# Path: citation_extractor/Utils/IO.py
# NIL_ENTITY = "urn:cts:GreekLatinLit:NIL"
# def init_logger(log_file=None, loglevel=logging.DEBUG):
# 
# Path: citation_extractor/pipeline.py
# def extract_entity_mentions(text, citation_extractor, postaggers, norm=False):
# def recover_segmentation_errors(text, abbreviation_list, verbose=False):
#     def is_abbreviation(token,abbreviations):
# def get_taggers(treetagger_dir='/Applications/treetagger/cmd/', abbrev_file=None):
# def get_extractor(settings):
# def detect_language(text, return_probability=False):
# def compact_abbreviations(abbreviation_dir):
# def split_sentences(filename, outfilename=None):
# def preproc_document(doc_id, inp_dir, interm_dir, out_dir, abbreviations, taggers, split_sentences=True):
# def validate_configuration(configuration_parameters, task="all"):
#     def is_valid_configuration_ner(configuration_parameters):
#     def is_valid_configuration_relex(configuration_parameters):
#     def is_valid_configuration_ned(configuration_parameters):
# def initialize(configuration):
# def init_working_dir(path, overwrite=False):
# def find_input_documents(task, processing_dirs, doc_id):
# def do_ner(doc_id, inp_dir, interm_dir, out_dir, extractor):
# def do_relex(doc_id, input_dir, output_dir, prefix=None):
# def do_ned(doc_id, input_dir, output_dir, matcher):
# def main():


# Path: citation_extractor/data/aph_corpus/align_IOB_to_ST_annotations.py
import sys
import re
import os
import codecs
	import argparse
from citation_extractor.Utils import IO
from citation_extractor.pipeline import read_ann_file


def main():
	parser = argparse.ArgumentParser(description="")
	parser.add_argument("input", type=str, help="IOB input file")
	parser.add_argument("--standoff-dir", help="Stand-off directory",type=str,required=True)
	parser.add_argument("--output-dir", help="IOB output file",type=str,required=True)
	args = parser.parse_args()

	print >> sys.stderr, "IOB Input:", args.input
	print >> sys.stderr, "Stand-off input folder: ",args.standoff_dir
	print >> sys.stderr, "IOB output dir:", args.output_dir

	fname = os.path.split(args.input)[1].split(".")[0]

	# read the correspondant .ann file with stand-off annotation
	so_entities,so_relations,so_annotations = read_ann_file("%s.txt"%fname,args.standoff_dir)

	# extract for each token the start and end
	sentences = process(args.input)
	token_start_end = get_start_end(sentences)
	
	# read IOB from file
	iob_data = IO.file_to_instances(args.input)
	# make sure that data is consistent
	assert [len(sentence) for sentence in iob_data] == [len(sentence) for sentence in token_start_end]

	so_entities = [(so_entities[ent][1],so_entities[ent][0],int(so_entities[ent][2]),int(so_entities[ent][3])) for ent in so_entities.keys()]
	updated_iob_instances = update(token_start_end,iob_data,so_entities)
	try:
		destination = "%s%s.txt"%(args.output_dir,fname)

		IO.write_iob_file(updated_iob_instances,destination)