Expand description
Clone detection via n-gram Jaccard similarity on per-method fingerprint token streams.
Each class node carries a methodFingerprints JSON blob with shape
{"methods": [{"name": str, "tokens": "KW_IF BINOP INVOKE …", "line": int, …}, …]}
where tokens is a space-separated string of normalised token classes
emitted by the extractor. We:
- Build the n-gram set (default
n = 3) of each method’s token stream. - Score every cross-class method pair by Jaccard similarity
|A ∩ B| / |A ∪ B|. Pairs withsim ≥ threshold(default 0.7) are emitted as clone pairs. - Run union-find over the clone-pair graph to collapse pairs into transitive clone families.
Two practical filters mirror the Python reference (analysis/clone_detection.py):
- token-count ratio gate — skip pairs whose token counts differ by more than 2× before computing n-grams (cheap O(1) prune).
- same-class same-name skip — partial-class siblings would otherwise self-match.
See DESIGN.md (Phase 3) at the workspace root.
Structs§
- Clone
Analysis - Whole-graph clone-detection readout.
- Clone
Family - A connected component over the clone-pair graph.
- Clone
Pair - A similarity edge between two methods.
- Method
Fingerprint - One method’s clone-detection record, harvested from a class’s
methodFingerprintsblob and tagged with the owning class.
Functions§
- compute_
clone_ analysis - Run clone detection across every node’s
methodFingerprintsblob. - detect_
clone_ pairs - Detect clone pairs among methods by n-gram Jaccard similarity. O(n²) over the method list — caller is responsible for capping graph size (the Python reference skips graphs above 500 nodes).
- extract_
ngrams - Build the set of n-grams of a space-separated token string.
- group_
clone_ families - Group clone pairs into transitive families via union-find. Returns families sorted by descending member count.
- ngram_
jaccard - Jaccard similarity on n-gram sets of two token strings. Empty-on-both
yields
0.0(matches Python). - parse_
method_ fingerprints - Parse the
{"methods": [...]}fingerprint blob attached to a single class.