get parent-children relationships from given hierar_taxonomy hierar_taxonomy: parent_label \t child_label_0 \t child_label_1 \n
(hierar_taxonomy, label_map)
| 142 | |
| 143 | |
| 144 | def get_hierar_relations(hierar_taxonomy, label_map): |
| 145 | """ get parent-children relationships from given hierar_taxonomy |
| 146 | hierar_taxonomy: parent_label \t child_label_0 \t child_label_1 \n |
| 147 | """ |
| 148 | hierar_relations = {} |
| 149 | new_label_map = {} |
| 150 | for label_path, idx in label_map.items(): |
| 151 | for label in label_path.split('--'): |
| 152 | new_label_map[label] = new_label_map.get(label, idx) |
| 153 | with cs.open(hierar_taxonomy, "r", "utf8") as f: |
| 154 | for line in f: |
| 155 | line_split = line.strip("\n").split("\t") |
| 156 | parent_label, children_label = line_split[0], line_split[1:] |
| 157 | if parent_label not in new_label_map: |
| 158 | continue |
| 159 | parent_label_id = new_label_map[parent_label] |
| 160 | children_label_ids = [new_label_map[child_label] \ |
| 161 | for child_label in children_label if child_label in new_label_map] |
| 162 | hierar_relations[parent_label_id] = children_label_ids |
| 163 | return hierar_relations |