import unicodedata
import re


def slugify(value, replace={}, keepSpace=True):
    """
    Adapted from django.utils.text.slugify
    https://docs.djangoproject.com/en/3.0/_modules/django/utils/text/#slugify
    """
    replace.update({"[": "(", "]": ")", ":": "_", "()": ""})
    value = str(value)
    value = (
        unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
    )

    for k, v in replace.items():
        value = value.replace(k, v)
    value = re.sub(r"[^\w\s)(_-]", "", value).strip()

    if keepSpace:
        value = re.sub(r"[\s]+", " ", value)
    else:
        value = re.sub(r"[-\s]+", "-", value)
    return value