#!/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 1 juil. 2011

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

import sys, os
import argparse
import pickle

# 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])))
    __package__ = "external_tools.lglex_lefff"

from formats.columnize import format_line

DEFAULT_DATA = "."
DEFAULT_PICKLED = os.path.join(path[0], "pickled")

def tag_stream(in_stream, out_stream, dictionary=DEFAULT_PICKLED):
    # Default number of columns for not found words (ugly)
    length = len(next(iter(dictionary.values())))
    _default_data = [set([DEFAULT_DATA])] * length
    for line in in_stream:
        formatted = format_line(line)
        formatted = formatted.strip(" \n")
        for word in formatted.splitlines():
            if word in dictionary:
                data = dictionary[word]
            else:
                data = _default_data
            data = (",".join(sorted(col)) if col else DEFAULT_DATA for col in data)
            data = [word] + list(data)
            out_stream.write("{0}\n".format("\t".join(data)))

plug = tag_stream

def main(argv=sys.argv[1:]):
    """Main function."""
    parser = argparse.ArgumentParser(description="Tagger from a pickled dictionary")
    parser.add_argument("--in-stream", "-i",
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="Input file to tag. (default: stdin)")
    parser.add_argument("--out-stream", "-o",
                        type=argparse.FileType('w'),
                        default=sys.stdout,
                        help="Output stream. Tagged file. (default: stdout).")
    parser.add_argument("--pickled",
                        type=argparse.FileType('rb'),
                        default=DEFAULT_PICKLED,
                        help="Pickled dictionary to use. (default: %(default)s).")

    args = parser.parse_args(argv)

    if args.in_stream.isatty():
        parser.print_help()
    else:
        lefff_dict = pickle.load(args.pickled)
        tag_stream(args.in_stream, args.out_stream, lefff_dict)

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