/
Parser.py
229 lines (209 loc) · 8.79 KB
/
Parser.py
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
import pprint
class Parser:
"""
Parses input to create Projects, ontologies, properties and to link them appropriately
"""
##TODO handle empty entries, especially in pretty_lines
project_data = {}
def __init__(self, file: str):
self.file = open(file, 'r',encoding="utf8",)
self.data=self.prepare_lines()
self.already_read = list()
def prepare_lines(self):
lines = self.file.readlines()
i = 0;
while i<len(lines) :
comment_cut = lines[i].find("//")
if comment_cut != -1:
lines[i]=lines[i][0:comment_cut]
lines[i]= lines[i] + "\n"
i=i+1
return lines
def pretty_line(self, line, to_cut):
start = line.find(to_cut)
end = start + len(to_cut)
new_line = line[0:start] + line[end+1:len(line)]
if not new_line == "":
while (new_line[0]==' '):
new_line = new_line[1:len(line)]
while (new_line[-1]==' ' or new_line[-1]=='\n'):
new_line = new_line[0:len(new_line)-1]
return new_line
def occurences(self,string,char):
return len(string)-len(string.replace(char,""))
def find_end(self, start):
lines = self.data
if (lines[start].find("{")==-1):
print("Error, no { in starting line ")
print(start)
return -1
bracketsCount = self.occurences(lines[start],"{")
i = start + 1
try:
while (bracketsCount>0 and i<len(lines)-1):
bracketsCount = bracketsCount + self.occurences(lines[i],"{")-self.occurences(lines[i],"}")
i=i+1
except IndexError:
print("Ended parsing with open parentheses")
return i-1
def read_description(self, start, end):
description={}
current_lang= ""
while start < end:
if self.data[start].find("Language")!=-1:
string = self.pretty_line(self.data[start],"Language")
string = string[0:string.find(",")]
description[string]={}
current_lang=string
if self.data[start].find("Value")!=-1:
string = self.data[start][self.data[start].find("Value"):len(self.data[start])]
description[current_lang]= self.pretty_line(string,"Value")
self.already_read.append(start)
start = start+1
return description
def read_normal_list(self, start, end):
i = start + 1
list=[]
while i < end:
string = self.pretty_line(self.data[i],"\n")
if string!="":
list.append(string)
self.already_read.append(i)
i=i+1
return list
def read_multiple_choice(self, start, end):
i = start + 1
list = {}
list['Name'] = ""
list['Nodes'] = []
while i < end:
string = self.data[i]
if string.find("Name")!=-1:
list["Name"]= self.pretty_line(string, "Name")
if string.find("Nodes")!=-1:
list['Nodes']=self.read_normal_list(i,self.find_end(i))
self.already_read.append(i)
i = i + 1
return list
#TODO maybe write Keywords in ALL CAPS to avoid usage of users.
def read_ontology(self,start,end):
ontology_data = {}
ontology_data["Resources"]=[]
i = start-1
while i<end:
i = i + 1
if i in self.already_read:
continue
line = self.data[i]
if (line.find("Resource")!=-1):
ontology_data["Resources"].append(self.read_resource(i,self.find_end(i)))
continue
if (line.find("Name")!=-1):
ontology_data["Name"] = self.pretty_line(line,"Name")
if (line.find("Label")!=-1):
ontology_data["Label"] = self.pretty_line(line,"Label")
self.already_read.append(i)
return ontology_data
def read_resource(self,start,end):
resource_data = {}
resource_data["Properties"]=[]
i= start-1
while i <end:
i = i + 1
if i in self.already_read:
continue
line = self.data[i]
if line.find("Property")!=-1:
resource_data["Properties"].append(self.read_property(i,self.find_end(i)))
continue
if line.find("Name")!=-1:
resource_data["Name"]= self.pretty_line(line, "Name")
if line.find("Super classes")!=-1:
resource_data["Super classes"] = self.read_normal_list(i,self.find_end(i))
if line.find("Labels")!=-1:
resource_data["Labels"] = self.read_description(i,self.find_end(i))
if line.find("Comments")!=-1:
resource_data["Comments"] = self.read_description(i,self.find_end(i))
self.already_read.append(i)
resource_data["Super classes"] = resource_data["Super classes"][0] # bad hack! TODO: make it better
resource_data["Comments"] = None
return resource_data
def read_attributes(self,start, end):
i = start
attributes_data=[]
while i<end-1:
i=i+1
line = self.pretty_line(self.data[i],"\n")
if i in self.already_read:
continue
attributes_data.append(line)
# j = line.find("=")
# if j!=-1:
# one=line[0:j]
# two=line[j+1:len(line)]
# while (one[0]==' '):
# one = one[1:len(one)]
# while (two[0]==' '):
# two=two[1:len(two)]
# while (one[-1]==' ' or one[-1]== '\n'):
# one = one[0:len(one)-1]
# while (two[-1] == ' ' or two[-1] == '\n'):
# two = two[0:len(two) - 1]
# attributes_data[one]=two
self.already_read.append(i)
return attributes_data
def read_property(self, start, end):
property_data={}
i = start-1
while i <end :
i = i + 1
line = self.data[i]
if i in self.already_read:
continue
if line.find("Name")!=-1:
property_data["Name"]= self.pretty_line(line, "Name")
if line.find("Super Properties")!=-1:
property_data["Super Properties"]=self.read_normal_list(i,self.find_end(i))
if line.find("Labels")!=-1:
property_data["Labels"]=self.read_description(i,self.find_end(i))
if line.find("Object")!=-1:
property_data["Object"] = self.pretty_line(line, "Object")
if line.find("Cardinality")!=-1:
property_data["Cardinality"] = self.pretty_line(line, "Cardinality")
if line.find("GUI Element")!=-1:
property_data["GUI Element"] = self.pretty_line(line, "GUI Element")
if line.find("GUI Attributes")!=-1:
property_data["GUI Attributes"]= self.read_attributes(i,self.find_end(i))
self.already_read.append(i)
return property_data
def read_project(self):
lines = self.data
i = -1
self.project_data["Lists"]=[]
while i<len(self.data)-1 :
i = i + 1
if i in self.already_read:
continue
line = self.data[i]
if line.find("Ontology")!=-1:
self.project_data["Ontology"] = self.read_ontology(lines.index(line),self.find_end(lines.index(line)))
continue
if (line.find("Short code")!=-1):
self.project_data["Short code"] = self.pretty_line(line,"Short code")
if (line.find("Short name") != -1):
self.project_data["Short name"] = self.pretty_line(line,"Short name")
if (line.find("Long name") != -1):
self.project_data["Long name"] = self.pretty_line(line, "Long name")
if (line.find("Description") != -1):
self.project_data["Description"]=self.read_description(lines.index(line),self.find_end(lines.index(line)))
if line.find("Keywords")!=-1:
self.project_data["Keywords"] = self.read_normal_list(lines.index(line),self.find_end(lines.index(line)))
if line.find("Logo")!=-1:
self.project_data["Logo"] = self.read_normal_list(lines.index(line),self.find_end(lines.index(line)))
if line.find("List")!=-1 :
self.project_data['Lists'].append(self.read_multiple_choice(i,self.find_end(i)))
self.already_read.append(i)
self.project_data["Logo"] = None # no logos yet!
return self.project_data
#parser = Parser("ubkvp-onto")
#pprint.pprint(parser.read_project())