/
Structure.py
41 lines (38 loc) · 1.75 KB
/
Structure.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
import Attribute
class Structure:
def __init__(self,path):
self.classAttribute = None
self.attributes = []
self.readFile(path)
if len(self.attributes) == 0:
raise Exception("There must be attributes")
if self.classAttribute is None:
raise Exception("There must be a class attribute")
def readFile(self,path):
lines = [line.rstrip('\n') for line in open(path)]
for line in lines:
tokens = line.split(' ')
if len(tokens) < 3:
raise Exception("There are not enough information about an certain attribute")
if tokens[0] != "@ATTRIBUTE":
raise Exception("First token in each line must be <@ATTRIBUTE>")
name = tokens[1]
last_token = tokens[len(tokens)-1]
if tokens[2] == "NUMERIC":
newAttribute = Attribute.Numeric(name)
elif tokens[2][0] == '{' and last_token[len(last_token)-1] == '}':
categoriesLine = line.split('{')[1].split('}')[0]
categories = []
for x in categoriesLine.split(','):
categories.append(Attribute.Value(x))
if len(categories) < 2:
raise Exception("Each categorical attribute must have at least 2 categories")
newAttribute = Attribute.Category(name,categories)
else:
raise Exception("Invalid structure")
if newAttribute.name != "class":
self.attributes.append(newAttribute)
else:
if self.classAttribute is not None:
raise Exception("There can not be more than 1 class attribute")
self.classAttribute = newAttribute