def compare_gramians()

in src/evaluator.py [0:0]


def compare_gramians(env, tgt, hyp, tolerance, norm1=False):
    nr_lines = tgt.count(env.line_separator)
    nr_cols = tgt.count(env.list_separator)
    nr_cols = nr_cols // nr_lines
    # read hypothesis
    h = hyp
    h_gramian = np.zeros((nr_lines, nr_cols), dtype=float)
    for i in range(nr_lines):
        for j in range(nr_cols):
            val, pos = env.parse_float(h)
            if np.isnan(val):
                return False
            if len(h) <= pos or h[pos] != env.list_separator:
                return False
            h_gramian[i][j] = val
            h = h[pos + 1 :]
        if len(h) == 0 or h[0] != env.line_separator:
            return False
        h = h[1:]
    # read target
    t = tgt
    t_gramian = np.zeros((nr_lines, nr_cols), dtype=float)
    for i in range(nr_lines):
        for j in range(nr_cols):
            val, pos = env.parse_float(t)
            t_gramian[i][j] = val
            t = t[pos + 1 :]
        t = t[1:]
    # compare
    if norm1:
        tot = 0
        nb = 0
        for i in range(nr_lines):
            for j in range(nr_cols):
                if t_gramian[i][j] != h_gramian[i][j]:
                    den = h_gramian[i][j] if t_gramian[i][j] == 0 else t_gramian[i][j]
                    delta = abs((t_gramian[i][j] - h_gramian[i][j]) / den)
                    tot += delta
                    nb += 1

        return tot <= tolerance * nb
    else:
        for i in range(nr_lines):
            for j in range(nr_cols):
                if t_gramian[i][j] != h_gramian[i][j]:
                    den = h_gramian[i][j] if t_gramian[i][j] == 0 else t_gramian[i][j]
                    delta = abs((t_gramian[i][j] - h_gramian[i][j]) / den)
                    if delta > tolerance:
                        return False
    return True