TSED is a metric used to evaluate code similarity by analyzing the Abstract Syntax Tree (AST) of code rather than just raw text. It is designed to capture structural similarity, making it more effective for evaluating code generation tasks (like LLM outputs) than traditional statistical metrics like BLEU or Jaccard similarity.
How TSED works:
- Code Analysis: Uses
tree-sitter to convert source code into an AST. - Tree Edit Distance Calculation: Employs the APTED algorithm to calculate the minimum operations required to transform one tree into another.
- Normalization: The resulting distance is normalized to a score between 0 and 1.
Mathematical Formula:
$$\Delta(G_1, G_2) = \min_{ops} \sum w(op_i)$$
$$\text{TSED} = \max{1 - \delta / \text{MaxNodes}(G_1, G_2), 0}$$