-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
document.py
179 lines (142 loc) · 6.04 KB
/
document.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from collections.abc import MutableSequence
from typing import Callable
from typing import Union, Sequence, Iterable, Tuple
import numpy as np
from ...helper import typename
try:
# when protobuf using Cpp backend
from google.protobuf.pyext._message import RepeatedCompositeContainer as RepeatedContainer
except:
# when protobuf using Python backend
from google.protobuf.internal.containers import RepeatedCompositeFieldContainer as RepeatedContainer
from ...proto.jina_pb2 import DocumentProto
if False:
from ..document import Document
__all__ = ['DocumentSet']
class DocumentSet(MutableSequence):
""":class:`DocumentSet` is a mutable sequence of :class:`Document`,
it gives an efficient view of a list of Document. One can iterate over it like
a generator but ALSO modify it, count it, get item, or union two 'DocumentSet's using the '+' and '+=' operators.
"""
def __init__(self, docs_proto: Union['RepeatedContainer', Sequence['Document']]):
super().__init__()
self._docs_proto = docs_proto
self._docs_map = {}
def insert(self, index: int, doc: 'Document') -> None:
self._docs_proto.insert(index, doc.proto)
def __setitem__(self, key, value: 'Document'):
if isinstance(key, int):
self._docs_proto[key].CopyFrom(value)
elif isinstance(key, str):
self._docs_map[key].CopyFrom(value)
else:
raise IndexError(f'do not support this index {key}')
def __delitem__(self, index):
del self._docs_proto[index]
def __len__(self):
return len(self._docs_proto)
def __iter__(self):
from ..document import Document
for d in self._docs_proto:
yield Document(d)
def __getitem__(self, item):
from ..document import Document
if isinstance(item, int):
return Document(self._docs_proto[item])
elif isinstance(item, str):
return Document(self._docs_map[item])
elif isinstance(item, slice):
return DocumentSet(self._docs_proto[item])
else:
raise IndexError(f'do not support this index {item}')
def __add__(self, other: 'DocumentSet'):
v = DocumentSet([])
for doc in self:
v.add(doc)
for doc in other:
v.add(doc)
return v
def __iadd__(self, other: 'DocumentSet'):
for doc in other:
self.add(doc)
return self
def append(self, doc: 'Document') -> 'Document':
return self._docs_proto.append(doc.proto)
def add(self, doc: 'Document') -> 'Document':
"""Shortcut to :meth:`append`, do not override this method """
return self.append(doc)
def extend(self, iterable: Iterable['Document']) -> None:
for doc in iterable:
self.append(doc)
def clear(self):
del self._docs_proto[:]
def reverse(self):
"""In-place reverse the sequence """
if isinstance(self._docs_proto, RepeatedContainer):
size = len(self._docs_proto)
hi_idx = size - 1
for i in range(int(size / 2)):
tmp = DocumentProto()
tmp.CopyFrom(self._docs_proto[hi_idx])
self._docs_proto[hi_idx].CopyFrom(self._docs_proto[i])
self._docs_proto[i].CopyFrom(tmp)
hi_idx -= 1
elif isinstance(self._docs_proto, list):
self._docs_proto.reverse()
def build(self):
"""Build a doc_id to doc mapping so one can later index a Document using
doc_id as string key
"""
self._docs_map = {d.id: d for d in self._docs_proto}
def sort(self, *args, **kwargs):
self._docs_proto.sort(*args, **kwargs)
def traverse(self, traversal_paths: Sequence[str], callback_fn: Callable, *args, **kwargs):
for d in self:
d.traverse(traversal_paths, callback_fn, *args, **kwargs)
@property
def all_embeddings(self) -> Tuple['np.ndarray', 'DocumentSet', 'DocumentSet']:
"""Return all embeddings from every document in this set as a ndarray
:return a tuple of embedding in :class:`np.ndarray`,
the corresponding documents in a :class:`DocumentSet`,
and the documents have no embedding in a :class:`DocumentSet`.
"""
return self._extract_docs('embedding')
@property
def all_contents(self) -> Tuple['np.ndarray', 'DocumentSet', 'DocumentSet']:
"""Return all embeddings from every document in this set as a ndarray
:return: a tuple of embedding in :class:`np.ndarray`,
the corresponding documents in a :class:`DocumentSet`,
and the documents have no contents in a :class:`DocumentSet`.
"""
return self._extract_docs('content')
def _extract_docs(self, attr: str) -> Tuple['np.ndarray', 'DocumentSet', 'DocumentSet']:
contents = []
docs_pts = []
bad_docs = []
for doc in self:
content = getattr(doc, attr)
if content is not None:
contents.append(content)
docs_pts.append(doc)
else:
bad_docs.append(doc)
contents = np.stack(contents) if contents else None
return contents, DocumentSet(docs_pts), DocumentSet(bad_docs)
def __bool__(self):
"""To simulate ```l = []; if l: ...``` """
return len(self) > 0
def new(self) -> 'Document':
"""Create a new empty document appended to the end of the set"""
from ..document import Document
return self.append(Document())
def __str__(self):
from ..document import Document
content = ',\n'.join(str(Document(d)) for d in self._docs_proto[:3])
if len(self._docs_proto) > 3:
content += f'in total {len(self._docs_proto)} items'
return content
def __repr__(self):
content = ' '.join(f'{k}={v}' for k, v in {'length': len(self._docs_proto)}.items())
content += f' at {id(self)}'
content = content.strip()
return f'<{typename(self)} {content}>'