-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_tabs.py
More file actions
246 lines (186 loc) · 4.81 KB
/
parse_tabs.py
File metadata and controls
246 lines (186 loc) · 4.81 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import csv
import json
import os.path
import cgitb
cgitb.enable()
class TaskHTN:
def __init__(self, task_name=None, order=None, action_set=[]):
self._children = []
self._values = {'name': task_name, 'actions': action_set, 'order_invariant': order, 'support_actions': []}
def add_child(self,childHTN):
self._children.append(childHTN)
def remove_child(self,child):
if child in self._children:
self._children.remove(child)
return True
else:
return False
@property
def children(self):
return self._children
@property
def values(self):
return self._values
def is_order_invariant(self):
return self._values['order_invariant'] is True
def is_order_agnostic(self):
return not self.is_order_invariant()
def to_json(self):
return json.dumps(self.to_dict())
def to_dict(self):
self_dict = {'values': self._values, 'children': []}
if len(self._children) > 0:
self_dict['children'] = [c.to_dict() for c in self._children]
return self_dict
def get_path_to_node(target_node, path=[]):
for c in self._children:
next_step = get_path(c, path)
if next_step is not None:
path.extend(next_step)
break
if target_node == self:
return [self]
elif length(path) > 0:
return path
return None
def save(self, filename, overwrite=False):
if os.path.isfile(filename):
if overwrite is False:
return False
print "Overwriting existing file..."
f = open(filename, 'w')
my_json = self.to_json()
f.write(my_json)
f.close()
def load(filename):
if os.path.isfile(filename) is False:
return None
htn_json = None
with open(filename,'r') as f:
htn_json = f.read()
return TaskHTN.from_json(htn_json)
def from_json(json):
root = TaskHTN(None)
root._values = json['values']
for c in json['children']:
root.add_child(TaskHTN.from_json(c))
return root
class State:
def __init__(self, feature_vector):
self._features = feature_vector
def num_of_spaces(string):
spaces = 0
for characters in string:
if bool(characters.isspace()) == True:
spaces += 1
else:
break
return spaces
def retrieve_text(row):
global rows
print "rows"
print rows
r = rows[row].strip()
print "rows[row]"
print rows[row]
print "r"
print r
if r[0] == '*' or r[0] == '-':
r = r.replace('*','')
r = r.replace('-','')
return r
def ordering_class(row):
global rows
prefix = rows[row].strip()
if prefix[0] == '*':
prefix = "clique"
return prefix
elif prefix[0] == '-':
prefix = "chain"
return prefix
else:
prefix = "primitive"
return prefix
def pop_stack_sib(spaces_in_current_row):
global num_spaces_per_row
global stack
for i in range(1, len(num_spaces_per_row)):
print num_spaces_per_row, spaces_in_current_row, i
if spaces_in_current_row >= num_spaces_per_row[-i]:
stack.pop()
num_spaces_per_row.pop()
print "popped"
break
def pop_stack_parent(spaces_in_current_row):
global num_spaces_per_row
global stack
for i in range(1, len(num_spaces_per_row)):
print num_spaces_per_row, spaces_in_current_row, i
if spaces_in_current_row == num_spaces_per_row[-i]:
to_pop = i
for x in range(to_pop):
# print stack[-1]
# print "look"
stack.pop()
num_spaces_per_row.pop()
print "popped"
break
def run():
crs = open("sampleText.txt", "r")
rows = crs.readlines()
row_at = 0
num_rows = len(rows)
num_spaces_per_row = []
stack = [None]
indent_leval = None
p = None
while(True):
if row_at < num_rows:
row = rows[row_at]
last_row = row_at - 1
prev_row = rows[last_row]
clean_row = retrieve_text(row_at)
order = ordering_class(row_at)
print order
new_node = TaskHTN(clean_row, order)
p = stack[-1]
if row_at > 0:
if p == None:
p = new_node
indent_leval = 0
elif num_of_spaces(prev_row) < num_of_spaces(row): #child
p.children.append(new_node)
elif num_of_spaces(prev_row) == num_of_spaces(row): #sibling
pop_stack_sib(num_of_spaces(row))
p = stack[-1]
if p == None:
p = new_node
else:
p.children.append(new_node)
elif num_of_spaces(prev_row) > num_of_spaces(row): #parent
pop_stack_parent(num_of_spaces(row))
p = stack[-1]
if p == None:
p = new_node
else:
p.children.append(new_node)
else:
if p == None:
p = new_node
else:
print '\n'
#print stack[1].to_json()
json_array = stack[1].to_json()
print "Content-Type: application/json"
print "\n"
print "\n"
print stack[1].to_json()
#print json.dumps(json_array, indent=1)
print "\n"
#return json.dumps(json_array)
print '\n'
break
stack.append(new_node)
num_spaces_per_row.append(num_of_spaces(row))
row_at += 1
print "\n\n"