Initialize a SemHash Instance
To use SemHash for deduplication, you first need to initialize aSemHash
instance with your dataset.
This will build an index, which can then be used for fast deduplication.
You can set several parameters here, such as the model to use.
The default model is minishlab/potion-base-8M
, which is a lightweight model that works well for most English text datasets.
For multilingual datasets, you can use minishlab/potion-multilingual-128M
, which is optimized for multilingual data.
Note that you can also use your own custom model, or any SentenceTransformer model.
Parameters
Parameters
A list of records (strings or dictionaries).
Columns to featurize if records are dictionaries.
Whether to use approximate nearest neighbors (True) or basic search (False).
Optional Encoder model. If
None
, uses the default (minishlab/potion-base-8M).Deduplicate a Single Dataset
To deduplicate a single dataset, you can use theself_deduplicate
method.
This will remove semantic duplicates from the dataset.
Parameters
Parameters
Similarity threshold for deduplication.
Deduplicate Across Multiple Datasets
To deduplicate across multiple datasets, you can use thededuplicate
method.
This allows you to remove duplicates from one dataset against another dataset, which is useful for ensuring that your test set does not overlap with your training set.
Parameters
Parameters
Deduplicate a Multi-Column Dataset
If you have a multi-column dataset, you can deduplicate it by specifying the columns to use for deduplication. For example, if you have a question-answering dataset withquestion
and context
columns, you can deduplicate based on both columns.
This will filter out records that have similar questions and contexts, ensuring that you do not have redundant entries in your dataset.
This is useful for datasets like SQuAD, where you can have the same question asked with different contexts, and you want to ensure that each question-context pair is unique.
DeduplicationResult Functionality
TheDeduplicationResult
object returned by the deduplication methods provides several useful attributes that help you understand the deduplication process:
selected
: The deduplicated records.filtered
: The records that were removed as duplicates. This is returned as a list ofDeduplicationResult
, each consisting of:record
: The original record that was removed.exact
: Whether the record was an exact duplicate.duplicates
: A list of records that were considered duplicates of the removed record, along with their similarity score.
duplicate_ratio
: The ratio of duplicates found in the dataset.exact_duplicate_ratio
: The ratio of exact duplicates found in the dataset.
get_least_similar_from_duplicates(n)
: Returns the least similar record from the duplicates. This allows you to easily find the right deduplcation threhsold.rethreshold(threshold)
: Re-applies the deduplication with a new threshold, allowing you to adjust the sensitivity of the deduplication process without rebuilding the SemHas index.