Spaces:
Sleeping
Sleeping
""" | |
Модуль содержит класс для представления формул в документе. | |
""" | |
from dataclasses import dataclass | |
from typing import Any, Callable | |
from .parsed_structure import DocumentElement | |
class ParsedFormula(DocumentElement): | |
""" | |
Формула из документа. | |
""" | |
title: str | None = None | |
latex: str = "" | |
# Номер формулы в документе | |
formula_number: str | None = None | |
# Дополнительное описание/пояснение формулы | |
description: str | None = None | |
def to_string(self) -> str: | |
""" | |
Преобразует формулу в строковое представление. | |
Returns: | |
str: Строковое представление формулы. | |
""" | |
title_str = f"{self.title}: " if self.title else "" | |
return f"{title_str}Формула: {self.latex}" | |
def apply(self, func: Callable[[str], str]) -> None: | |
""" | |
Применяет функцию к текстовым элементам формулы. | |
Args: | |
func (Callable[[str], str]): Функция для применения к текстовым элементам. | |
""" | |
if self.title: | |
self.title = func(self.title) | |
self.latex = func(self.latex) | |
if self.description: | |
self.description = func(self.description) | |
def to_dict(self) -> dict[str, Any]: | |
""" | |
Преобразует формулу в словарь. | |
Returns: | |
dict[str, Any]: Словарное представление формулы. | |
""" | |
result = { | |
'title': self.title, | |
'latex': self.latex, | |
'formula_number': self.formula_number, | |
'description': self.description, | |
'page_number': self.page_number, | |
'index_in_document': self.index_in_document, | |
'referenced_element_index': self.referenced_element_index | |
} | |
return result | |