File size: 3,690 Bytes
d6585f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#
# Pyserini: Reproducible IR research with sparse and dense representations
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import multiprocessing
import json
import time
from joblib import Parallel, delayed
from transformers import AutoTokenizer

'''Replace original contents fields with bert tokenization'''


parser = argparse.ArgumentParser(description='Convert BEIR original documents to word piece tokenized.')
parser.add_argument('--input', metavar='input file', help='input file',
                    type=str, required=True)
parser.add_argument('--output', metavar='output file', help='output file',
                    type=str, required=True)
parser.add_argument('--workers', metavar='# of processes', help='# of workers to spawn',
                    type=int, default=multiprocessing.cpu_count() - 2)
parser.add_argument('--tokenizer', metavar='tokenizer', help='tokenizer',
                    type=str, default='bert-base-cased')


args = parser.parse_args()
print(args)
arg_vars = vars(args)

def get_retokenized(tokenizer, text):
    """
    copy from pyserini.scripts.ltr_msmarco.convert_common.get_retokenized
    Obtain a space separated re-tokenized text.
    :param tokenizer:  a tokenizer that has the function
                       tokenize that returns an array of tokens.
    :param text:       a text to re-tokenize.
    """
    return ' '.join(tokenizer.tokenize(text))


def batch_file(iterable, n=10000):
    batch = []
    for line in iterable:
        batch.append(line)
        if len(batch) == n:
            yield batch
            batch = []
    if len(batch) > 0:
        yield batch
        batch = []
    return


def batch_process(batch):
    bert_tokenizer = AutoTokenizer.from_pretrained(args.tokenizer)

    def process(line):
        if not line:
            return None
        json_line = json.loads(line)
        pid = json_line['_id']
        body = json_line['text']
        metadata = json_line['metadata']

        doc = {"_id": pid,
               "text": get_retokenized(bert_tokenizer, body.lower()),
               "metadata":metadata, }
        return doc
    
    res = []
    start = time.time()
    for line in batch:
        res.append(process(line))
        if len(res) % 1000 == 0:
            end = time.time()
            print(f"finish {len(res)} using {end-start}")
            start = end
    return res


if __name__ == '__main__':
    workers = args.workers
    print(f"Spawning {workers} processes")
    pool = Parallel(n_jobs=workers, verbose=10)
    line_num = 0

    with open(args.input) as inFile:
        with open(args.output, 'w') as outFile:
            for batch_json in pool([delayed(batch_process)(batch) for batch in batch_file(inFile)]):
                for doc_json in batch_json:
                    line_num = line_num + 1
                    if doc_json is not None:
                        outFile.write(json.dumps(doc_json) + '\n')
                    else:
                        print(f"Ignoring misformatted line {line_num}")

                    if line_num % 10000 == 0:
                        print(f"Processed {line_num} passages")

    print(f"Processed {line_num} passages")