File size: 2,496 Bytes
7375cc5 e908d09 7375cc5 e908d09 7375cc5 e908d09 7375cc5 e908d09 7375cc5 e908d09 |
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 |
# filename: isco.py
from collections import defaultdict
import csv
def create_hierarchy_dict(filename):
"""
Creates a dictionary where keys are nodes and values are sets of parent nodes representing the hierarchy of the ISCO-08 codes from the "unit" column of the isco_structure.csv file.
Args:
- filename: A string representing the path to the CSV file containing the ISCO-08 codes and their hierarchy.
Returns:
- A dictionary where keys are ISCO-08 unit codes and values are sets of their parent codes.
"""
isco_hierarchy = {}
with open(filename, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# Extract unit group level code (4 digits)
unit_code = row["unit"].zfill(4)
# Extract the parent code for the unit group level, which is the minor group level (3 digits)
parent_code = unit_code[:3]
# Add the unit code to the hierarchy with its parent code
isco_hierarchy[unit_code] = {parent_code}
# Additionally, we can add the parent's parent codes if needed
# For example, the major group level (1 digit) and sub-major group level (2 digits)
major_code = unit_code[0]
sub_major_code = unit_code[:2]
isco_hierarchy[unit_code].update({major_code, sub_major_code})
return isco_hierarchy
def create_hierarchy_tree(hierarchy_dict: dict) -> tuple:
"""
Builds the hierarchy tree and a mapping from name to ISCO code.
Args:
- hierarchy_json: A dictionary representing the hierarchical structure.
Returns:
- tree: A dictionary representing the hierarchical structure.
- code_to_node: A dictionary mapping from ISCO code to node name.
"""
tree = defaultdict(lambda: {"children": [], "parent": None})
code_to_node = {}
def add_node(parent_code, node):
code = node["name"].split("=")[0].strip()
code_to_node[code] = node["name"]
tree[code]["parent"] = parent_code
if parent_code:
tree[parent_code]["children"].append(code)
for child in node.get("children", []):
add_node(code, child)
add_node(None, hierarchy_dict) # Root node has no parent
return tree, code_to_node
# Example usage:
# hierarchy_dict = create_hierarchy("ISCO_structure.csv")
# tree, code_to_node = create_hierarchy_tree(hierarchy_dict)
# print(hierarchy)
# print(code_to_node)
# print(tree)
|