-
Notifications
You must be signed in to change notification settings - Fork 7
/
merge_indexes.py
53 lines (43 loc) · 2.23 KB
/
merge_indexes.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
51
52
53
# _*_ coding:utf-8 _*_
#**********************************************************************************************************************
# author: Ahmed Mourad
# date: 08 Dec 2021
# description: Utility script to merge TILDE expanded index given a list of directories
#**********************************************************************************************************************
from argparse import ArgumentParser
import os
import h5py
import numpy as np
def main(args):
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
docids = None
with h5py.File(os.path.join(args.output_dir, "tildev2_index.hdf5"), "w") as h5fw:
counter = 0
for index_path in args.input_dirs:
if os.path.isdir(index_path):
print(index_path)
# read the h5py ([tokens], [scores])
f = h5py.File(os.path.join(index_path, "tildev2_index.hdf5"), 'r')
# name of the dataset
dset_name = list(f.keys())[0]
tokens_scores = f[dset_name][:]
# initial creation of the dataset
if counter == 0:
h5fw.create_dataset(dset_name, data=tokens_scores, maxshape=(None,))
docids = np.load(os.path.join(index_path, "docids.npy"))
else:
# resize the dataset
h5fw[dset_name].resize((counter+tokens_scores.shape[0],))
h5fw[dset_name][counter:counter+tokens_scores.shape[0]] = tokens_scores[:]
docids = np.append(docids, np.load(os.path.join(index_path, "docids.npy")))
counter += tokens_scores.shape[0]
f.close()
assert len(docids) == len(h5fw[dset_name])
np.save(os.path.join(args.output_dir, "docids.npy"), docids)
if __name__ == "__main__":
parser = ArgumentParser(prog="Merge TILDE v2 indexes")
parser.add_argument('-o', '--output_dir', type=str, default='', help="output directory for the final index")
parser.add_argument('input_dirs', nargs='+', help="list of directories for different indexes")
args = parser.parse_args()
main(args)