akommula commited on
Commit
d628a86
·
1 Parent(s): 3522ada

Task 1, 3, 4

Browse files
Files changed (3) hide show
  1. .openpiv2_tasks.py.swp +0 -0
  2. openpi_v2.py +55 -9
  3. openpiv2_tasks.py +265 -0
.openpiv2_tasks.py.swp ADDED
Binary file (20.5 kB). View file
 
openpi_v2.py CHANGED
@@ -244,10 +244,6 @@ class OpenpiV2(datasets.GeneratorBasedBuilder):
244
  dataset = json.load(f)
245
 
246
  for id_ in dataset:
247
- print (dataset[id_])
248
- print ("--------------")
249
- print (filepath)
250
-
251
  yield int(id_), {
252
  "goal": dataset[id_]["goal"],
253
  "steps": dataset[id_]["steps"],
@@ -257,13 +253,63 @@ class OpenpiV2(datasets.GeneratorBasedBuilder):
257
  }
258
 
259
  elif self.config.name == "Task 1":
260
- pass
261
-
 
 
 
 
 
 
 
 
 
 
262
  elif self.config.name == "Task 3":
263
- pass
 
 
 
 
 
264
 
 
 
 
 
 
265
  elif self.config.name == "Task 4":
266
- pass
267
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  elif self.config.name == "Task 7":
269
  pass
 
244
  dataset = json.load(f)
245
 
246
  for id_ in dataset:
 
 
 
 
247
  yield int(id_), {
248
  "goal": dataset[id_]["goal"],
249
  "steps": dataset[id_]["steps"],
 
253
  }
254
 
255
  elif self.config.name == "Task 1":
256
+ with open(filepath) as f:
257
+ dataset = json.load(f)
258
+
259
+ for id_ in dataset:
260
+ steps_ar = dataset[id_]["steps"]
261
+ entity_changes = find_change("entity", dataset[id_]["states"])
262
+
263
+ yield int(id_), {
264
+ "steps": steps_ar,
265
+ "entity_changes": entity_changes
266
+ }
267
+
268
  elif self.config.name == "Task 3":
269
+ with open(filepath) as f:
270
+ dataset = json.load(f)
271
+
272
+ for id_ in dataset:
273
+ steps_ar = dataset[id_]["steps"]
274
+ attr_entity_changes = find_attr_entity_change(dataset[id_]["states"])
275
 
276
+ yield int(id_), {
277
+ "steps": steps_ar,
278
+ "attr_entity_changes": attr_entity_changes
279
+ }
280
+
281
  elif self.config.name == "Task 4":
282
+ with open(filepath) as f:
283
+ dataset = json.load(f)
284
+
285
+ for id_ in dataset:
286
+ steps_ar = dataset[id_]["steps"]
287
+
288
+ for state in dataset[id_]["states"]:
289
+ for el in state["entity"].split(" | "):
290
+ entity = el
291
+ attribute_changes = []
292
+
293
+ for state2 in dataset[id_]["states"]:
294
+ flag = False
295
+
296
+ for el2 in state2["entity"].split(" | "):
297
+ if entity == el2:
298
+ flag = True
299
+ break
300
+
301
+ if flag == False:
302
+ continue
303
+
304
+ if change_occur(state2["answers"]):
305
+ list_attribute = state2["attribute"].split(" | ")
306
+ attribute_changes.append([el for el in list_attribute])
307
+
308
+ yield int(id_), {
309
+ "steps": steps_ar,
310
+ "entity": entity,
311
+ "attribute_changes": attribute_changes
312
+ }
313
+
314
  elif self.config.name == "Task 7":
315
  pass
openpiv2_tasks.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import sys
3
+
4
+ sys.path.append("/Users/akommula/code/AI2/openpi_v2/v2/src")
5
+
6
+ from main import Main
7
+
8
+ class OpenpiTasks:
9
+ def __init__(self):
10
+ pass
11
+
12
+ '''
13
+ --Task 1--
14
+ Input: Paragraph path (e.g. 5 steps)
15
+ Output: JSON Entities that change (challenge: implicit entities &
16
+ explicit entites that do not change)
17
+ '''
18
+ def task1(self, paragraph_path: str):
19
+ t1 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
20
+ t1.run(mode = 3)
21
+ res_json = t1.get_json()
22
+ answer = {}
23
+
24
+ for goal in res_json:
25
+ output = {}
26
+ output["id"] = "www.wikihow.com/" + res_json[goal]["goal"].replace(' ', '-')
27
+ output["question"] = "When you " + res_json[goal]["goal"] + ", what entities will undergo some change?"
28
+ output["answer"] = []
29
+
30
+ for state in res_json[goal]["states"]:
31
+ flag = False
32
+
33
+ for step in state["answers"]:
34
+ if (len(step) > 0):
35
+ flag = True
36
+ break
37
+
38
+ if (flag):
39
+ list_entities = state["entity"].split(" | ")
40
+ output["answer"].append([el for el in list_entities])
41
+
42
+ answer[goal] = output
43
+
44
+ return answer
45
+
46
+ '''
47
+ --Task 2--
48
+ Input: Paragraph path (e.g. 5 steps)
49
+ Output: JSON Attributes that change (challenge: implicit attributes)
50
+ '''
51
+ def task2(self, paragraph_path: str):
52
+ t2 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
53
+ t2.run(mode = 3)
54
+ res_json = t2.get_json()
55
+ answer = {}
56
+
57
+ for goal in res_json:
58
+ output = {}
59
+ output["id"] = "www.wikihow.com/" + res_json[goal]["goal"].replace(' ', '-')
60
+ output["question"] = "When you " + res_json[goal]["goal"] + ", what attributes will undergo some change?"
61
+ output["answer"] = []
62
+
63
+ for state in res_json[goal]["states"]:
64
+ flag = False
65
+
66
+ for step in state["answers"]:
67
+ if (len(step) > 0):
68
+ flag = True
69
+ break
70
+
71
+ if (flag):
72
+ list_entities = state["attribute"].split(" | ")
73
+ output["answer"].append([el for el in list_entities])
74
+
75
+ answer[goal] = output
76
+
77
+ return answer
78
+
79
+ '''
80
+ --Task 3--
81
+ Input: Paragraph path (e.g. 5 steps)
82
+ Output: 2 Lists <Attr of entity> that change and do not change (challenge: implicit entities,
83
+ attributes & many combinations)
84
+ '''
85
+ def task3(self, paragraph_path: str):
86
+ t3 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
87
+ t3.run(mode = 3)
88
+ res_json = t3.get_json()
89
+
90
+ change_answer = []
91
+ no_change_answer = []
92
+
93
+ for goal in res_json:
94
+ for state in res_json[goal]["states"]:
95
+ flag = False
96
+
97
+ for step in state["answers"]:
98
+ if (len(step) > 0):
99
+ flag = True
100
+ break
101
+
102
+ if (flag):
103
+ change_str = "[" + state["attribute"] + "] of [" + state["entity"] + "] changed"
104
+ change_answer.append(change_str)
105
+ else:
106
+ no_change_str = "[" + state["attribute"] + "] of [" + state["entity"] + "] did not change"
107
+ no_change_answer.append(no_change_str)
108
+
109
+ return (change_answer, no_change_answer)
110
+
111
+ '''
112
+ --Task 4--
113
+ Input: Paragraph path (e.g. 5 steps), Entity
114
+ Output: Sequence of attribute value changes (challenge: implicit attributes)
115
+ '''
116
+ def task4(self, paragraph_path: str, entity: str):
117
+ t4 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
118
+ t4.run(mode = 3)
119
+ res_json = t4.get_json()
120
+
121
+ answer = {}
122
+
123
+ for goal in res_json:
124
+ output = {}
125
+ output["id"] = "www.wikihow.com/" + res_json["goal"].replace(' ', '-')
126
+ output["question"] = "When you " + res_json["goal"] + ", what attributes of entity: " + entity + " will undergo some change?"
127
+ output["answer"] = []
128
+
129
+ for state in res_json[goal]["states"]:
130
+ flag = False
131
+
132
+ list_entity = state["entity"].split(" | ")
133
+ for el in list_entity:
134
+ flag = (flag or el == entity)
135
+
136
+ if (flag == False):
137
+ continue
138
+
139
+ flag = False
140
+
141
+ for step in state["answers"]:
142
+ if (len(step) > 0):
143
+ flag = True
144
+ break
145
+
146
+ if (flag):
147
+ list_entities = state["attribute"].split(" | ")
148
+ output["answer"].append([el for el in list_entities])
149
+
150
+ answer[goal] = output
151
+
152
+ return (answer)
153
+
154
+ '''
155
+ --Task 5--
156
+ Input: Paragraph path (e.g. 5 steps), Entity, Attribute
157
+ Output: Sequence of attribute value changes (challenge: identify no_change steps)
158
+ '''
159
+ def task5(self, paragraph_path: str, entity: str, attribute: str):
160
+ pass
161
+ '''
162
+ t5 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
163
+ t5.run(mode = 3)
164
+ res_json = t5.get_json()
165
+
166
+ changed_state = []
167
+
168
+ for goal in res_json:
169
+ for state in res_json[goal]["states"]:
170
+ flag = False
171
+
172
+ list_entity = state["entity"].split(" | ")
173
+ for el in list_entity:
174
+ flag = (flag or el == entity)
175
+
176
+ if flag == False:
177
+ continue
178
+
179
+ flag = False
180
+
181
+ list_attr = state["attribute"].split(" | ")
182
+ for el in list_attr:
183
+ flag = (flag or el == attribute)
184
+
185
+ if flag == False:
186
+ continue
187
+
188
+ for step in state["answers"]:
189
+ list_state = step.split(" | ")
190
+ changed_state.append([el for el in list_state])
191
+
192
+ return changed_state
193
+ '''
194
+
195
+ '''
196
+ --Task 6--
197
+ Input: Paragraph path (e.g. 5 steps), Step #
198
+ Output: <Attr of entity> that changes (challenge: implicit entities,
199
+ attributes & many combinations)
200
+ '''
201
+ def task6(self, paragraph_path: str, step_num: int):
202
+ t6 = Main(input_path = paragraph_path, output_path = "", global_statistics = True, delete_singularities = False)
203
+ t6.run(mode = 3)
204
+ res_json = t6.get_json()
205
+
206
+ answer = []
207
+
208
+ for goal in res_json:
209
+ for state in res_json[goal]["states"]:
210
+ if (len(state["answers"][step_num - 1]) > 0):
211
+ change_str = "[" + state["attribute"] + "] of [" + state["entity"] +"] changed"
212
+ answer.append(change_str)
213
+
214
+ return (answer)
215
+
216
+
217
+ '''
218
+ --Task 7--
219
+ Input: Paragraph path (e.g. 5 steps), Image, Step #
220
+ Output: <Attr values of entity> that change at that step (before and after)
221
+ '''
222
+ def task7(self, paragraph_path: str, image: str, step_num: int):
223
+ pass
224
+
225
+ '''
226
+ --Task 8 (TODO: Check with Niket)--
227
+ Input: None
228
+ Output: Accuracy (Prepcsion & Recall) in addition to
229
+ Consistency (before of step 'x' doesn't contradict after of step 'x - 1')
230
+ '''
231
+
232
+ if __name__ == '__main__':
233
+ op_t = OpenpiTasks()
234
+ args = sys.argv[1:]
235
+
236
+ if args[0] == '1':
237
+ assert(len(args) == 2)
238
+ print(op_t.task1(args[1]))
239
+
240
+ elif args[0] == '2':
241
+ assert(len(args) == 2)
242
+ print(op_t.task2(args[1]))
243
+
244
+ elif args[0] == '3':
245
+ assert(len(args) == 2)
246
+ print(op_t.task3(args[1]))
247
+
248
+ elif args[0] == '4':
249
+ assert(len(args) == 3)
250
+ print(op_t.task4(args[1], args[2]))
251
+
252
+ elif args[0] == '5':
253
+ assert(len(args) == 4)
254
+ print(op_t.task5(args[1], args[2], args[3]))
255
+
256
+ elif args[0] == '6':
257
+ assert(len(args) == 3)
258
+ print(op_t.task6(args[1], args[2]))
259
+
260
+ elif args[0] == '7':
261
+ assert(len(args) == 4)
262
+ print(op_t.task7(args[1], args[2], args[3]))
263
+
264
+ else:
265
+ print ("Not valid task or parameter length")