TensorTonic
Study Plans
Study Plan
Perform Stable Exact Deduplication
Language ModelsData Processing
Easy

Deduplicate documents while retaining the earliest occurrence of each normalized text. Apply Unicode NFKC normalization, then Unicode case folding when lowercase is true. When collapse_whitespace is true, also trim the text and replace each Unicode whitespace run with one ASCII space.

Bucket keys are the low-order hash_bits bits of the normalized text's SHA-256 digest, using UTF-8 encoding and a big-endian unsigned digest integer. Hash collisions do not imply equality: different normalized texts must remain separate. Document IDs are not normalized.

Return a Python dictionary with retained_ids, a list of retained string IDs in input order, and removed_to_retained, a dictionary mapping each removed string ID to its earliest retained matching ID.

Loading visualization...

Examples

Input: documents = [{"id":"a","text":" Hello World "},{"id":"b","text":"hello world"},{"id":"c","text":"HELLO WORLD!"}], lowercase = true, collapse_whitespace = true, hash_bits = 64

Output: {"retained_ids":["a","c"],"removed_to_retained":{"b":"a"}}

Explanation: The first two texts normalize identically, while punctuation keeps the third text distinct.

Input: documents = [{"id":"x","text":"Straße"},{"id":"y","text":"STRASSE"}], lowercase = true, collapse_whitespace = true, hash_bits = 64

Output: {"retained_ids":["x"],"removed_to_retained":{"y":"x"}}

Input: documents = [{"id":"a","text":"a"},{"id":"b","text":"b"}], lowercase = false, collapse_whitespace = false, hash_bits = 1

Output: {"retained_ids":["a","b"],"removed_to_retained":{}}

Hint 1

unicodedata.normalize supports Unicode compatibility normalization.

Hint 2

str.casefold handles Unicode caseless matching.

Hint 3

hashlib.sha256 accepts encoded bytes.

Requirements

  • Use Python strings and standard-library normalization and hashing; NumPy is not required.
  • Retention is determined by exact normalized-text equality, even when hashes collide.
  • Do not modify document records or their order.

Constraints

  • documents is a list of dictionaries with unique nonempty string id values and string text values.
  • lowercase and collapse_whitespace are Python Booleans.
  • hash_bits is a Python integer from 1 through 256.
  • Empty and whitespace-only text are valid.
Try Similar Problems
Compute Temperature-Based Data MixturesMediumBuild Deterministic MinHash SignaturesMediumCanonicalize Pairwise Preference DataMediumOrthogonalize a Muon Momentum UpdateEasyPatchify a Batch of ImagesEasyMixed-Precision Training Memory AccountantEasyBuild an Assistant-Only SFT Loss MaskEasyGradient Accumulation EquivalenceEasyNormalized DifferenceEasyColumn SelectionEasyMissing Value ImputationEasyTensor Shape ManipulationEasy

Accepts: array

Accepts: number

You must run your code first.

Perform Stable Exact Deduplication

Language ModelsData Processing
Easy

Deduplicate documents while retaining the earliest occurrence of each normalized text. Apply Unicode NFKC normalization, then Unicode case folding when lowercase is true. When collapse_whitespace is true, also trim the text and replace each Unicode whitespace run with one ASCII space.

Bucket keys are the low-order hash_bits bits of the normalized text's SHA-256 digest, using UTF-8 encoding and a big-endian unsigned digest integer. Hash collisions do not imply equality: different normalized texts must remain separate. Document IDs are not normalized.

Return a Python dictionary with retained_ids, a list of retained string IDs in input order, and removed_to_retained, a dictionary mapping each removed string ID to its earliest retained matching ID.

Loading visualization...

Examples

Input: documents = [{"id":"a","text":" Hello World "},{"id":"b","text":"hello world"},{"id":"c","text":"HELLO WORLD!"}], lowercase = true, collapse_whitespace = true, hash_bits = 64

Output: {"retained_ids":["a","c"],"removed_to_retained":{"b":"a"}}

Explanation: The first two texts normalize identically, while punctuation keeps the third text distinct.

Input: documents = [{"id":"x","text":"Straße"},{"id":"y","text":"STRASSE"}], lowercase = true, collapse_whitespace = true, hash_bits = 64

Output: {"retained_ids":["x"],"removed_to_retained":{"y":"x"}}

Input: documents = [{"id":"a","text":"a"},{"id":"b","text":"b"}], lowercase = false, collapse_whitespace = false, hash_bits = 1

Output: {"retained_ids":["a","b"],"removed_to_retained":{}}

Hint 1

unicodedata.normalize supports Unicode compatibility normalization.

Hint 2

str.casefold handles Unicode caseless matching.

Hint 3

hashlib.sha256 accepts encoded bytes.

Requirements

  • Use Python strings and standard-library normalization and hashing; NumPy is not required.
  • Retention is determined by exact normalized-text equality, even when hashes collide.
  • Do not modify document records or their order.

Constraints

  • documents is a list of dictionaries with unique nonempty string id values and string text values.
  • lowercase and collapse_whitespace are Python Booleans.
  • hash_bits is a Python integer from 1 through 256.
  • Empty and whitespace-only text are valid.
Try Similar Problems
Compute Temperature-Based Data MixturesMediumBuild Deterministic MinHash SignaturesMediumCanonicalize Pairwise Preference DataMediumOrthogonalize a Muon Momentum UpdateEasyPatchify a Batch of ImagesEasyMixed-Precision Training Memory AccountantEasyBuild an Assistant-Only SFT Loss MaskEasyGradient Accumulation EquivalenceEasyNormalized DifferenceEasyColumn SelectionEasyMissing Value ImputationEasyTensor Shape ManipulationEasy

Accepts: array

Accepts: number

You must run your code first.