write a snippet of code which deduplicates documents (list of texts) and only returns…
- User
write a snippet of code which deduplicates documents (list of texts) and only returns the texts without duplicates using python
- Assistantpython
def deduplicate_documents(documents): """Deduplicates a list of text documents. Args: documents: A list of strings, where each string represents a document. Returns: A list of unique documents, preserving the order of the first occurrence. """ seen_documents = set() unique_documents = [] for document in documents: if document not in seen_documents: seen_documents.add(document) unique_documents.append(document) return unique_documents # Example usage: documents = [ "This is the first document.", "This is the second document.", "This is the first document.", "This is the third document." ] unique_documents = deduplicate_documents(documents) print(unique_documents)Explanation:
-
deduplicate_documents(documents)function:- Takes a list of
documentsas input. - Initializes an empty
setcalledseen_documentsto keep track of unique documents encountered. - Initializes an empty list called
unique_documentsto store the deduplicated documents.
- Takes a list of
-
Iterating through documents:
- The code iterates through each
documentin the inputdocumentslist.
- The code iterates through each
-
Checking for duplicates:
- For each
document, it checks if the document is already present in theseen_documentsset. - If the document is not in the set (meaning it's unique), the following happens:
- The document is added to the
seen_documentsset to mark it as seen. - The document is appended to the
unique_documentslist.
- The document is added to the
- For each
-
Returning unique documents:
-
- User
can I use minhash for that, for near deduplication
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.