-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmindmup.py
48 lines (42 loc) · 1.54 KB
/
mindmup.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
import json
import logging
log = logging.getLogger(__name__)
class MindMup:
def __init__(self, json_file):
self.json_file = json_file
def parse(self):
try:
with open(self.json_file, 'r') as file:
data = json.load(file)
return self.extract_triples(data['ideas'])
except FileNotFoundError:
print(f"Error: JSON file '{self.json_file}' not found.")
return ""
def extract_triples(self, idea_dict, parent=None):
triples = ""
for key, idea in idea_dict.items():
object_ = idea['title']
if not object_:
log.info(f'untitled box encountered, skipping triple')
continue
if parent:
predicate = self.get_predicate(idea)
subject = parent
if not predicate: predicate = '->'
triples += "{} {} {}.\n".format(subject, predicate, object_ )
if 'ideas' in idea:
triples += (self.extract_triples(idea['ideas'], object_))
return triples
def get_predicate(self, idea):
if 'attr' in idea:
if 'parentConnector' in idea['attr']:
label = idea['attr']['parentConnector'].get('label', '->')
return label
else:
return '->'
# Usage
if __name__ == "__main__":
json_file = 'mindmup/tutorial.mup' # Replace with the path to your JSON file
mindmup_parser = MindMup(json_file)
triples = mindmup_parser.parse()
print(triples)