lyg
2 天以前 22f370322412074174cde20ecfd14ec03657ab63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- 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()