#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#==============================================================================
# Copyright (C) 2011 by Loïc Grobol
#
# This file is part of CTESLO.
# 
#    CTESLO is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
# 
#    CTESLO is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with CTESLO.  If not, see <http://www.gnu.org/licenses/>.
#==============================================================================

'''
Created on 23 mai 2011

@author: Loïc Grobol <loic.grobol@gmail.com>
'''

import sys, argparse

# Ugly hack to allow absolute import from the root folder
# whatever its name is. Please forgive the heresy.
if __name__ == "__main__" and __package__ is None:
    from sys import path
    from os.path import dirname as dir
    path.append(dir(dir(path[0])))

import libcteslo.dflib.tokenization as tok
import libcteslo.dflib.abstractions as dfa


def main(argv=sys.argv[1:]):


#==============================================================================
# Common arguments
#==============================================================================
    parent = argparse.ArgumentParser(add_help=False)

    parent.add_argument("--in-stream", "-i",
                         type=argparse.FileType('r'), default=sys.stdin,
                         help="Text data file to be processed. (default: stdin).")
    parent.add_argument("--out-stream", "-o",
                         type=argparse.FileType('w'), default=sys.stdout,
                         help="Output text data file. (default: stdout).")
    parent.add_argument("--column-delimiter", "-c",
                        default=dfa.DEFAULT_DELIMITER,
                        help="""Regex : columns delimiter for the data file.
                         (default: <TAB> character).""")
    parent.add_argument("--words_delimiters", "-w",
                        default=tok.DEFAULT_DELIMITERS,
                        help="Regex : words delimiters.(default: {0}).".format(
                                                        tok.DEFAULT_DELIMITERS.pattern))
    parent.add_argument("--BIO-tags", "-B",
                        default=tok.DEFAULT_BIO,
                        help=("BIO tags. (default: %(default)s)."))
    parent.add_argument("--BIO-column",
                        type=int,
                        default=tok.DEFAULT_BIO_COLUMN,
                        help="BIO column. (default: last column (append)).")
    parent.add_argument("--lexeme-column",
                        type=int,
                        default=tok.DEFAULT_LEXEME_COLUMN,
                        help="""Lexeme's column in both input and output file.
                         (default: first column).""")
    parent.add_argument("--custom-lexemes", "-l",
                    nargs="*", default=None,
                    metavar="LEXEME:WORD,WORD,...",
                    help="A list of custom lexemes and corresponding tokenization")
#==============================================================================
# Main parser
#==============================================================================
    parser = argparse.ArgumentParser(
            parents=[parent],
            description="""Tokenize a text data file 
                        and add BIO tags as a new column.""")

#==============================================================================
# Subparsers (commands)
#==============================================================================
    subparsers = parser.add_subparsers(help="sub-commands")
    parser_tokenize = subparsers.add_parser(
                        "tokenize",
                        parents=[parent],
                        description="Tokenize the lexemes of a text data file.")
    parser_lexemize = subparsers.add_parser(
                        "lexemize",
                        parents=[parent],
                        description="Lexemize the words of a text data file.")

#==============================================================================
# tokenize command defaults and arguments    
#==============================================================================
    parser_tokenize.add_argument("--missing-characters-placeholder", "-D",
                        default=None,
                        help="""Placeholder for missing characters in lexemes
                         (e.g. lost unicode characters)""")

    parser_tokenize.set_defaults(func=tok.stream_tokenize,
                                 selected=(("in_stream", "stream_in"),
                                           ("out_stream", "stream_out"),
                                           ("column_delimiter", "column_delimiter"),
                                           ("words_delimiters", "words_delimiters"),
                                           ("BIO_tags", "bio"),
                                           ("BIO_column", "bio_column"),
                                           ("lexeme_column", "lexeme_column"),
                                           ("missing_characters_placeholder",
                                                 "missing_characters_placeholder"),
                                           ("custom_lexemes", "custom_lexemes")))

#==============================================================================
# lexemize command defaults and arguments    
#==============================================================================
    parser_lexemize.add_argument("--default-words-delimiter", "-P",
                        default=tok.DEFAULT_DELIMITER,
                        help="""Delimiter between non-delimiter words.
                                (default: %(default)s).""")
    parser_lexemize.set_defaults(func=tok.stream_lexemize,
                                 selected=(("in_stream", "stream_in"),
                                           ("out_stream", "stream_out"),
                                           ("column_delimiter", "column_delimiter"),
                                           ("words_delimiters", "words_delimiters"),
                                           ("default_words_delimiter", "default_words_delimiter"),
                                           ("BIO_tags", "bio"),
                                           ("BIO_column", "bio_column"),
                                           ("lexeme_column", "lexeme_column")))

#==============================================================================
# parsing and running
#==============================================================================
    args = parser.parse_args(argv)
    dargs = vars(args)

    try:
        # ugly
        args.custom_lexemes = {lexeme:tuple(tokens.split(",")) for (lexeme, tokens) in
                                            [elem.split(":") for elem in args.custom_lexemes]}
    except TypeError:
        args.custom_lexemes = dict([])

    if args.in_stream.isatty():
        parser.print_help()
    else:
        pass_args = {argument: dargs[keyword] for (keyword, argument) in args.selected
                                    if dargs[keyword] is not None}
        args.func(**pass_args)

if __name__ == "__main__":
    sys.exit(main())
