# -*- coding: utf-8 -*-
|
#
|
# @author: lyg
|
# @date: 2025-5-12
|
# @version: 1
|
# @description: 文档相关数据类
|
from dataclasses import dataclass
|
import typing
|
|
from knowledgebase.db.doc_db_models import TEntity
|
|
|
@dataclass
|
class ParagraphInfo:
|
"""
|
段落信息
|
|
属性:
|
text: str - 段落文本
|
title_level: int - 段落级别,1-9级标题,0表示正文
|
title_num: str - 标题编号,如1.1、1.1.1等,列表编号,如(1)、(2)
|
num_level: int - 列表序号级别,0表示正文
|
num: int - 列表序号,如果是列表
|
children: typing.List[ParagraphInfo] - 子段落列表
|
refs: 引用文档
|
"""
|
text: str
|
title_level: int
|
title_num: str
|
num_level: int
|
num: int
|
children: typing.List
|
refs: typing.List
|
entities: typing.List[TEntity]
|
|
@property
|
def full_text(self):
|
"""
|
获取段落完整文本,包含标题编号
|
:return: str - 段落完整文本
|
"""
|
if self.title_num:
|
return f"{self.title_num}. {self.text}"
|
else:
|
return f"{self.text}"
|
|
@property
|
def full_text_with_children(self):
|
"""
|
获取段落完整文本,包含标题编号和子段落
|
:return: str - 段落完整文本
|
"""
|
full_text = ''
|
if self.title_num:
|
full_text = f"{self.title_num}. {self.text}"
|
else:
|
full_text = f"{self.text}"
|
if len(self.children):
|
for child in self.children:
|
full_text = full_text + "\n" + child.full_text_with_children
|
return full_text
|
|
def __init__(self, text: str, title_level: int, num=0, num_level=0):
|
"""
|
段落信息
|
|
属性:
|
text: str - 段落文本
|
title_level: int - 段落级别,1-9级标题,0表示正文
|
num: int - 列表序号
|
num_level: int - 列表序号级别
|
"""
|
self.text = text
|
self.title_level = title_level
|
self.title_num = ''
|
self.num = num
|
self.num_level = num_level
|
self.children: typing.List[ParagraphInfo] = []
|
self.entities: typing.List[TEntity] = []
|
|
def __str__(self):
|
return f"{self.full_text}"
|
|
def __repr__(self):
|
return f"{self.full_text}"
|
|
|
@dataclass
|
class DocInfo:
|
"""
|
文档信息
|
|
属性:
|
id: int - id
|
file_name: str - 文档名称。
|
file: typing.BinaryIO - 文档文件。
|
file_type: str - 文档类型
|
paragraphs: typing.List[ParagraphInfo] - 文档段落列表。
|
"""
|
id: int
|
file_name: str
|
file: str
|
file_type: str
|
paragraphs: typing.List[ParagraphInfo]
|
|
def __init__(self, file_name: str, file: bytes, file_type: str, paragraphs: typing.List[ParagraphInfo]):
|
"""
|
文档信息
|
|
属性:
|
file_name: str - 文档名称。
|
file: bytes - 文档文件。
|
"""
|
self.file_name = file_name
|
self.file = file
|
self.file_type = file_type
|
self.paragraphs: typing.List[ParagraphInfo] = paragraphs
|