This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneralizer.py
62 lines (50 loc) · 1.8 KB
/
generalizer.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
#!/usr/bin/python
#
# Copyright (c) 2014, Portavita BV Netherlands
class Generalizer:
levels = None
def __repr__(self):
return str(self.tree)
class NominalGeneralizer(Generalizer):
def __init__(self, tree):
self.tree = tree
self.levels = len(tree.itervalues().next())
def generalize(self, value, level):
return self.tree[value][level]
class IntervalGeneralizer(Generalizer):
def __init__(self, min, max, levels):
self.cache = {}
self.levels = levels
self.tree = {}
segments = 2 ** (levels - 3)
for l in range(1, levels - 1): # lowest and highest level are evaluated differently (see generalize())
delta = (max - min) / segments
self.tree[l] = [(min + s * delta, min + (s + 1) * delta) for s in xrange(segments)]
segments = segments / 2
def generalize(self, value, level):
try:
return self.cache[(value, level)]
except:
pass
if level == 0: # actual value on lowest level
self.cache[(value, level)] = value
return value
if level == self.levels - 1: # None on highest level
self.cache[(value, level)] = None
return None
result = None
for (min, max) in self.tree[level]:
if value >= min and value < max:
result = (min, max)
self.cache[(value, level)] = result
return self.cache[(value, level)]
if __name__ == '__main__':
trees = {}
trees['bp'] = IntervalGeneralizer(min=40, max=180, levels=4)
trees['agree'] = NominalGeneralizer({
'completely agree': ('completely agree', 'agree', 'agree or disagree'),
'mostly agree': ('mostly agree', 'agree', 'agree or disagree'),
'mostly disagree': ('mostly disagree', 'disagree', 'agree or disagree'),
'completely disagree': ('completely disagree', 'disagree', 'agree or disagree')})
print trees['bp'].generalize(112, 1)
print trees['agree'].generalize('mostly disagree', 1)