File size: 1,893 Bytes
6a42990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Various utility functions for behavior analysis and action similarity computation.
"""

import textdistance



def next_action_jaccard_similarity(agent, proposed_next_action):
    """
    Computes the Jaccard similarity between the agent's current action and a proposed next action,
    modulo target and type (i.e., similarity will be computed using only the content, provided that the action 
    type and target are the same). If the action type or target is different, the similarity will be 0.

    Jaccard similarity is a measure of similarity between two sets, defined as the size of the intersection 
    divided by the size of the union of the sets.

    Args:
        agent (TinyPerson): The agent whose current action is to be compared.
        proposed_next_action (dict): The proposed next action to be compared against the agent's current action.

    Returns:
        float: The Jaccard similarity score between the agent's current action and the proposed next action.
    """
    # Get the agent's current action
    current_action = agent.last_remembered_action()
    
    if current_action is None:
        return 0.0
    
    # Check if the action type and target are the same
    if ("type" in current_action) and ("type" in proposed_next_action) and ("target" in current_action) and ("target" in proposed_next_action) and \
            (current_action["type"] != proposed_next_action["type"] or current_action["target"] != proposed_next_action["target"]):
        return 0.0
    
    # Compute the Jaccard similarity between the content of the two actions
    current_action_content = current_action["content"]
    proposed_next_action_content = proposed_next_action["content"]

    # using textdistance to compute the Jaccard similarity
    jaccard_similarity = textdistance.jaccard(current_action_content, proposed_next_action_content)

    return jaccard_similarity