(
self,
client: botocore.client.BaseClient,
input_id: str,
decoder
)
| 205 | return result |
| 206 | |
| 207 | def _handle_documents( |
| 208 | self, |
| 209 | client: botocore.client.BaseClient, |
| 210 | input_id: str, |
| 211 | decoder |
| 212 | ) -> InputResult: |
| 213 | # download doc |
| 214 | input_uri = urlparse( |
| 215 | os.path.join( |
| 216 | self._input_base_uri, f"{input_id}.json.gz" |
| 217 | ) |
| 218 | ) |
| 219 | dl_status, input_buffer = self._dload_file(input_uri, client=client) |
| 220 | |
| 221 | # check if download was successful |
| 222 | if not dl_status.is_success: |
| 223 | return InputResult( |
| 224 | is_success=False, msg=dl_status.msg, input_id=input_id |
| 225 | ) |
| 226 | |
| 227 | num_docs = 0 |
| 228 | total_tokens = 0 |
| 229 | token_counts = [] |
| 230 | |
| 231 | try: |
| 232 | with gzip.open(input_buffer, mode="rb") as in_fh: |
| 233 | for idx, obj in enumerate(in_fh): |
| 234 | record = decoder.decode(obj) |
| 235 | |
| 236 | # tokenize |
| 237 | num_tokens = len( |
| 238 | TOKENIZER.encode(record.raw_content).tokens |
| 239 | ) |
| 240 | token_counts.append((idx, num_tokens)) |
| 241 | |
| 242 | total_tokens += num_tokens |
| 243 | num_docs += 1 |
| 244 | |
| 245 | except Exception as e: |
| 246 | msg = ( |
| 247 | f"__DECODE_ENCODE_FAIL__ {input_id}: " |
| 248 | f"caught exception {e.__class__.__name__}: {e}" |
| 249 | ) |
| 250 | return InputResult(is_success=False, msg=msg, input_id=input_id) |
| 251 | |
| 252 | return InputResult( |
| 253 | is_success=True, |
| 254 | msg="", |
| 255 | input_id=input_id, |
| 256 | num_docs=num_docs, |
| 257 | num_tokens=total_tokens, |
| 258 | token_counts=token_counts |
| 259 | ) |
| 260 | |
| 261 | def run(self): |
| 262 | # init logging |
no test coverage detected