# Path: tlfp/tools/sort_articles.py
# def split_article(a):
# def quantify_bis(b):
# def type_detail(d):
# def compare_details(a, b):
# def compare_articles(a, b):
# def article_is_lower(a, b):


import sys, os, time, re, requests, io
import json
import locale
import contextlib
from datetime import date, datetime
from html.entities import name2codepoint
from csv import DictReader
from difflib import SequenceMatcher
from diff_match_patch import diff_match_patch
from .sort_articles import bister
from lawfactory_utils.urls import download
decode_char = lambda x: chr(int(x.group(1)[1:]) if x.group(1).startswith('#') else name2codepoint[x.group(1)])
decode_html = lambda text: re_entities.sub(decode_char, text)

re_clean_spaces = re.compile(r"[\s\n]+")
clean_spaces = lambda x: re_clean_spaces.sub(" ", x)

re_clean_balises = re.compile(r"<\/?[!a-z][^>]*>", re.I)
clean_balises = lambda x: re_clean_balises.sub("", x)

clean_statuses = lambda x: re_alin_sup.sub("", x)

strip_text = lambda x: clean_spaces(clean_statuses(clean_balises(x))).strip()

re_non_alphanum = re.compile(r"[^a-z0-9]+")

upcase_accents = "ÇÀÂÄÉÈÊËÎÏÔÖÙÛÜ"
locase_accents = "çàâäéèêëîïôöùûü"
case_noaccents = "caaaeeeeiioouuu"

def real_lower(text):
    for a in upcase_accents:
        text = text.replace(a, locase_accents[upcase_accents.find(a)])
    return text.lower()

def clean_accents(text):
    text = real_lower(text)
    for a in locase_accents:
        text = text.replace(a, case_noaccents[locase_accents.find(a)])
    return text


# Path: tlfp/tools/sort_articles.py
# def split_article(a):
# def quantify_bis(b):
# def type_detail(d):
# def compare_details(a, b):
# def compare_articles(a, b):
# def article_is_lower(a, b):


# Path: tlfp/tools/common.py
import sys, os, time, re, requests, io
import json
import locale
import contextlib
from datetime import date, datetime
from html.entities import name2codepoint
from csv import DictReader
from difflib import SequenceMatcher
from diff_match_patch import diff_match_patch
from .sort_articles import bister
from lawfactory_utils.urls import download

decode_char = lambda x: chr(int(x.group(1)[1:]) if x.group(1).startswith('#') else name2codepoint[x.group(1)])
decode_html = lambda text: re_entities.sub(decode_char, text)

re_clean_spaces = re.compile(r"[\s\n]+")
clean_spaces = lambda x: re_clean_spaces.sub(" ", x)

re_clean_balises = re.compile(r"<\/?[!a-z][^>]*>", re.I)
clean_balises = lambda x: re_clean_balises.sub("", x)

clean_statuses = lambda x: re_alin_sup.sub("", x)

strip_text = lambda x: clean_spaces(clean_statuses(clean_balises(x))).strip()

re_non_alphanum = re.compile(r"[^a-z0-9]+")

upcase_accents = "ÇÀÂÄÉÈÊËÎÏÔÖÙÛÜ"
locase_accents = "çàâäéèêëîïôöùûü"
case_noaccents = "caaaeeeeiioouuu"

def real_lower(text):
    for a in upcase_accents:
        text = text.replace(a, locase_accents[upcase_accents.find(a)])
    return text.lower()

def clean_accents(text):
    text = real_lower(text)
    for a in locase_accents:
        text = text.replace(a, case_noaccents[locase_accents.find(a)])
    return text


re_clean_alin = re.compile(r'^"?(([IVXCDLM]+|\d+|[a-z])[°)\-\.\s]+)+\s*((%s|[A-Z%s]+)[°)\-\.\s]+)*' % (bister, upcase_accents))