examples/text_to_knowledge/ernie-ctm/metric.py
def wordseg_soft_acc(list_a: List[Tuple[str, str]],
                     list_b: List[Tuple[str, str]]) -> float:
    """
    Calculate extra metrics of word-seg

    Args:
        list_a: prediction list
        list_b: real list

    Returns:
        acc: the extra accuracy
    """
    i, j = 0, 0
    acc = 0.0
    a_l, b_l = 0, 0
    while i < len(list_a) and j < len(list_b):
        a_r = a_l + len(list_a[i][0]) - 1
        b_r = b_l + len(list_b[j][0]) - 1
        if a_r < b_l:
            i += 1
            a_l = a_r + 1
            continue
        if b_r < a_l:
            j += 1
            b_l = b_r + 1
            continue
        if a_l == b_l and a_r == b_r:
            acc += 1.0
            a_l, b_l = a_r + 1, b_r + 1
            i, j = i + 1, j + 1
            continue
        if a_l == b_l and a_r < b_r:
            cnt = 0.0
            tmp_a_r = a_r
            for k in range(i + 1, len(list_a)):
                tmp_a_r += len(list_a[k])
                cnt += 1.0
                if tmp_a_r == b_r:
                    acc += cnt
                    i, j = k + 1, j + 1
                    a_l, b_l = tmp_a_r + 1, b_r + 1
                    break
            i += 1
            continue
        if a_l == b_l and a_r > b_r:
            tmp_b_r = b_r
            for k in range(j + 1, len(list_b)):
                tmp_b_r += len(list_b[k])
                if tmp_b_r == a_r:
                    acc += 1.0
                    i, j = i + 1, k + 1
                    a_l, b_l = a_r + 1, tmp_b_r + 1
                break
            j += 1
            continue
        i += 1
    return acc

    Calculate extra metrics of word-seg

    Args:
        list_a: prediction list
        list_b: real list

    Returns:
        acc: the extra accuracy
    
Calculate extra metrics of word-seg
Calculate extra metrics of word - seg
def wordseg_soft_acc(list_a: List[Tuple[str, str]],
                     list_b: List[Tuple[str, str]]) -> float:
    i, j = 0, 0
    acc = 0.0
    a_l, b_l = 0, 0
    while i < len(list_a) and j < len(list_b):
        a_r = a_l + len(list_a[i][0]) - 1
        b_r = b_l + len(list_b[j][0]) - 1
        if a_r < b_l:
            i += 1
            a_l = a_r + 1
            continue
        if b_r < a_l:
            j += 1
            b_l = b_r + 1
            continue
        if a_l == b_l and a_r == b_r:
            acc += 1.0
            a_l, b_l = a_r + 1, b_r + 1
            i, j = i + 1, j + 1
            continue
        if a_l == b_l and a_r < b_r:
            cnt = 0.0
            tmp_a_r = a_r
            for k in range(i + 1, len(list_a)):
                tmp_a_r += len(list_a[k])
                cnt += 1.0
                if tmp_a_r == b_r:
                    acc += cnt
                    i, j = k + 1, j + 1
                    a_l, b_l = tmp_a_r + 1, b_r + 1
                    break
            i += 1
            continue
        if a_l == b_l and a_r > b_r:
            tmp_b_r = b_r
            for k in range(j + 1, len(list_b)):
                tmp_b_r += len(list_b[k])
                if tmp_b_r == a_r:
                    acc += 1.0
                    i, j = i + 1, k + 1
                    a_l, b_l = a_r + 1, tmp_b_r + 1
                break
            j += 1
            continue
        i += 1
    return acc
def wordseg_soft_acc ( list_a : List [ Tuple [ str , str ] ] , list_b : List [ Tuple [ str , str ] ] ) -> float : i , j = 0 , 0 acc = 0.0 a_l , b_l = 0 , 0 while i < len ( list_a ) and j < len ( list_b ) : a_r = a_l + len ( list_a [ i ] [ 0 ] ) - 1 b_r = b_l + len ( list_b [ j ] [ 0 ] ) - 1 if a_r < b_l : i += 1 a_l = a_r + 1 continue if b_r < a_l : j += 1 b_l = b_r + 1 continue if a_l == b_l and a_r == b_r : acc += 1.0 a_l , b_l = a_r + 1 , b_r + 1 i , j = i + 1 , j + 1 continue if a_l == b_l and a_r < b_r : cnt = 0.0 tmp_a_r = a_r for k in range ( i + 1 , len ( list_a ) ) : tmp_a_r += len ( list_a [ k ] ) cnt += 1.0 if tmp_a_r == b_r : acc += cnt i , j = k + 1 , j + 1 a_l , b_l = tmp_a_r + 1 , b_r + 1 break i += 1 continue if a_l == b_l and a_r > b_r : tmp_b_r = b_r for k in range ( j + 1 , len ( list_b ) ) : tmp_b_r += len ( list_b [ k ] ) if tmp_b_r == a_r : acc += 1.0 i , j = i + 1 , k + 1 a_l , b_l = a_r + 1 , tmp_b_r + 1 break j += 1 continue i += 1 return acc
Calculate extra metrics of word-seg
Calculate extra metrics of word - seg
"""
    Calculate extra metrics of word-seg

    Args:
        list_a: prediction list
        list_b: real list

    Returns:
        acc: the extra accuracy
    """
the extra accuracy
the extra accuracy
acc
list_a
List[Tuple[str, str]]
None
list_b
List[Tuple[str, str]]
None