#!/usr/bin/env python2.7
# -*- coding: utf-8; -*-

##################################################################
# Libraries
import langid
import sys

from alt_argparse import argparser
from alt_fio import AltFileInput, AltFileOutput

##################################################################
# Arguments
argparser.description = "Utility for detecting text language."
argparser.add_argument("-l", "--language", \
                         help="language code for language which should be extracted", \
                         default = 'de')
argparser.add_argument("-n", "--no-lang-filter", action="store_true", default=False, \
                         help="don't filter lines by language")
argparser.add_argument("-q", "--quiet", action="store_true", default=False, \
                         help="don't print warning messages about skipped lines")
argparser.add_argument("-x", "--skip-xml", action="store_true", default=False, \
                         help="don't process lines which appear to be a single XML tag")
args = argparser.parse_args()

language = args.language
nquiet   = not args.quiet

##################################################################
# Main
foutput   = AltFileOutput(encoding = args.encoding, \
                            flush = args.flush)
ferror    = AltFileOutput(ofile = sys.stderr, \
                            encoding = args.encoding, \
                            flush = args.flush)
finput    = AltFileInput(*args.files, \
                            skip_line = args.skip_line, \
                            skip_xml = args.skip_xml, \
                            print_func = foutput.fprint, \
                            errors = 'replace')

if args.no_lang_filter:
  for line in finput:
    foutput.fprint(line)
else:
  lang   = ""
  weight = 0
  for line in finput:
    lang, weight = langid.classify(line)
    if lang == language:
      foutput.fprint(line)
    elif nquiet:
      ferror.fprint(u"Line '" + line + u"' skipped as", lang, \
                      "with certainty", weight)
