Read in RACE files, combine, clean-up, tokenize, and convert to samples.
(datapath, tokenizer, max_qa_length, max_seq_length)
| 47 | |
| 48 | |
| 49 | def process_single_datapath(datapath, tokenizer, max_qa_length, max_seq_length): |
| 50 | """Read in RACE files, combine, clean-up, tokenize, and convert to |
| 51 | samples.""" |
| 52 | |
| 53 | print_rank_0(' > working on {}'.format(datapath)) |
| 54 | start_time = time.time() |
| 55 | |
| 56 | # Get list of files. |
| 57 | filenames = glob.glob(os.path.join(datapath, '*.txt')) |
| 58 | |
| 59 | samples = [] |
| 60 | num_docs = 0 |
| 61 | num_questions = 0 |
| 62 | num_samples = 0 |
| 63 | # Load all the files |
| 64 | for filename in filenames: |
| 65 | with open(filename, 'r') as f: |
| 66 | for line in f: |
| 67 | data = json.loads(line) |
| 68 | num_docs += 1 |
| 69 | |
| 70 | context = data["article"] |
| 71 | questions = data["questions"] |
| 72 | choices = data["options"] |
| 73 | answers = data["answers"] |
| 74 | # Check the length. |
| 75 | assert len(questions) == len(answers) |
| 76 | assert len(questions) == len(choices) |
| 77 | |
| 78 | # Context: clean up and convert to ids. |
| 79 | context = clean_text(context) |
| 80 | context_ids = tokenizer.tokenize(context) |
| 81 | |
| 82 | # Loop over questions. |
| 83 | for qi, question in enumerate(questions): |
| 84 | num_questions += 1 |
| 85 | # Label. |
| 86 | label = ord(answers[qi]) - ord("A") |
| 87 | assert label >= 0 |
| 88 | assert label < NUM_CHOICES |
| 89 | assert len(choices[qi]) == NUM_CHOICES |
| 90 | |
| 91 | # For each question, build num-choices samples. |
| 92 | ids_list = [] |
| 93 | types_list = [] |
| 94 | paddings_list = [] |
| 95 | for ci in range(NUM_CHOICES): |
| 96 | choice = choices[qi][ci] |
| 97 | # Merge with choice. |
| 98 | if "_" in question: |
| 99 | qa = question.replace("_", choice) |
| 100 | else: |
| 101 | qa = " ".join([question, choice]) |
| 102 | # Clean QA. |
| 103 | qa = clean_text(qa) |
| 104 | # Tokenize. |
| 105 | qa_ids = tokenizer.tokenize(qa) |
| 106 | # Trim if needed. |
no test coverage detected