File size: 2,383 Bytes
2e3ba97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from src.model.container import Container
from src.model.paragraph import Paragraph
from src.tools.index_creation import set_good_indexes
from src.tools.reader import get_pdf_title_styles


class Doc:

    def __init__(self, path='', id_=None):

        self.title = self.get_good_title(path)
        self.id_ = id(self)
        self.path = path
        paragraphs = get_pdf_title_styles(path)
        self.container = Container(paragraphs, father=self)
        set_good_indexes(self.container)
        self.blocks = self.get_blocks()


    def get_good_title(self,path):
        if '/' in path:
            res = path.split('/')[-1]
        if '\\' in path:
            res = path.split('\\')[-1]
        else:
            res = path
        return res 

    @property
    def structure(self):

        return self.container.structure

    def get_blocks(self):

        def from_list_to_str(index_list):
            index_str = str(index_list[0])
            for el in index_list[1:]:
                index_str += '.' + str(el)
            return index_str

        blocks = self.container.blocks
        # blocks = self.delete_duplicate()
        for block in blocks:
            block.doc = self.title
            block.index = from_list_to_str(block.index)
            print(block.index + ' ' + block.title)
        return blocks
    

    def delete_duplicate(self):
        while self.found_duplicates(self.container.blocks):
            for i in range(len(self.container.blocks) - 1):
                if self.container.blocks[i].index == self.container.blocks[i + 1].index:
                    if self.container.blocks[i].index != []:
                        self.container.blocks[i].index.pop()
        return self.container.blocks
        
    def found_duplicates(self, blocks):
        for i in range(len(blocks) - 1):
            if blocks[i].index == blocks[i + 1].index:
                return True
        return False

"""
    current_level = len(current_index)
    if 0 < block.level:
        if block.level == current_level:
            current_index[-1] += 1
        elif current_level < block.level:
            current_index.append(1)
        elif block.level < current_level:
            current_index = current_index[:block.level]
            current_index[-1] += 1
        block.index = from_list_to_str(current_index)
    else:
        block.index = "0"
"""