MCPcopy Create free account
hub / github.com/K-Quant/HiDy / convert_example

Function convert_example

tax_policy/utils.py:578–632  ·  view source on GitHub ↗

example: { title prompt content result_list }

(example, tokenizer, max_seq_len, multilingual=False)

Source from the content-addressed store, hash-verified

576
577
578def convert_example(example, tokenizer, max_seq_len, multilingual=False):
579 """
580 example: {
581 title
582 prompt
583 content
584 result_list
585 }
586 """
587 encoded_inputs = tokenizer(
588 text=[example["prompt"]],
589 text_pair=[example["content"]],
590 truncation=True,
591 max_seq_len=max_seq_len,
592 pad_to_max_seq_len=True,
593 return_attention_mask=True,
594 return_position_ids=True,
595 return_dict=False,
596 return_offsets_mapping=True,
597 )
598 encoded_inputs = encoded_inputs[0]
599 offset_mapping = [list(x) for x in encoded_inputs["offset_mapping"]]
600 bias = 0
601 for index in range(1, len(offset_mapping)):
602 mapping = offset_mapping[index]
603 if mapping[0] == 0 and mapping[1] == 0 and bias == 0:
604 bias = offset_mapping[index - 1][1] + 1 # Includes [SEP] token
605 if mapping[0] == 0 and mapping[1] == 0:
606 continue
607 offset_mapping[index][0] += bias
608 offset_mapping[index][1] += bias
609 start_ids = [0.0 for x in range(max_seq_len)]
610 end_ids = [0.0 for x in range(max_seq_len)]
611 for item in example["result_list"]:
612 start = map_offset(item["start"] + bias, offset_mapping)
613 end = map_offset(item["end"] - 1 + bias, offset_mapping)
614 start_ids[start] = 1.0
615 end_ids[end] = 1.0
616 if multilingual:
617 tokenized_output = {
618 "input_ids": encoded_inputs["input_ids"],
619 "position_ids": encoded_inputs["position_ids"],
620 "start_positions": start_ids,
621 "end_positions": end_ids,
622 }
623 else:
624 tokenized_output = {
625 "input_ids": encoded_inputs["input_ids"],
626 "token_type_ids": encoded_inputs["token_type_ids"],
627 "position_ids": encoded_inputs["position_ids"],
628 "attention_mask": encoded_inputs["attention_mask"],
629 "start_positions": start_ids,
630 "end_positions": end_ids,
631 }
632 return tokenized_output

Callers

nothing calls this directly

Calls 1

map_offsetFunction · 0.85

Tested by

no test coverage detected