File size: 4,225 Bytes
86d1691 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
import numpy as np
def match_int(y_true, y_pred):
y_true_int = int(y_true)
try:
y_pred_int = int(y_pred)
except:
return 0.0
return float(y_true_int == y_pred_int)
def match_str(y_true, y_pred):
y_true_str = str(y_true)
try:
y_pred_str = str(y_pred)
except:
return 0.0
return float(y_true_str == y_pred_str)
def match_list(seq_true, seq_pred, match=match_str):
assert isinstance(seq_true, list)
if len(seq_true) == 0:
return 0.0
try:
seq_pred == list(seq_pred)
except:
return 0.0
if len(seq_true) != len(seq_pred):
return 0.0
arr = np.array([match(st, sp) for st, sp in zip(seq_true, seq_pred)])
return float(arr.all())
def reduce_last_intermediate(y_pred):
y_pred_ret = y_pred.copy()
if len(y_pred['intermediate']) == 0:
return y_pred_ret
if exact_match(y_pred['final'], y_pred['intermediate'][-1]):
y_pred_ret['intermediate'] = y_pred['intermediate'][:-1]
return y_pred_ret
def exact_match(y_true, y_pred):
type_y_true = type(y_true)
if type_y_true == str:
return match_str(y_true, y_pred)
elif type_y_true == int:
return match_int(y_true, y_pred)
elif type_y_true == list:
return match_list(y_true, y_pred)
else:
raise TypeError(f'type of ground truth is {type_y_true} and not supported')
def match_hit(seq_true, seq_pred):
assert type(seq_true) == list
if len(seq_true) == 0:
return np.nan
hit = 0.0
for st, sp in zip(seq_true, seq_pred):
hit += exact_match(st, sp)
return hit
def prefix_match_length(seq_true, seq_pred):
assert type(seq_true) == list
if len(seq_true) == 0:
return 0.0
hit = 0.0
for st, sp in zip(seq_true, seq_pred):
if exact_match(st, sp):
hit += 1
else:
break
return hit
def match_ratio(seq_true, seq_pred):
assert type(seq_true) == list
len_true = len(seq_true)
len_pred = len(seq_pred)
len_max = max(len_true, len_pred)
if len_true == 0:
return 0.0
hit = 0.0
for st, sp in zip(seq_true, seq_pred):
hit += exact_match(st, sp)
return hit / len_max
def prefix_accuracy(seq_true, seq_pred):
assert type(seq_true) == list
len_true = len(seq_true)
len_pred = len(seq_pred)
len_max = max(len_true, len_pred)
if len_true == 0:
return 0.0
hit = 0.0
for st, sp in zip(seq_true, seq_pred):
if exact_match(st, sp):
hit += 1
else:
break
return hit / len_max
if __name__ == '__main__':
assert match_int(5, '5') == 1.0
assert match_int(32, '32') == 1.0
assert match_int('512', 512) == 1.0
assert match_int(234, (234)) == 1.0
assert match_int(5, '55') == 0.0
assert match_int(512, 'a') == 0.0
assert match_int('512', 'a') == 0.0
assert match_int(231, [231]) == 0.0
assert match_int(231, [231, 66]) == 0.0
assert match_int(65, None) == 0.0
assert match_int(73, []) == 0.0
assert match_str('abc', 'abc') == 1.0
assert match_str('3', '3') == 1.0
assert match_str('21', 21) == 1.0
assert match_str('fe a t', 'fe a t') == 1.0
assert match_str('abc', 'ac') == 0.0
assert match_str('fe a t', 'feat') == 0.0
assert match_str('abc', ['abc']) == 0.0
assert match_str('abc', None) == 0.0
assert match_str('abc', []) == 0.0
assert match_list([1], [1]) == 1.0
assert match_list([1, 3, 5], [1, 3, 5]) == 1.0
assert match_list(['abc'], ['abc']) == 1.0
assert match_list(['abc', 'de'], ['abc', 'de']) == 1.0
assert match_list([], []) == 0.0
assert match_list([], [1]) == 0.0
assert match_list([2], [3]) == 0.0
assert match_list([1], [1, 1]) == 0.0
assert match_list([1], 1) == 0.0
assert match_list(['abc', 'de', 'f'], ['abc', 'de']) == 0.0
assert match_list(['abc', 'de'], ['abc', 'de', 'f']) == 0.0
assert match_list(['abc', 'de'], ['de', 'abc']) == 0.0
assert match_list([1], []) == 0.0
assert match_list([1], None) == 0.0
assert match_list(['abc', 'de'], [None, 'de']) == 0.0
print('All tests passed!') |