Source code for contextgem.public.paragraphs

# SPDX-FileCopyrightText: Shcherbak AI AS
# SPDX-License-Identifier: Apache-2.0

"""
Module for handling document paragraphs.

This module provides the Paragraph class, which represents a structured segment of text
within a document. Paragraphs serve as containers for sentences and maintain the raw text
content of the segment they represent.

The module supports validation to ensure data integrity and provides mechanisms to prevent
inconsistencies during document analysis by restricting certain attribute modifications
after initial assignment.
"""

from __future__ import annotations

from contextgem.internal.base.paras_and_sents import _Paragraph
from contextgem.internal.decorators import _expose_in_registry


[docs] @_expose_in_registry(additional_key=_Paragraph) class Paragraph(_Paragraph): """ Represents a paragraph of a document with its raw text content and constituent sentences. Paragraphs are immutable text segments that can contain multiple sentences. Once sentences are assigned to a paragraph, they cannot be changed to maintain data integrity during analysis. :ivar raw_text: The complete text content of the paragraph. This value is frozen after initialization. :vartype raw_text: str :ivar sentences: The individual sentences contained within the paragraph. Defaults to an empty list. Cannot be reassigned once populated. :vartype sentences: list[Sentence] :ivar additional_context: Optional supplementary information about the paragraph, as a single-line string, e.g. its structural role, formatting, or position in the document ("Heading, level 2" or "Table: Fees, row 2, column 3"). It is passed to the LLM together with the paragraph text in the prompts that list paragraphs, i.e. when aspects are extracted, or when concepts are extracted with references. A decision model is sent it together with the paragraph text when aspects are extracted. Sentences segmented from the paragraph inherit it. Defaults to None. :vartype additional_context: str | None Note: Normally, you do not need to construct paragraphs manually, as they are populated automatically from document's ``raw_text`` attribute. Only use this constructor for advanced use cases, such as when you have a custom paragraph segmentation tool, or when you want to provide additional context for the paragraphs. Example: .. literalinclude:: ../../../dev/usage_examples/docstrings/paragraphs/def_paragraph.py :language: python :caption: Paragraph definition """