code: def training_data_dedupe(
      data, common_key, training_size=50000
  ) -> TrainingData:  # pragma: nocover
      """
      Construct training data for consumption by the func:`mark_pairs`
      method from an already deduplicated dataset.
  
      Args:
  
          data: Dictionary of records where the keys are record_ids and
                the values are dictionaries with the keys being field names
          common_key: The name of the record field that uniquely identifies
                      a match
          training_size: the rough limit of the number of training examples,
                         defaults to 50000
  
      .. note::
  
           Every match must be identified by the sharing of a common key.
           This function assumes that if two records do not share a common key
           then they are distinct records.
      """
  
      identified_records: dict[str, list[RecordID]]
      identified_records = collections.defaultdict(list)
      matched_pairs: set[tuple[RecordID, RecordID]] = set()
      distinct_pairs: set[tuple[RecordID, RecordID]] = set()
      unique_record_ids: set[RecordID] = set()
  
      # a list of record_ids associated with each common_key
      for record_id, record in data.items():
          unique_record_ids.add(record_id)
          identified_records[record[common_key]].append(record_id)
  
      # all combinations of matched_pairs from each common_key group
      for record_ids in identified_records.values():
          if len(record_ids) > 1:
              matched_pairs.update(
                  itertools.combinations(sorted(record_ids), 2)
              )
  
      # calculate indices using dedupe.core.randomPairs to avoid
      # the memory cost of enumerating all possible pairs
      unique_record_ids_l = list(unique_record_ids)
      pair_indices = randomPairs(len(unique_record_ids), training_size)
      distinct_pairs = set()
      for i, j in pair_indices:
          distinct_pairs.add(
              (unique_record_ids_l[i], unique_record_ids_l[j])
          )
  
      distinct_pairs -= matched_pairs
  
      matched_records = [
          (data[key_1], data[key_2]) for key_1, key_2 in matched_pairs
      ]
  
      distinct_records = [
          (data[key_1], data[key_2]) for key_1, key_2 in distinct_pairs
      ]
  
      training_pairs: TrainingData
      training_pairs = {
          "match": matched_records,
          "distinct": distinct_records,
      }
  
      return training_pairs
  
imports: from __future__ import annotations
  import collections
  import itertools
  import random
  import sys
  import warnings
  from typing import Iterator, Tuple, overload
  import numpy
  import dedupe
  from dedupe._typing import DataInt, DataStr, Literal, RecordDict, RecordDictPair, RecordID, TrainingData
  from dedupe.canonical import getCanonicalRep
  from dedupe.core import unique
label_desc: Use image node_id 12 for calling a global function with example usage: training_data_dedupe(data, common_key, training_size) and returns: training_pairs