#!/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/>.
#==============================================================================

""" Transcriber's text exports cleaner

Created on 14 mai 2011
@author: Loïc Grobol <loic.grobol@gmail.com>

This is a all-in-one module for cleaning Transcriber's text exports.

The typical Transcriber text export is something like this

    JK:  hm hm ils ont combien d'ans à peu près ?
    VS 466:  #1 on les prend à #
    JK:  #2 [pron=pif] #
    VS 466:  à huit ans
    JK:  #1 ah oui #
    VS 466:  #2 jusque # seize ans oui

This module process such files and clean following items:
    - Speaker code, colon and spaces at line's beginning
    - All # statements
    - All statements [between brackets]
    - Replace special compound statements, e.g. (fr) aux -> à les
    
So the result should something like

    hm hm ils ont combien d'ans à peu près ?
    on les prend à
    
    à huit ans
    ah oui 
    jusque seize ans oui
    
The processing is made line-wise and can be applied to
any stream, (e.g. stdin) or text file and redirected the same way.

A typical call would be

    clean -i raw_transcriber_file -o clean

By default input is stdin and output is stdout for easy piping.

clean --help will also give you an usage guide.
"""
import sys, csv, os
import argparse

import re

# 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__ = "formats.transcriber_export_fr"

from formats.transcriber_export_fr import CUSTOM_LEXEMES

# Expansions and other replacements
REPLACEMENTS = {(r"\b" + lex + r"\b"):" ".join(words) for lex, words in CUSTOM_LEXEMES.items()}
_replacements_default_file = os.path.join(path[0], "replacements")

_replacements_patterns = {re.compile(input):output for input, output in REPLACEMENTS.items()}

def clean_stream(in_stream, out_stream, replacements):
    """Delete all same-line occurrences of _delete_pattern from
       %in_stream and write the output in %out_stream."""
    buffer = in_stream.read()
    for input, output in replacements.items():
        buffer = re.sub(input, output, buffer)
    out_stream.write(buffer)

def main(argv=sys.argv[1:]):
    """Main function."""
    parser = argparse.ArgumentParser(description="Clean text exports of Transcriber.")
    parser.add_argument("--in_stream", "-i",
                         type=argparse.FileType('r'),
                         default=sys.stdin,
                         help="Transcriber export file to be cleansed. Standard input by default.")
    parser.add_argument("--out_stream", "-o",
                         type=argparse.FileType('w'),
                         default=sys.stdout,
                         help="Clean file to be written. Standard output by default.")
    parser.add_argument("--replacements",
                        type=argparse.FileType('r'),
                        default=None,
                        help="File that define substitutions.")

    args = parser.parse_args(argv)

    if args.replacements or os.path.exists(_replacements_default_file):
        args.replacements = args.replacements or open(_replacements_default_file)
        sniffer = csv.Sniffer()
        # Guess the format from the 1024 first characters
        dialect = sniffer.sniff(args.replacements.read(4096))
        args.replacements.seek(0)
        reader = csv.DictReader(args.replacements, dialect=dialect, fieldnames=("input", "output"))
        _replacements_patterns = {re.compile(row['input'], re.MULTILINE):row['output'] for row in reader}

    if args.in_stream.isatty():
        parser.print_help()
    else:
        clean_stream(args.in_stream, args.out_stream, _replacements_patterns)

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