Paragraphs#
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.
- class contextgem.public.paragraphs.Paragraph(**data)[source]#
Bases:
_ParasAndSentsBase
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.
- Variables:
raw_text – The complete text content of the paragraph. This value is frozen after initialization.
sentences – The individual sentences contained within the paragraph. Defaults to an empty list. Cannot be reassigned once populated.
- Parameters:
- 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.- Example:
- Paragraph definition#
from contextgem import Paragraph # Create a paragraph with raw text content contract_paragraph = Paragraph( raw_text=( "This agreement is effective as of January 1, 2025. " "All parties must comply with the terms outlined herein. " "Failure to adhere to these terms may result in termination of the agreement." ) )
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- raw_text: NonEmptyStr#
- sentences: list[Sentence]#
- clone()#
Creates and returns a deep copy of the current instance.
- Return type:
typing.Self
- Returns:
A deep copy of the current instance.
- classmethod from_dict(obj_dict)#
Reconstructs an instance of the class from a dictionary representation.
This method deserializes a dictionary containing the object’s attributes and values into a new instance of the class. It handles complex nested structures like aspects, concepts, and extracted items, properly reconstructing each component.
- classmethod from_disk(file_path)#
Loads an instance of the class from a JSON file stored on disk.
This method reads the JSON content from the specified file path and deserializes it into an instance of the class using the from_json method.
- Parameters:
file_path (str) – Path to the JSON file to load (must end with ‘.json’).
- Returns:
An instance of the class populated with the data from the file.
- Return type:
Self
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
OSError – If there’s an error reading the file.
RuntimeError – If deserialization fails.
- classmethod from_json(json_string)#
Creates an instance of the class from a JSON string representation.
This method deserializes the provided JSON string into a dictionary and uses the from_dict method to construct the class instance. It validates that the class name in the serialized data matches the current class.
- to_dict()#
Transforms the current object into a dictionary representation.
Converts the object to a dictionary that includes: - All public attributes - Special handling for specific public and private attributes
When an LLM or LLM group is serialized, its API credentials and usage/cost stats are removed.
- to_disk(file_path)#
Saves the serialized instance to a JSON file at the specified path.
This method converts the instance to a dictionary representation using to_dict(), then writes it to disk as a formatted JSON file with UTF-8 encoding.
- Parameters:
file_path (str) – Path where the JSON file should be saved (must end with ‘.json’).
- Return type:
- Returns:
None
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
IOError – If there’s an error during the file writing process.
- to_json()#
Converts the object to its JSON string representation.
Serializes the object into a JSON-formatted string using the dictionary representation provided by the to_dict() method.
- Returns:
A JSON string representation of the object.
- Return type:
- additional_context: Optional[NonEmptyStr]#
- custom_data: dict#