/
document_wrapper.py
160 lines (115 loc) · 4.85 KB
/
document_wrapper.py
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
# -*- coding: utf-8 -*-
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Wrappers for Document AI Document type."""
import dataclasses
import re
from typing import List
from google.cloud import documentai
from google.cloud import storage
from google.cloud.documentai_toolbox.wrappers import page_wrapper, entity_wrapper
def _entities_from_shards(
shards: documentai.Document,
) -> List[entity_wrapper.EntityWrapper]:
result = []
for shard in shards:
for entity in shard.entities:
result.append(entity_wrapper.EntityWrapper.from_documentai_entity(entity))
return result
def _pages_from_shards(shards: documentai.Document) -> List[page_wrapper.PageWrapper]:
result = []
for shard in shards:
text = shard.text
for page in shard.pages:
result.append(page_wrapper.PageWrapper.from_documentai_page(page, text))
return result
def _get_bytes(output_bucket: str, output_prefix: str) -> List[bytes]:
result = []
storage_client = storage.Client()
blob_list = storage_client.list_blobs(output_bucket, prefix=output_prefix)
for blob in blob_list:
if blob.name.endswith(".json"):
blob_as_bytes = blob.download_as_bytes()
result.append(blob_as_bytes)
return result
def _get_shards(gcs_prefix: str) -> List[documentai.Document]:
"""Gets shards from gcs_prefix location and returns a list of shards."""
shards = []
match = re.match(r"gs://(.*?)/(.*)", gcs_prefix)
if match is None:
raise ValueError("gcs_prefix does not match accepted format")
output_bucket, output_prefix = match.groups()
file_check = re.match(r"(.*[.].*$)", output_prefix)
if file_check is not None:
raise ValueError("gcs_prefix cannot contain file types")
byte_array = _get_bytes(output_bucket, output_prefix)
for byte in byte_array:
shards.append(documentai.Document.from_json(byte))
return shards
def print_gcs_document_tree(gcs_prefix: str) -> None:
"""Prints a tree of Documents in gcs_prefix location."""
display_filename_prefix_middle = "├──"
display_filename_prefix_last = "└──"
match = re.match(r"gs://(.*?)/(.*)", gcs_prefix)
if match is None:
raise ValueError("gcs_prefix does not match accepted format")
output_bucket, output_prefix = match.groups()
file_check = re.match(r"(.*[.].*$)", output_prefix)
if file_check is not None:
raise ValueError("gcs_prefix cannot contain file types")
storage_client = storage.Client()
blob_list = storage_client.list_blobs(output_bucket, prefix=output_prefix)
path_list = {}
for blob in blob_list:
file_path = blob.name.split("/")
file_name = file_path.pop()
file_path2 = "/".join(file_path)
if file_path2 in path_list:
path_list[file_path2] += f"{file_name},"
else:
path_list[file_path2] = f"{file_name},"
for key in path_list:
a = path_list[key].split(",")
a.pop()
print(f"{key}")
togo = 4
for idx, val in enumerate(a):
if idx == len(a) - 1:
if len(a) > 4:
print("│ ....")
print(f"{display_filename_prefix_last}{val}\n")
elif len(a) > 4 and togo != -1:
togo -= 1
print(f"{display_filename_prefix_middle}{val}")
elif len(a) <= 4:
print(f"{display_filename_prefix_middle}{val}")
@dataclasses.dataclass
class DocumentWrapper:
"""Represents a wrapped Document.
A single Document protobuf message might be written as several JSON files on
GCS by Document AI's BatchProcessDocuments method. This class hides away the
shards from the users and implements convenient methods for searching and
extracting information within the Document.
"""
gcs_prefix: str
def __post_init__(self):
self._shards = _get_shards(gcs_prefix=self.gcs_prefix)
self.pages = _pages_from_shards(shards=self._shards)
self.entities = _entities_from_shards(shards=self._shards)
pages: List[page_wrapper.PageWrapper] = dataclasses.field(init=False, repr=False)
entities: List[entity_wrapper.EntityWrapper] = dataclasses.field(
init=False, repr=False
)
_shards: List[documentai.Document] = dataclasses.field(init=False, repr=False)