| 60 | |
| 61 | #using bfs to create the inheritance dictionary: |
| 62 | class Hierarchy: |
| 63 | # class Constructor |
| 64 | def __init__(self): |
| 65 | #define dictionary of list. This will store the hierarchy |
| 66 | self.hierarchy = defaultdict(list) |
| 67 | |
| 68 | def addEdge(self,u,v): |
| 69 | self.hierarchy[u].append(v) |
| 70 | |
| 71 | # Function to compute BFS |
| 72 | def BreadthFirstSearch(self, top_type): |
| 73 | visited = {} |
| 74 | for key in self.hierarchy: |
| 75 | visited[key] = False |
| 76 | |
| 77 | # BFS waiting buffer |
| 78 | buffer = [] |
| 79 | buffer.append(top_type) |
| 80 | visited[top_type] = True |
| 81 | while buffer: |
| 82 | top_type = buffer.pop(0) |
| 83 | for i in self.hierarchy[top_type]: |
| 84 | if visited.get(i, "None") == False: |
| 85 | buffer.append(i) |
| 86 | visited[i] = True |
| 87 | else: |
| 88 | visited[i] = True |
| 89 | return visited |
| 90 | |
| 91 | |
| 92 | SCHEMA_FILE_PATH = r"../yago/yago_original/yago-wd-schema.nt" #contains labels and some types too |