# -*- coding: utf-8 -*-
|
#
|
# @author: lyg
|
# @date: 2025-5-12
|
# @version: 1
|
# @description: 文档相关数据类
|
from dataclasses import dataclass
|
import typing
|
from enum import Enum
|
|
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: 引用列表
|
entities: 实体词列表
|
is_toc: 是否是目录标志
|
"""
|
id: int
|
text: str
|
title_level: int
|
title_num: str
|
num_level: int
|
num: int
|
children: typing.List
|
refs: typing.List
|
entities: typing.List[TEntity]
|
is_toc: bool
|
|
@property
|
def title(self):
|
if self.title_level:
|
text = self.full_text
|
idx = text.index('\n')
|
return text[0:idx]
|
return ''
|
|
@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, is_toc=False):
|
"""
|
段落信息
|
|
属性:
|
text: str - 段落文本
|
title_level: int - 段落级别,1-9级标题,0表示正文
|
num: int - 列表序号
|
num_level: int - 列表序号级别
|
is_toc: bool - 是否是目录
|
"""
|
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] = []
|
self.refs: typing.List[ParagraphInfo] = []
|
self.is_toc = is_toc
|
self.id = 0
|
|
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
|
|
|
class _DocType:
|
tm_outline = '遥测大纲'
|
user_requirements = '用户需求'
|
tm_pkt_design = '源包设计'
|
bus_comm_proto = '总线通信协议'
|
tc_format = '指令格式'
|
tc_cmd_table = '遥控指令表'
|
|
|
DocType = _DocType()
|