File size: 1,015 Bytes
57a6d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ast
from thefuzz import fuzz

def compute_ast_distance(original_code: str, mutated_code: str) -> float:
    """

    Computes the string similarity distance between the AST dumps of the original

    and mutated code using thefuzz (Levenshtein based).

    Zero edits = 0 score.

    Targeted (small) edit = high plausibility (closer to 1.0).

    Random / wide corruption = low score.

    """
    try:
        orig_ast = ast.dump(ast.parse(original_code))
        mut_ast = ast.dump(ast.parse(mutated_code))
    except SyntaxError:
        return 0.0

    ratio = fuzz.ratio(orig_ast, mut_ast)
    
    if ratio == 100:
        return 0.0
        
    # Empirical calibration: simple AST mutations typically result in 
    # a fuzz ratio of 85-98%.
    
    if 85 <= ratio < 100:
        return 1.0  # Perfect sweet spot
    elif 50 <= ratio < 85:
        # Linearly decay from 1.0 at 85 down to 0.1 at 50
        return max(0.1, (ratio - 50) / 35.0)
    else:
        return 0.0