THINK FIRST·CODE LATER

← All labs

Did the AI copy licensed code? A similarity scanner

Problem

AI assistants sometimes reproduce licensed code nearly verbatim. Build a simple scanner that compares a generated snippet with known licensed snippets.

Normalization: convert the code to a list of tokens: split on any character that is not a letter, digit or underscore, and keep the non-empty pieces; then replace every token that is not a Java keyword from this list with ID if it starts with a letter or _, or NUM if it starts with a digit:

public private protected static final void int long double boolean char String return if else for while new class this null true false

(So renaming variables does not hide copying.) Compute the set of 4-grams (sequences of 4 consecutive tokens). The similarity of two snippets is the Jaccard index |A ∩ B| / |A ∪ B| of their 4-gram sets (0 if both are empty).

Input:

THRESHOLD t
KNOWN name licence
<code lines>
END
... (more KNOWN blocks)
CANDIDATE
<code lines until end of input>

Output one line per known snippet in input order: name (licence): similarity x.xx, and then Closest: name (x.xx) and either Verdict: LIKELY COPIED from name - apply licence LICENCE or rewrite if the highest similarity is ≥ t, or Verdict: no close match.

Input:

THRESHOLD 0.5
KNOWN gplsort GPL3
static void sort(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[j] < a[i]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } } }
END
KNOWN mitsum MIT
static int sum(int[] v) { int s = 0; for (int x : v) s += x; return s; }
END
CANDIDATE
static void order(int[] nums) { for (int p = 0; p < nums.length; p++) { for (int q = p + 1; q < nums.length; q++) { if (nums[q] < nums[p]) { int tmp = nums[p]; nums[p] = nums[q]; nums[q] = tmp; } } } }

Output:

gplsort (GPL3): similarity 1.00
mitsum (MIT): similarity 0.09
Closest: gplsort (1.00)
Verdict: LIKELY COPIED from gplsort - apply licence GPL3 or rewrite

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.