|
| 1 | +from promptflow.core import tool |
| 2 | + |
| 3 | + |
| 4 | +def string_to_number(raw_string: str) -> float: |
| 5 | + """Try to parse the prediction string and groundtruth string to float number. |
| 6 | + Support parse int, float, fraction and recognize non-numeric string with wrong format. |
| 7 | + Wrong format cases: 'the answer is \box{2/3}', '0, 5, or any number greater than 11', '4/7//9' |
| 8 | + """ |
| 9 | + float_number = 0.0 |
| 10 | + try: |
| 11 | + float_number = float(raw_string) |
| 12 | + except Exception: |
| 13 | + if "/" in raw_string: |
| 14 | + split_list = raw_string.split("/") |
| 15 | + if len(split_list) == 2: |
| 16 | + numerator, denominator = split_list |
| 17 | + try: |
| 18 | + float_number = float(numerator) / float(denominator) |
| 19 | + except Exception: |
| 20 | + return None |
| 21 | + else: |
| 22 | + return None |
| 23 | + else: |
| 24 | + return None |
| 25 | + return float_number |
| 26 | + |
| 27 | + |
| 28 | +@tool |
| 29 | +def line_process(groundtruth: str, prediction: str) -> int: |
| 30 | + pred_float = string_to_number(prediction) |
| 31 | + """Early stop""" |
| 32 | + if pred_float is None: |
| 33 | + return -1 |
| 34 | + gt_float = string_to_number(groundtruth) |
| 35 | + if gt_float is None: |
| 36 | + return -1 |
| 37 | + """ both pred_float and gt_float are valid""" |
| 38 | + if round(pred_float, 10) == round(gt_float, 10): |
| 39 | + return 1 |
| 40 | + else: |
| 41 | + return -1 |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + processed_result = line_process("3/5", "6/10") |
| 46 | + print("The processed result is", processed_result) |
| 47 | + |
| 48 | + processed_result = line_process("1/2", "0.5") |
| 49 | + print("The processed result is", processed_result) |
| 50 | + |
| 51 | + processed_result = line_process("3", "5") |
| 52 | + print("The processed result is", processed_result) |
| 53 | + |
| 54 | + processed_result = line_process("2/3", "the answer is \box{2/3}") |
| 55 | + print("The processed result is", processed_result) |
0 commit comments