def distance(a, b):
    """Compute the distance between a and b.

    This is based on Damerau-Levenshtein distance, but we modify the cost
    of some edits, like insertion or removal of '_', or capitalisation changes.

    """
    d = {}
    la = len(a)
    lb = len(b)
    for i in range(la + 1):
        d[i, 0] = i
    for j in range(1, lb + 1):
        d[0, j] = j
    for i, ca in enumerate(a, start=1):
        for j, cb in enumerate(b, start=1):
            cost = int(ca != cb)
            if ca.lower() == cb.lower():
                subst_cost = 0
            else:
                subst_cost = 1.25 * cost
            insertion_cost = 0.7 if cb == '_' else 1.0
            deletion_cost = 0.7 if ca == '_' else 1.0
            d[i, j] = min(d[i - 1, j] + deletion_cost, d[i, j - 1] +
                insertion_cost, d[i - 1, j - 1] + subst_cost)
            if i > 1 and j > 1 and ca == b[j - 2] and a[i - 2] == cb:
                d[i, j] = min(d[i, j], d[i - 2, j - 2] + cost)
    return d[la, lb]
