TensorTonic
Study Plans
Study Plan
Build Deterministic MinHash Signatures
Language ModelsData Processing
Medium

Build deterministic MinHash signatures for tokenized documents. A shingle is one consecutive token window, encoded as a JSON array with Unicode preserved and no separator spaces. Repeated shingles count once.

Hash columns are numbered from zero. A shingle's hash in column h is SHA-256 of the UTF-8 text seed:h: followed by its encoded shingle, using decimal seed and column values. Interpret the first four digest bytes as an unsigned big-endian integer. Each signature entry is the smallest hash for that document and column; a document without shingles uses 232−12^{32}-1232−1.

For nonempty shingle sets, estimate similarity by

J^(i,j)=1H∑h=0H−11[si,h=sj,h].\widehat J(i,j)=\frac{1}{H}\sum_{h=0}^{H-1}\mathbf{1}[s_{i,h}=s_{j,h}].J(i,j)=H1​h=0∑H−1​1[si,h​=sj,h​].

Here, i and j index documents, H is the number of hash columns, s is the signature matrix, and the indicator is one when the two entries match. Diagonal entries and pairs of empty shingle sets have similarity 1; empty/nonempty pairs have similarity 0.

Return a Python dictionary with signatures, a uint32 NumPy array of shape (N, H), and similarities, a float64 NumPy array of shape (N, N), where N is the number of documents.

Loading visualization...

Examples

Input: documents = [["a","b","c"],["a","b","c"]], shingle_size = 2, num_hashes = 2, seed = 7

Output: {"signatures":[[1402394988,3748241178],[1402394988,3748241178]],"similarities":[[1,1],[1,1]]}

Explanation: Identical shingle sets choose the same minimum in every hash column.

Input: documents = [["a"],["a","b"]], shingle_size = 2, num_hashes = 2, seed = 3

Output: {"signatures":[[4294967295,4294967295],[936885737,1343921959]],"similarities":[[1,0],[0,1]]}

Input: documents = [["a","b","c"],["a","b","d"]], shingle_size = 2, num_hashes = 2, seed = 11

Output: {"signatures":[[1936632583,3094012498],[473285690,1512557262]],"similarities":[[1,0],[0,1]]}

Hint 1

json.dumps supports compact separators and unescaped Unicode.

Hint 2

int.from_bytes reads a byte sequence in a chosen byte order.

Hint 3

NumPy broadcasting compares signature rows across all columns.

Requirements

  • Use NumPy for the two returned arrays.
  • Preserve document order and the specified deterministic hash encoding.
  • Do not modify the documents.

Constraints

  • documents is a list of token lists, and every token is a string.
  • shingle_size and num_hashes are positive Python integers.
  • seed is a nonnegative Python integer.
  • Documents shorter than shingle_size have empty shingle sets.
Try Similar Problems
Compute Temperature-Based Data MixturesMediumPerform Stable Exact DeduplicationEasyCanonicalize Pairwise Preference DataMediumTrain a BPE Tokenizer From Scratch in PythonMediumApply Ranked BPE MergesMediumParallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumParameter-Matched SwiGLU BlockMediumTile and DiffMediumBox Encode and DecodeMediumDrop DuplicatesMediumFancy IndexingMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Build Deterministic MinHash Signatures

Language ModelsData Processing
Medium

Build deterministic MinHash signatures for tokenized documents. A shingle is one consecutive token window, encoded as a JSON array with Unicode preserved and no separator spaces. Repeated shingles count once.

Hash columns are numbered from zero. A shingle's hash in column h is SHA-256 of the UTF-8 text seed:h: followed by its encoded shingle, using decimal seed and column values. Interpret the first four digest bytes as an unsigned big-endian integer. Each signature entry is the smallest hash for that document and column; a document without shingles uses 232−12^{32}-1232−1.

For nonempty shingle sets, estimate similarity by

J^(i,j)=1H∑h=0H−11[si,h=sj,h].\widehat J(i,j)=\frac{1}{H}\sum_{h=0}^{H-1}\mathbf{1}[s_{i,h}=s_{j,h}].J(i,j)=H1​h=0∑H−1​1[si,h​=sj,h​].

Here, i and j index documents, H is the number of hash columns, s is the signature matrix, and the indicator is one when the two entries match. Diagonal entries and pairs of empty shingle sets have similarity 1; empty/nonempty pairs have similarity 0.

Return a Python dictionary with signatures, a uint32 NumPy array of shape (N, H), and similarities, a float64 NumPy array of shape (N, N), where N is the number of documents.

Loading visualization...

Examples

Input: documents = [["a","b","c"],["a","b","c"]], shingle_size = 2, num_hashes = 2, seed = 7

Output: {"signatures":[[1402394988,3748241178],[1402394988,3748241178]],"similarities":[[1,1],[1,1]]}

Explanation: Identical shingle sets choose the same minimum in every hash column.

Input: documents = [["a"],["a","b"]], shingle_size = 2, num_hashes = 2, seed = 3

Output: {"signatures":[[4294967295,4294967295],[936885737,1343921959]],"similarities":[[1,0],[0,1]]}

Input: documents = [["a","b","c"],["a","b","d"]], shingle_size = 2, num_hashes = 2, seed = 11

Output: {"signatures":[[1936632583,3094012498],[473285690,1512557262]],"similarities":[[1,0],[0,1]]}

Hint 1

json.dumps supports compact separators and unescaped Unicode.

Hint 2

int.from_bytes reads a byte sequence in a chosen byte order.

Hint 3

NumPy broadcasting compares signature rows across all columns.

Requirements

  • Use NumPy for the two returned arrays.
  • Preserve document order and the specified deterministic hash encoding.
  • Do not modify the documents.

Constraints

  • documents is a list of token lists, and every token is a string.
  • shingle_size and num_hashes are positive Python integers.
  • seed is a nonnegative Python integer.
  • Documents shorter than shingle_size have empty shingle sets.
Try Similar Problems
Compute Temperature-Based Data MixturesMediumPerform Stable Exact DeduplicationEasyCanonicalize Pairwise Preference DataMediumTrain a BPE Tokenizer From Scratch in PythonMediumApply Ranked BPE MergesMediumParallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumParameter-Matched SwiGLU BlockMediumTile and DiffMediumBox Encode and DecodeMediumDrop DuplicatesMediumFancy IndexingMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.