Skip to main content

SCE: Structural Cross Entropy for Code Similarity

Compute Structural Cross Entropy (SCE) and Jensen–Shannon-divergence-based similarity between two code snippets, using Tree-sitter abstract syntax trees.

Each snippet is parsed into an AST, and subtrees (a node type plus its child types, optionally with leaf token values) are counted up to max_depth. The two subtree frequency distributions are then compared with cross entropy or Jensen–Shannon divergence.


Installation

Available on PyPI:

pip install code-sce

Requires Python 3.9+.


Quick Start

from code_sce import (
    compute_cte_jsd_struct,
    compute_cte_jsd_value,
    compute_sce_norm_struct,
    compute_sce_norm_value,
)

code1 = """
if x >= 0:
    sign = "non-negative"
else:
    sign = "negative"
print(sign)
"""

code2 = """
if x >= 0:
    sign = "non-negative"
    print(sign)
else:
    sign = "negative"
    print(sign)
"""

lang = "python"
max_depth = 10

print(f"JSD Struct:      {compute_cte_jsd_struct(lang, code1, code2, max_depth):.6f}")
print(f"SCE-Norm Struct: {compute_sce_norm_struct(lang, code1, code2, max_depth):.6f}")
print(f"JSD Value:       {compute_cte_jsd_value(lang, code1, code2, max_depth):.6f}")
print(f"SCE-Norm Value:  {compute_sce_norm_value(lang, code1, code2, max_depth):.6f}")

Output:

JSD Struct:      0.933744
SCE-Norm Struct: 0.607837
JSD Value:       0.932699
SCE-Norm Value:  0.626398

Metrics

Function Compares Score
compute_cte_jsd_struct AST structure (node types) 1 - JSD
compute_sce_norm_struct AST structure (node types) normalised SCE, H(Q) / H(P, Q)
compute_cte_jsd_value AST structure + leaf token values 1 - JSD
compute_sce_norm_value AST structure + leaf token values normalised SCE, H(Q) / H(P, Q)

All four have the signature metric(lang, code_a, code_b, max_depth=30, eps=1e-10) and return 1.0 for identical code. The *_struct variants ignore identifier names and literals; the *_value variants take them into account.

Configuration Options

  • lang: a language name supported by tree-sitter-language-pack (e.g. "python", "sql", "javascript", "java").
  • max_depth: how deep the AST traversal goes (root is depth 1). Increase for more detailed subtree extraction; decrease to speed up comparisons.
  • eps: smoothing constant (default 1e-10) that prevents zero-probability issues when computing entropy/divergence.

Notes

  • The JSD-based functions compute

    JS Divergence = 0.5 * [KL(P || M) + KL(Q || M)],  where M = (P + Q)/2
    

    and return 1 - JSD as a similarity score.

  • If a divergence calculation yields NaN, the functions print a warning and use JSD = 1.0 (similarity 0.0).


Development

git clone https://github.com/Etamin/SCE
cd SCE
pip install -e ".[test]"
pytest

Release files for code-sce 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for code-sce 0.1.0
File Size Uploaded
code_sce-0.1.0.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for code-sce 0.1.0
File Interpreter ABI Platform
code_sce-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / code_sce-0.1.0.tar.gz

Download URL code_sce-0.1.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8883e570a1e77dc7c07048819a9381f294981541ade29a49f37d3a4e2f58f5a9
BLAKE2b-256 checksum
How to use checksums
8d06a8d0fe80345773e426c779d8f1edb9a81d4d65b98e379171b0f928a7261e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / code_sce-0.1.0-py3-none-any.whl

Download URL code_sce-0.1.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8873e0e75ad564ad29e154242336d68c01389e1615116f7e31c43a852c5293b
BLAKE2b-256 checksum
How to use checksums
b634cda583afd8241e4cbc22487a62b001dc3b4b7b0cab2bbdec5f9cc75e3487
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page