lyg
2025-05-14 acde3bd32f07bf02839a21e8fe5b4e69bfca2251
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
# -*- 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