Skip to main content

Module clone_detection

Module clone_detection 

Source
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:

  1. Build the n-gram set (default n = 3) of each method’s token stream.
  2. Score every cross-class method pair by Jaccard similarity |A ∩ B| / |A ∪ B|. Pairs with sim ≥ threshold (default 0.7) are emitted as clone pairs.
  3. 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§

CloneAnalysis
Whole-graph clone-detection readout.
CloneFamily
A connected component over the clone-pair graph.
ClonePair
A similarity edge between two methods.
MethodFingerprint
One method’s clone-detection record, harvested from a class’s methodFingerprints blob and tagged with the owning class.

Functions§

compute_clone_analysis
Run clone detection across every node’s methodFingerprints blob.
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.