medkit.text.preprocessing.duplicate_finder
==========================================

.. py:module:: medkit.text.preprocessing.duplicate_finder


Classes
-------

.. autoapisummary::

   medkit.text.preprocessing.duplicate_finder.DuplicationAttribute
   medkit.text.preprocessing.duplicate_finder.DuplicateFinder


Module Contents
---------------

.. py:class:: DuplicationAttribute(value: bool, source_doc_id: str | None = None, source_spans: list[medkit.core.text.AnySpan] | None = None, source_doc_date: Any | None = None, metadata: dict[str, Any] | None = None, uid: str | None = None)

   Bases: :py:obj:`medkit.core.Attribute`


   
   Attribute indicating if some text is a duplicate of some other text in another document.














   :Attributes:

       **uid** : str
           Identifier of the attribute

       **label** : str
           The attribute label, always set to :attr:`DuplicationAttribute.LABEL`

       **value** : Any, optional
           `True` if the segment or entity to which the attribute belongs is a
           duplicate of the part of another document, `False` otherwise.

       **source_doc_id** : str, optional
           Identifier of the document from which the text was copied

       **source_spans** : list of AnySpan, optional
           Spans of the duplicated text in the source document

       **source_doc_date** : Any, optional
           Date of the source document, if known


   ..
       !! processed by numpydoc !!

   .. py:attribute:: source_doc_id
      :type:  str | None


   .. py:attribute:: source_spans
      :type:  list[medkit.core.text.AnySpan] | None


   .. py:attribute:: source_doc_date
      :type:  Any | None


   .. py:attribute:: LABEL
      :type:  ClassVar[str]
      :value: 'is_duplicate'


      
      Label used for all TNM attributes
















      ..
          !! processed by numpydoc !!


   .. py:method:: to_dict() -> dict[str, Any]


   .. py:method:: from_dict(attr_dict: dict[str, Any]) -> typing_extensions.Self
      :classmethod:


      
      Create an Attribute from a dict.


      :Parameters:

          **attribute_dict: dict of str to Any**
              A dictionary from a serialized Attribute as generated by to_dict()














      ..
          !! processed by numpydoc !!


.. py:class:: DuplicateFinder(output_label: str, segments_to_output: typing_extensions.Literal[dup, nondup, both] = 'dup', min_duplicate_length: int = 5, fingerprint_type: typing_extensions.Literal[char, word] = 'word', fingerprint_length: int = 2, date_metadata_key: str | None = None, case_sensitive: bool = True, allow_multiline: bool = True, orf: int = 1)

   Bases: :py:obj:`medkit.core.Operation`


   
   Detect duplicated chunks of text across a collection of text documents.

   When a duplicated chunk of text is found, a segment is created on the newest
   document covering the span that is duplicated. A
   :class:`~.DuplicationAttribute` having `"is_duplicate"` as label and `True`
   as value is attached to the segment. It can later be propagated to the
   entities created from the duplicate segments.

   The attribute also holds the id of the source document from which the text
   was copied, the spans of the text in the source document, and optionally the
   date of the source document if provided.

   Optionally, segments can also be created for non-duplicate zones to make it
   easier to process only those parts of the documents. For these segments, the
   attribute value is `False` and the source, spans and date fields are `None`.

   NB: better performance may be achieved by installing the `ncls` python
   package, which will then be used by `duptextfinder` library.

   :Parameters:

       **output_label** : str
           Label of created segments

       **segments_to_output** : str, default="dup"
           Type of segments to create: only duplicate segments (`"dup"`), only
           non-duplicate segments (`"nondup"`), or both (`"both"`)

       **min_duplicate_length** : int, default=5
           Minimum length of duplicated segments, in characters (shorter
           segments will be discarded)

       **fingerprint_type** : str, default="word"
           Base unit to use for fingerprinting (either `"char"` or `"word"`)

       **fingerprint_length** : int, default=2
           Number of chars or words in each fingerprint. If `fingerprint_type`
           is set to `"char"`, this should be the same value as
           `min_duplicate_length`. If `fingerprint_type` is set to `"word"`,
           this should be around the average word size multiplied by
           `min_duplicate_length`

       **date_metadata_key** : str, optional
           Key to use to retrieve the date of each document from their metadata
           dicts. When provided, this is used to determine which document
           should be the source of a duplicate (the older) and which document
           should be the recipient (the newer). If None, the order of the
           documents in the collection will be used.

       **case_sensitive** : bool, default=True
           Whether duplication detection should be case-sensitive or not

       **allow_multiline** : bool, default=True
           Whether detected duplicates can span across multiline lines, or
           each line should be handled separately

       **orf** : int, default=1
           Step size when building fingerprints, cf the `duptextfinder`
           documentation














   ..
       !! processed by numpydoc !!

   .. py:attribute:: _NON_EMPTY_REGEXP


   .. py:method:: run(collections: list[medkit.core.Collection])

      
      Find duplicates in each collection of documents.

      For each duplicate found, a :class:`~.core.text.Segment` object with a
      :class:`~.DuplicationAttribute` will be created and attached to the document that
      is the recipient of the duplication (ie not the source document).















      ..
          !! processed by numpydoc !!


   .. py:method:: _find_duplicate_in_docs(docs: list[medkit.core.text.TextDocument])

      
      Find duplicates among a set of documents.
















      ..
          !! processed by numpydoc !!


   .. py:method:: _find_duplicates_in_doc(doc: medkit.core.text.TextDocument, duplicate_finder: duptextfinder.DuplicateFinder, docs_by_id: dict[str, medkit.core.text.TextDocument])

      
      Find duplicates between a document and previously processed documents.


      :Parameters:

          **doc** : TextDocument
              Document in which to look for duplicates

          **duplicate_finder** : DuplicateFinder
              Duplicate finder to use, that has already processed previous documents if any

          **docs_by_id** : dict of str to TextDocument
              Previously processed documents, by id














      ..
          !! processed by numpydoc !!


   .. py:method:: _create_nondup_segment(target_segment, range_)

      
      Create a segment representing a non-duplicated zone.
















      ..
          !! processed by numpydoc !!


   .. py:method:: _create_duplicate_segment(target_segment, target_range, source_doc, source_range)

      
      Create a segment representing a duplicated zone.
















      ..
          !! processed by numpydoc !!


