File size: 6,253 Bytes
4cf88e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from src.domain.paragraph import Paragraph
from src.domain.block import Block

INFINITE = 10000


class Container:

    def __init__(self, paragraphs: [Paragraph], title: Paragraph = None, level: int = 0, index: [int] = None,
                 father=None, id_=0):
        if index is None:
            index = []
        self.level = level
        if not self.level:
            pass
        self.title = title
        self.paragraphs = []
        self.all_paragraphs = paragraphs
        self.children = []
        self.index = index
        self.father = father  # if not father, then the container is at the top of the hierarchy
        self.id_ = int(str(1) + str(father.id_) + str(id_))
        if paragraphs:
            self.paragraphs, self.children = self.create_children(paragraphs.copy(), level, index)
        self.containers = [self]
        for child in self.children:
            self.containers += child.containers
        self.blocks = self.get_blocks()
        self.normal, self.comment, self.task, _ = self.sort_paragraphs()

        self.one_liner = (self.title.text if self.title else '') + ' ' + self.comment
        self.root_text = self.one_liner + ' ' + self.normal


    @property
    def text(self):
        text = ""
        if self.title:
            text = "Titre " + str(self.level) + " : " + self.title.text + '\n'
        for p in self.paragraphs:
                text += p.text + '\n'
        for child in self.children:
                text += child.text
        return text

    @property
    def table_of_contents(self):
        toc = []
        if self.title:
            toc += [{str(self.level): self.title.text}]
        if self.children:
            for child in self.children:
                toc += child.table_of_contents
        return toc

    def move(self, position: int, new_father=None):
        current_father = self.father  # should be added in the domain
        current_father.children.remove(self)

        self.rank = new_father.rank + 1 if new_father else 0
        self.father = new_father
        if position < len(new_father.children):
            new_father.children.insert(position, self)
        else:
            new_father.children.append(self)

    def create_children(self, paragraphs, level, rank) -> ([], []):
        """
        creates children containers or directly attached content
        and returns the list of containers and contents of level+1
        :return:
        [Content or Container]
        """
        attached_paragraphs = []
        container_paragraphs = []
        container_title = None
        children = []
        in_children = False
        level = INFINITE
        child_id = 0

        while paragraphs:
            p = paragraphs.pop(0)
            if not in_children and not p.is_structure:
                attached_paragraphs.append(p)
            else:
                in_children = True
                if p.is_structure and p.level <= level:  # if p is higher or equal in hierarchy
                    if container_paragraphs or container_title:
                        children.append(Container(container_paragraphs, container_title, level, rank, self, child_id))
                        child_id += 1
                    container_paragraphs = []
                    container_title = p
                    level = p.level

                else:  # p is strictly lower in hierarchy
                    container_paragraphs.append(p)

        if container_paragraphs or container_title:
            children.append(Container(container_paragraphs, container_title, level, rank, self, child_id))
            child_id += 1

        return attached_paragraphs, children

    @property
    def structure(self):

        self_structure = {str(self.id_): {
            'index': str(self.id_),
            'canMove': True,
            'isFolder': True,
            'children': [p.id_ for p in self.paragraphs] + [child.id_ for child in self.children],
            'canRename': True,
            'data': {},
            'level': self.level,
            'title': self.title.text if self.title else 'root'
        }}
        paragraphs_structure = [p.structure for p in self.paragraphs]
        structure = [self_structure] + paragraphs_structure
        for child in self.children:
            structure += child.structure
        return structure

    def get_lang(self):
        """
        returns the main language of the document
        :return:
        """

    def get_structure(self, level=2):
        """
        returns the structure of the document
        :return:
        """

    def create_embeddings(self):
        """

        :return:
        """

    def get_blocks(self):
        block = Block(level=self.level, index=self.index)
        if self.title:
            block.title = self.title.text
        for p in self.paragraphs:
            if not p.blank:
                if p.text.startswith('##### '):
                    special_action = p.text.lstrip('##### ')
                    block.specials.append(special_action)
                else:
                    block.content += p.text
        blocks = [block] if block.content or block.specials else []
        for child in self.children:
            blocks += child.blocks
        return blocks

    def get_fulltask(self, doc_one_liner):
        siblings_ = self.father.children.copy()
        index = siblings_.index(self)
        siblings_before_context = [sibling.one_liner for idx, sibling in enumerate(siblings_) if idx < index]
        siblings_after_context = [sibling.one_liner for idx, sibling in enumerate(siblings_) if index < idx]

        fulltask = {'description': self.task,
                    'about': self.one_liner,
                    'doc_description': doc_one_liner,
                    'above': self.father.one_liner,
                    'before': siblings_before_context,
                    'after': siblings_after_context}
        return fulltask

    def sort_paragraphs(self) -> (str, str, str, str):
        mapping = {'normal': '', 'comment': '', 'task': '', 'title': ''}
        for p in self.paragraphs:
            mapping[p.type] += ' ' + p.parsed_text
        return mapping['normal'], mapping['comment'], mapping['task'], mapping['title']