#!/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, argparse
import csv
import pickle
import re

def get_dict(in_stream, extractor: "tuple -> [2uples] method") -> "{word:data…}":
    """Get a dictionary as as dict using some extracting method."""
    dialect = csv.Sniffer().sniff(in_stream.read(2048))
    in_stream.seek(0)
    input_file = csv.reader(in_stream, dialect)
    result = {}
    for row in input_file:
        try:
            for (word, data) in extractor(row):
                if word not in result:
                    result[word] = [set([str(col)]) if col else set([]) for col in data]
                else:
                    for old, new in zip(result[word], data):
                        if new:
                            old.add(str(new))
        except IndexError:
            continue
    return result

def lglex_lefff_extract(row) -> "(2uple, 2uple,…)":
    """Extract lefff dictionary entry as word:(lemma, compound, POS, type)."""
    words, pos, bundled = row[0], row[2], row[4].split("_")
    words_list = re.split(r"\s|-", words)
    lemma_list = re.split(r"\s|-", bundled[0])
    lemma_list = lemma_list if len(lemma_list) == len(words_list) else [None] * len(words_list)
    compound = len(words_list) > 1
    re.sub("\s", "_", pos)
    bundled = [re.sub("\s", "_" , item) for item in bundled[1:] if item]
    return ((word, (lemma, compound, pos, row[7][1:]))
             for word, lemma in zip(words_list, lemma_list))

def main(argv=sys.argv[1:]):
    """Main function."""
    parser = argparse.ArgumentParser(description="Pickler for Lefff dictionary.")
    parser.add_argument("--in-stream", "-i",
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="Lefff extensive dictionary. (default: stdin)")
    parser.add_argument("--out-stream", "-o",
                        type=argparse.FileType('wb'),
                        default=sys.stdout,
                        help="Output stream. Pickled dictionary. (default: stdout).")

    args = parser.parse_args(argv)

    if args.in_stream.isatty():
        parser.print_help()
    else:
        lefff_dict = get_dict(args.in_stream, lglex_lefff_extract)
        try:
            pickle.dump(lefff_dict, args.out_stream)
        except TypeError:
            args.out_stream.write(str(lefff_dict))

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

