File size: 2,075 Bytes
9de5882
73588d1
 
 
 
 
 
 
 
 
 
 
 
 
9de5882
 
 
 
 
 
 
73588d1
9de5882
73588d1
9de5882
 
 
73588d1
9de5882
73588d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"""
    json_string = json_string.replace('\n', '')
    json_string = json_string.replace('\t', '')
    json_string = json_string.replace(' ', '')
    json_string = json_string.replace('"', '')
    json_string = json_string.replace('{', '')
    json_string = json_string.replace('}', '')
    json_string = json_string.replace(':', ',')
    json_string = json_string.split(',')
    return {
        json_string[i]: json_string[i + 1]
        for i in range(0, len(json_string), 2)
    }

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}