# -*- coding: utf-8 -*-
"""Process on google OCR.

The files generated by google ocr have some generic issues.
Some of them are amenable to scripting.
This script applies some changes to make it moer readable.

Usage - python3 google_ocr_preprocess.py CODE
e.g. python3 google_ocr_preprocess.py AMAR
"""
import codecs
import re
import sys
import os
import json

# load all dictionary codes and their full forms.
with codecs.open('dictcode.json', 'r', 'utf-8') as fin:
    dictdata = json.load(fin)


def code_to_dict(code):
    """Return the dictionary name from code."""
    return dictdata[code]


def generic_preprocess(text):
    """Apply preprocess changes relevant to all dictionaries."""
    text = text.replace('|', '।')
    text = text.replace('।।', '॥')
    text = re.sub(r'॥ ([^१२३४५६७८९०])', r'॥\n\g<1>', text)
    text = re.sub(r'। ([^१२३४५६७८९०])', r'।\n\g<1>', text)
    return text


def specific_preprocess(text, code):
    """Apply preprocess changes relevant to specific dictionaries."""
    if code in ['ABCH', 'NGSH', 'ACPH', 'ACSJ']:
        text = re.sub(r'॥([ १२३४५६७८९०]+)([^॥])', r'॥\g<1>॥\n\g<2>', text)
    return text


def generic_postprocess(text):
    """Apply postprocess changes relevant to all dictionaries."""
    text = re.sub(r'\n[ -~]+\n', r'\n', text)
    text = re.sub(r'\n[१२३४५६७८९० 1234567890।*=\-]{0,4}\n', r'\n', text)
    text = re.sub(r'(\n)+', r'\n', text)
    text = re.sub(r'॥([१२३४५६७८९०])', r'॥ \g<1>', text)
    text = re.sub(r'([१२३४५६७८९०])॥', r'\g<1> ॥', text)
    text = re.sub(r'([^ ])([।॥])', r'\g<1> \g<2>', text)
    return text


def specific_postprocess(text, code):
    """Apply postprocess changes relevant to specific dictionaries."""
    if code not in ['DKDD', 'SRMS']:
        text = re.sub(r'([^१२३४५६७८९०]) ॥\n', r'\g<1> ।\n', text)
    text = re.sub(r'\n[। ]+\n', '\n', text)
    if code == 'KKTV':
        text = re.sub(r'([।॥]+)[ ]*([१२३४५६७८९०]+)[ ]+([^।॥\n]+)', r'\g<1> \g<2> ॥\n\g<3>', text)
        text = re.sub(r'([१२३४५६७८९०]+)[ ]+\n', r'\g<1> ॥\n', text)
    return text


if __name__ == "__main__":
    code = sys.argv[1]
    dictname = code_to_dict(code)
    print(dictname)
    # Prepare input file and output file names from codes.
    filein = os.path.join('..', dictname, 'orig', dictname + '_googleocr.txt')
    fileout = os.path.join('..', dictname, 'orig', dictname + '_googleocr_adjusted.txt')
    print(filein)
    # Read the data from input file
    data = codecs.open(filein, 'r', 'utf-8').read()
    # Apply changes in sequence.
    data = generic_preprocess(data)
    data = specific_preprocess(data, code)
    data = generic_postprocess(data)
    data = specific_postprocess(data, code)
    print(fileout)
    # Store data in output file.
    fout = codecs.open(fileout, 'w', 'utf-8')
    fout.write(data)
    fout.close()
