shivangibithel commited on
Commit
a2e43c2
1 Parent(s): 39ddb78

Update sotab.py

Browse files
Files changed (1) hide show
  1. sotab.py +30 -42
sotab.py CHANGED
@@ -77,9 +77,13 @@ class WikiTableQuestions(datasets.GeneratorBasedBuilder):
77
  CTA_Validation = os.path.join(dl_manager.download_and_extract(dev_url), "CTA_Validation")
78
  CTA_Test = os.path.join(dl_manager.download_and_extract(test_url), "CTA_Test")
79
 
80
- train_file = "{}.json.gz".format(self.config.name)
81
- test_file = "{}.json.gz".format(self.config.name)
82
- dev_file = "{}.json.gz".format(self.config.name)
 
 
 
 
83
 
84
  return [
85
  datasets.SplitGenerator(
@@ -100,56 +104,40 @@ class WikiTableQuestions(datasets.GeneratorBasedBuilder):
100
  ]
101
 
102
  def _read_table_from_file(self, table_name: str, root_dir: str):
103
- def _extract_table_content(_line: str):
104
- _vals =
105
- # _vals = [_.replace("\n", " ").strip() for _ in _line.strip("\n").split("\t")]
106
- return _vals
107
-
108
- rows = []
109
- df_gt = pd.read_csv('CTA_Test_MusicRecording.csv', encoding="utf8")
110
- df = pd.read_json("./MusicRecording/"+df_gt.loc[i]["table_name"], compression='gzip', lines=True)
111
- df = df.dropna().reset_index(drop=True)
112
- correct_label = df_gt.loc[i]["label"]
113
-
114
- if df.empty:
115
- op = "None"
116
- output_list.append(op)
117
- continue
118
 
119
  col = []
120
- for j in range(len(df.loc[0])):
121
  col.append("col" + str(j+1))
122
 
123
  row = []
124
- for index in range(len(df)):
125
  row.append(df.loc[index, :].values.tolist())
126
 
127
  # {"header": ["col1", "col2", "col3"], "rows": [["row11", "row12", "row13"], ["row21", "row22", "row23"]]}
 
128
  table_context = {}
129
  table_context["header"] = col
130
  table_context["rows"] = row
131
-
132
- # assert ".csv" in _wtq_table_name
133
- # use the normalized table file
134
- table_name = table_name.replace(".csv", ".tsv")
135
- with open(os.path.join(root_dir, table_name), "r", encoding="utf8") as table_f:
136
- table_lines = table_f.readlines()
137
- # the first line is header
138
- # header = _extract_table_content(table_lines[0])
139
- for line in table_lines[1:]:
140
- rows.append(_extract_table_content(line))
141
- return {"header": header, "rows": rows, "name": table_name}
142
 
143
  # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
144
  def _generate_examples(self, main_filepath, root_dir):
145
- # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
146
- with open(main_filepath, encoding="utf-8") as f:
147
- # skip the first line since it is the tsv header
148
- next(f)
149
- for idx, line in enumerate(f):
150
- example_id, question, table_name, answer = line.strip("\n").split("\t")
151
- answer = answer.split("|")
152
- # must contain rows and header keys
153
- table_content = self._read_table_from_file(table_name, root_dir)
154
-
155
- yield idx, {"id": example_id, "question": question, "answers": answer, "table": table_content}
 
 
 
 
 
 
77
  CTA_Validation = os.path.join(dl_manager.download_and_extract(dev_url), "CTA_Validation")
78
  CTA_Test = os.path.join(dl_manager.download_and_extract(test_url), "CTA_Test")
79
 
80
+ # train_file = "{}.json.gz".format(self.config.name)
81
+ # test_file = "{}.json.gz".format(self.config.name)
82
+ # dev_file = "{}.json.gz".format(self.config.name)
83
+
84
+ train_file = "CTA_training_gt.csv".format(self.config.name)
85
+ test_file = "CTA_test_gt.csv".format(self.config.name)
86
+ dev_file = "CTA_validation_gt.csv".format(self.config.name)
87
 
88
  return [
89
  datasets.SplitGenerator(
 
104
  ]
105
 
106
  def _read_table_from_file(self, table_name: str, root_dir: str):
107
+ df_ = pd.read_json(os.path.join(root_dir, table_name), compression='gzip', lines=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  col = []
110
+ for j in range(len(df_.loc[0])):
111
  col.append("col" + str(j+1))
112
 
113
  row = []
114
+ for index in range(len(df_)):
115
  row.append(df.loc[index, :].values.tolist())
116
 
117
  # {"header": ["col1", "col2", "col3"], "rows": [["row11", "row12", "row13"], ["row21", "row22", "row23"]]}
118
+
119
  table_context = {}
120
  table_context["header"] = col
121
  table_context["rows"] = row
122
+ table_context["name"] = table_name
123
+
124
+ return table_context
 
 
 
 
 
 
 
 
125
 
126
  # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
127
  def _generate_examples(self, main_filepath, root_dir):
128
+
129
+ df = pd.read_csv(main_filepath, encoding="utf8")
130
+ for ind in df.index:
131
+ example_id = ind
132
+ table_name = df['table_name'][ind]
133
+ column_index = df['column_index'][ind]
134
+ label = df['label'][ind]
135
+ table_content = self._read_table_from_file(table_name, root_dir)
136
+
137
+ # for idx, line in enumerate(f):
138
+ # example_id, question, table_name, answer = line.strip("\n").split("\t")
139
+ # answer = answer.split("|")
140
+ # # must contain rows and header keys
141
+ # table_content = self._read_table_from_file(table_name, root_dir)
142
+
143
+ yield idx, {"id": example_id, "column_index": column_index, "label": label, "table": table_content}