-
Notifications
You must be signed in to change notification settings - Fork 16
/
lcc.py
98 lines (77 loc) · 2.51 KB
/
lcc.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
from portality.dao import DomainObject
class LCC(DomainObject):
__type__ = "lcc"
def term_path(self, term):
def dive(node, path):
if node.get("name") == term:
path.append(term)
return True
if "children" not in node:
return False
path.append(node.get("name"))
for n in node.get("children", []):
found = dive(n, path)
if found:
return True
path.pop()
return False
roots = self.data.get("children", [])
for r in roots:
path = []
found = dive(r, path)
if found:
return path
return None
def pathify(self, term, path_separator=": "):
"""
Convert the given term into a string containing the path from the root via the parents to the term
:param term:
:param path_separator:
:return:
"""
path = self.term_path(term)
if path is not None:
return path_separator.join(path)
return None
def expand_codes(self, code):
def dive(node, path):
if node.get("code") == code:
path.append(code)
return True
if "children" not in node:
return False
path.append(node.get("code"))
for n in node.get("children", []):
found = dive(n, path)
if found:
return True
path.pop()
return False
roots = self.data.get("children", [])
for r in roots:
path = []
found = dive(r, path)
if found:
return path
return []
def longest(self, paths):
"""
Returns only the longest paths from the provided classification paths (created with pathify)
:param paths:
:return:
"""
keep = []
for candidate in paths:
trip = False
for other in paths:
# if the candidate is a substring of another, longer, string, hit the tripwire
if candidate in other and len(candidate) != len(other):
trip = True
break
if not trip:
if candidate not in keep:
keep.append(candidate)
return keep
def save(self, **kwargs):
self.set_id("lcc")
return super(LCC, self).save(**kwargs)