"""Modul pro odstranovani diakritiky a zvlastnich znaku."""

import unicodedata
from spolecne import ABECEDA

def deaccent(unistr):
    """Vrati text bez akcentu. Pochybna metoda pouzivajici unicodedata."""
    return ''.join(aChar 
                   for aChar in unicodedata.normalize('NFD', unistr) 
                   if not unicodedata.combining(aChar))

def ocesat(text, mezery=True):
    """Odstrani z textu akcenty, zvlastni znaky nahradi mezerami, posloupnost
    mezer jednou mezerou a prevede vsechna pismena na velka."""
    text = deaccent(unicode(text)).upper()
    if mezery:
        pole = [' ']
        last_white = True
        for char in text:
            if char in ABECEDA:
                pole.append(char)
                last_white = False
            elif not last_white:
                pole.append(' ')
                last_white = True
        if not last_white:
            pole.append(' ')
        return ''.join(pole)
    else: # bez mezer
        return ''.join([c for c in text if c in ABECEDA])
