gtamkaleidoscope's picture
Update app/util.py
9797e5d
import json
def compare_completion_and_prediction(completion, prediction, verbose=False):
"""
a function that compares the completion and prediction
separating each string by comma into their respective columns,
then compare each column and return a DataFrame with the results
Args:
completion (_type_): str
prediction (_type_): str
verbose (bool, optional): bool. Defaults to False.
Returns:
_type_: json object with completion, prediction, matches, and num_correct
"""
# if verbose is True, print the completion and prediction strings
if verbose:
print("Completion:", completion, f"type({type(completion)}):")
print("Prediction:", prediction, f"type({type(prediction)}):")
# split completion and prediction strings on comma character
completion = completion.split(',')
prediction = prediction.split(',')
# create a column that counts the number of matches between completion and prediction
matches = [completion[i] == prediction[i] for i in range(len(completion))]
return {
"completion": completion,
"prediction": prediction,
"matches": matches,
"num_correct": sum(matches),
}
def json_to_dict(json_string):
"""function that takes string in the form of json and returns a dictionary"""
return json.loads(json_string)
def join_dicts(dict1, dict2):
"""function that joins two dictionaries into one dictionary
Args:
dict1 (_type_): dict
dict2 (_type_): dict
Returns:
_type_: dict
"""
return {key:[dict1[key], dict2[key]] for key in dict1}