-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_input.py
More file actions
148 lines (127 loc) · 5.18 KB
/
data_input.py
File metadata and controls
148 lines (127 loc) · 5.18 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import json
class DataInput:
def __init__(self):
self.file_path = os.getcwd() + '/sid-data'
self.data = self.extract_umrf_action_data()
# self.data = self.extract_umrf_data()
# self.training_split = 251 # len(self.data)
self.training_jsons = None
self.training_jsons_str = None
self.train_nl_prompts = None
# self.get_training_data_umrf_actions()
# self.get_training_data()
self.new_get_training_data()
def get_nl_prompt(self, json_dicts: list):
nl_prompts = []
for json in json_dicts:
nl_prompts.append(json['graph_description'])
return nl_prompts
def get_nl_prompt_actions(self, json_dicts: list):
nl_prompts = []
for json in json_dicts:
nl_prompts.append(json['description'])
return nl_prompts
def extract_umrf_action_data(self):
self.file_path = os.getcwd() + '/ta_place.txt'
file = open(self.file_path, 'r')
lines = file.readlines()
data = []
for line in lines:
data.append(json.loads(line))
return data
def extract_umrf_data(self):
json_list = [pos_json for pos_json in os.listdir(self.file_path) if pos_json.endswith('.json')]
json_list_cropped = [e[11:] for e in json_list]
json_list_cropped = [e[:-11] for e in json_list_cropped]
json_cropped_int = [int(x) for x in json_list_cropped]
json_cropped_int.sort()
json_cropped_int = [str(x) for x in json_cropped_int]
x = range(len(json_cropped_int))
sid_file_names = []
for i in x:
sid_file_string = 'umrf_graph_' + json_cropped_int[i] + '.umrfg.json'
sid_file_names.append(sid_file_string)
json_dicts = []
for json_file in sid_file_names:
with open(self.file_path + '/' + json_file, 'r') as f:
json_string = f.read()
json_dict = json.loads(json_string)
del json_dict['graph_name']
del json_dict['graph_state']
json_dicts.append(json_dict)
return json_dicts
# def get_training_data_umrf_actions(self):
# # self.training_jsons = self.data[:self.training_split]
# self.training_jsons = self.data
# self.train_nl_prompts = self.get_nl_prompt(self.training_jsons)
# labels = []
# for ex in self.training_jsons:
# # items_list = []
# for items in ex['umrf_actions']:
# if items['package_name'] == 'ta_place':
# del items['id']
# del items['effect']
# del items['package_name']
# try:
# del items['children']
# except:
# print('no children')
# try:
# del items['parents']
# except:
# print('no children')
# # items_list.append(items)
# labels.append(items)
# self.training_jsons_str = self.get_json_as_string(labels)
#
# out_file = open('ta_place' + '.txt', 'w')
# for ex in self.training_jsons_str:
# out_file.write(ex + '\n')
# out_file.close()
def get_training_data(self):
# self.training_jsons = self.data[:self.training_split]
self.training_jsons = self.data
# self.train_nl_prompts = self.get_nl_prompt_actions(self.training_jsons)
self.train_nl_prompts = self.get_nl_prompt(self.training_jsons)
labels = []
for ex in self.training_jsons:
items_list = []
for items in ex['umrf_actions']:
del items['id']
del items['effect']
del items['package_name']
items_list.append(items)
labels.append(items_list)
self.training_jsons_str = self.get_json_as_string(labels)
def new_get_training_data(self):
# self.training_jsons = self.data[:self.training_split]
self.training_jsons = self.data
self.train_nl_prompts = self.get_nl_prompt_actions(self.training_jsons)
#
# labels = []
# for ex in self.training_jsons:
# items_list = []
# for items in ex['umrf_actions']:
# del items['id']
# del items['effect']
# del items['package_name']
# items_list.append(items)
# labels.append(items_list)
# self.training_jsons_str = self.get_json_as_string(labels)
self.file_path = os.getcwd() + '/ta_place.txt'
file = open(self.file_path, 'r')
lines = file.readlines()
data = []
for line in lines:
data.append(json.loads(line))
self.training_jsons_str = data
def get_json_as_string(self, json_list: list):
json_strings = []
for item in json_list:
json_strings.append(json.dumps(item))
return json_strings
if __name__ == '__main__':
datainput = DataInput()
print('you are running data input with file path: ' + datainput.file_path)
print('data collection finished')