buildtree.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from utils.fileio import load_json
  2. def switch_dict(_lattice):
  3. data_type_dict = load_json('lattices/datatype_dictionary.json')
  4. purpose_dict = load_json('lattices/purpose_dictionary.json')
  5. _data_type, _purpose = _lattice['dataType'], _lattice['purpose']
  6. datatype_gen = generate(_data_type, data_type_dict)
  7. purpose_gen = generate(_purpose, purpose_dict)
  8. return {"dataType": datatype_gen, "purpose": purpose_gen}
  9. def generate(_datatype, datatype_dictionary, datatype_gen=None, prefix=None):
  10. if datatype_gen is None:
  11. datatype_gen = dict()
  12. if prefix is None:
  13. prefix = ""
  14. for key in _datatype.keys():
  15. prefix_son = key if prefix == "" else (prefix + '/' + key)
  16. if isinstance(_datatype[key], dict):
  17. datatype_gen = generate(_datatype[key], datatype_dictionary, datatype_gen, prefix_son)
  18. elif len(_datatype[key]) == 0:
  19. if key in datatype_dictionary.keys():
  20. datatype_gen[key] = {"path": prefix_son, "abbr": datatype_dictionary[key]["abbr"]}
  21. else:
  22. datatype_gen[key] = {"path": prefix_son, "abbr": [key]}
  23. else:
  24. for _type in _datatype[key]:
  25. prefix_grandson = prefix_son + '/' + _type
  26. if _type in datatype_dictionary.keys():
  27. datatype_gen[_type] = {"path": prefix_grandson, "abbr": datatype_dictionary[_type]["abbr"]}
  28. else:
  29. datatype_gen[_type] = {"path": prefix_grandson, "abbr": [_type]}
  30. return datatype_gen
  31. if __name__ == '__main__':
  32. data_type = load_json('datatype.json')
  33. purpose = load_json('purpose.json')
  34. lattice = {'dataType': data_type, 'purpose': purpose}
  35. switch_dict(lattice)