Skip to content

Commit e90d15f

Browse files
committed
new: add json reader
1 parent f15fcd3 commit e90d15f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

dataset_reader/json_reader.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Iterator
2+
3+
import json
4+
5+
from dataset_reader.base_reader import BaseReader, Record, Query
6+
7+
8+
class JSONReader(BaseReader):
9+
def __init__(self, path):
10+
self.path = path
11+
12+
def read_queries(self) -> Iterator[Query]:
13+
with open(self.path, "r") as json_fp:
14+
for json_line in json_fp:
15+
line = json.loads(json_line)
16+
yield Query(
17+
vector=line, meta_conditions=None, expected_result=None,
18+
)
19+
20+
def read_data(self) -> Iterator[Record]:
21+
with open(self.path, "r") as json_fp:
22+
for idx, json_line in enumerate(json_fp):
23+
line = json.loads(json_line)
24+
yield Record(id=idx, vector=line, metadata=None)
25+
26+
27+
if __name__ == "__main__":
28+
import os
29+
from benchmark.settings import DATASET_DIR
30+
31+
test_path = os.path.join(DATASET_DIR, "random-100", "vectors.jsonl")
32+
record = next(JSONReader(test_path).read_data())
33+
print(record, end="\n\n")
34+
35+
query = next(JSONReader(test_path).read_queries())
36+
print(query)

0 commit comments

Comments
 (0)