-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathid2txt.py
56 lines (49 loc) · 1.89 KB
/
id2txt.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
54
55
56
import gzip
import pickle
import sys
import tqdm
import mmap_index
import os
def open_possibly_gz_file(f_or_name):
if f_or_name is None:
return None
elif isinstance(f_or_name, str): # a file name of some sort
if f_or_name.endswith(".gz"):
return gzip.open(f_or_name, "rt")
elif os.path.exists(f_or_name):
return open(f_or_name)
elif os.path.exists(f_or_name+".gz"):
return gzip.open(f_or_name+".gz", "rt")
else:
raise ValueError(
f"No such file {f_or_name}, neither plain nor zipped")
else:
return f_or_name
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--qry-sentences", nargs="+",
help="File(s) with the sentences in the same order in which they were queried in print_nearest_...")
parser.add_argument(
"--sentenceindex", help="File prefix with sentence mmappable index produced by mmap_index.py")
parser.add_argument("--knn-ipkl", help="The ipkl file with the knn data")
args = parser.parse_args()
# Read query texts first, we will need these to be able to print the outcome
qry_sents = []
for fname in tqdm.tqdm(args.qry_sentences):
f = open_possibly_gz_file(fname)
qry_sents.extend((l.strip() for l in f if l.strip()))
f.close()
print(f"Read {len(qry_sents)} q-sentences", file=sys.stderr, flush=True)
qry = mmap_index.Qry(args.sentenceindex)
with open(args.knn_ipkl, "rb") as f:
while True:
try:
qry_ids, knn_ids = pickle.load(f)
except EOFError:
break
for q_id, knn_id in zip(qry_ids, knn_ids):
print("**", qry_sents[int(q_id)])
for nn_id in knn_id[:10]:
print(" ", qry.get(nn_id))
print("\n\n\n")