Files
wehub-resource-sync e768098d0e
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:52 +08:00

56 lines
1.8 KiB
Python

from promptflow.core import tool
def string_to_number(raw_string: str) -> float:
''' Try to parse the prediction string and groundtruth string to float number.
Support parse int, float, fraction and recognize non-numeric string with wrong format.
Wrong format cases: 'the answer is \box{2/3}', '0, 5, or any number greater than 11', '4/7//9'
'''
float_number = 0.0
try:
float_number = float(raw_string)
except Exception:
if '/' in raw_string:
split_list = raw_string.split('/')
if len(split_list) == 2:
numerator, denominator = split_list
try:
float_number = float(numerator) / float(denominator)
except Exception:
return None
else:
return None
else:
return None
return float_number
@tool
def line_process(groundtruth: str, prediction: str) -> int:
pred_float = string_to_number(prediction)
'''Early stop'''
if (pred_float is None):
return -1
gt_float = string_to_number(groundtruth)
if (gt_float is None):
return -1
''' both pred_float and gt_float are valid'''
if round(pred_float, 10) == round(gt_float, 10):
return 1
else:
return -1
if __name__ == "__main__":
processed_result = line_process("3/5", "6/10")
print("The processed result is", processed_result)
processed_result = line_process("1/2", "0.5")
print("The processed result is", processed_result)
processed_result = line_process("3", "5")
print("The processed result is", processed_result)
processed_result = line_process("2/3", "the answer is \box{2/3}")
print("The processed result is", processed_result)