-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_tree.py
More file actions
67 lines (54 loc) · 1.85 KB
/
parse_tree.py
File metadata and controls
67 lines (54 loc) · 1.85 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
import pprint
import numpy
derivations= [
['S'],
['X', 'U'],
['a', 'U'],
['a', 'T', 'Y'],
['a', 'X', 'Y', 'Y'],
['a', 'a', 'Y', 'Y'],
['a', 'a', 'b', 'Y'],
['a', 'a', 'b', 'b']
]
# get max length of largest list in derivations variable
max_length = max(len(l) for l in derivations)
# create vertical lists
vertical_lists = []
for i in range(max_length):
vertical_lists.append([l[i] if i < len(l) else None for l in derivations])
# we get the following:
[
['S', 'X', 'a', 'a', 'a', 'a', 'a', 'a'],
[None, 'U', 'U', 'T', 'X', 'a', 'a', 'a'],
[None, None, None, 'Y', 'Y', 'Y', 'b', 'b'],
[None, None, None, None, 'Y', 'Y', 'Y', 'b']
]
cleaned_lists = []
for l in vertical_lists:
cleaned_list = [l[0]]
for i in range(1, len(l)):
if l[i] != l[i - 1]:
cleaned_list.append(l[i])
else:
cleaned_list.append(None)
cleaned_lists.append(cleaned_list)
converted_list = [
['S', 'X', 'a', None, None, None, None, None],
[None, 'U', None, 'T', 'X', 'a', None, None],
[None, None, None, 'Y', None, None, 'b', None],
[None, None, None, None, 'Y', None, None, 'b']
]
# now if there are 'None' values after the first non None value in the list remove it and move all values to the left
for list in converted_list:
index = next((i for i, value in enumerate(list) if value is not None), None)
if index is not None:
# Remove subsequent None values
list = list[:index+1] + [value for value in list[index+1:] if value is not None]
print(list)
# rotate the list and convert to tuples
parse_tree = list(zip(*converted_list[::1]))
# remove None values to clean up list:
parse_tree = [[val if val is not None else '' for val in row] for row in parse_tree]
# remove empty string values if
# rotate the list and we get the following
pprint.pprint(parse_tree)