-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
94 lines (75 loc) · 2.64 KB
/
dataset.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
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
# Build dataset object
import json
import csv
from pydantic import BaseModel
from pathlib import Path
from tqdm import tqdm
# models for th pipline
class TrainData(BaseModel):
words: list[str]
defs: list[str]
words_embds: list[list[float]] | None
class TestData(BaseModel):
defs: list[str]
ids: list[str]
# handle the files and extarct needed data
class TrainFile(BaseModel):
path: Path
kind: str
def _extract_data_json(self,word_h, definition_h, emb_h:str=None):
# open json
with open(self.path, "r") as f:
data = json.load(f)
words = []
defs = []
embds = [] if emb_h else None
for i in tqdm(data, desc="extracting json data ..."):
words.append(i[word_h])
defs.append(i[definition_h])
embds.append(i[emb_h]) if emb_h else None
return TrainData(words=words, defs=defs, words_embds=embds)
def _extract_data_csv(self,word_h, definition_h, emb_h:str=None):
# This function is not tested yet
# open csv
with open(self.path, "r") as f:
data = csv.reader(f)
words = []
defs = []
embds = [] if emb_h else None
for i in data:
words.append(i[word_h])
defs.append(i[definition_h])
embds.append(i[emb_h]) if emb_h else None
return TrainData(words=words, defs=defs, words_embds=embds)
def extract_data(self,word_h, definition_h, emb_h:str=None):
if self.kind == "json":
return self._extract_data_json(word_h, definition_h, emb_h)
elif self.kind == "csv":
return self._extract_data_csv(word_h, definition_h, emb_h)
class TestFile(BaseModel):
path: Path
kind: str
def _extract_data_json(self, def_h: str, id_h: str):
with open(self.path, "r") as f:
data = json.load(f)
defs = []
ids = []
for i in tqdm(data, desc="extracting data"):
defs.append(i[def_h])
ids.append(i[id_h])
return TestData(defs=defs, ids=ids)
def _extract_data_csv(self, def_h: str, id_h: str):
# Open the CSV file
with open(self.path, "r") as f:
reader = csv.DictReader(f) # Read CSV rows as dictionaries
defs = []
ids = []
for row in tqdm(reader, desc="extracting data from csv"):
defs.append(row[def_h]) # Extract the value of the definition column
ids.append(row[id_h]) # Extract the value of the ID column
return TestData(defs=defs, ids=ids)
def extract_data(self, def_h: str, id_h: str):
if self.kind == "json":
return self._extract_data_json(def_h, id_h)
if self.kind == "csv":
return self._extract_data_csv(def_h, id_h)