-
Notifications
You must be signed in to change notification settings - Fork 0
/
fresh_tokenize.py
50 lines (40 loc) · 1.91 KB
/
fresh_tokenize.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
# tokenize and save the extracted rationales
# KR4: Korean Restaurant Reviews Rationalized with Ratings
# Uses multiprocessing
# $ python fresh_tokenize.py [name] [save_path] [--unrationale]
# example: $ python fresh_tokenize.py FT3_TopK FT3_TopK
# example: $ python fresh_tokenize.py FT3_TopK FT3_TopK-Un --unrationale
import argparse
from glob import glob
from multiprocessing import Pool
from datasets import load_from_disk
from transformers import BertTokenizer
# 0. argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="path to load rationales(KR4)")
parser.add_argument("save_path", help='path to save the tokenized rationales (or unrationales')
parser.add_argument("--unrationale", help='use the flag to tokenize unrationale instead of rationale', action='store_true') # default false
args = parser.parse_args()
# 1. tokenizer from hgf
tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased')
def tokenize_func(example, use_unrationale=False):
if use_unrationale:
return tokenizer(example['Unrationale'], truncation=True, padding=True)
return tokenizer(example['Rationale'], truncation=True, padding=True)
# 2. function to preprocess (including tokenize) on single batch
def preprocess_batch(batch_path):
# load batch
batch = load_from_disk(batch_path)
# tokenize
batch = batch.map(tokenize_func, batched=True, fn_kwargs={'use_unrationale':args.unrationale})
# process columns
batch = batch.remove_columns(['Unrationale', 'Rationale'])
batch = batch.rename_column('Rating', 'labels')
batch.set_format(type='torch')
# save tokenized rationale
batch_index = batch_path[batch_path.find('batch'): ]
batch.save_to_disk(f'kr4_tokenized/{args.save_path}/{batch_index}')
# 3. execute preprocess_batch() with multiprocessing
batch_path_list = glob(f'kr4/{args.name}/batch_*')
with Pool(127) as p:
p.map(preprocess_batch, batch_path_list)