Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Haystack Inference #63

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions brickschema/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def infer_model(self, model):

entities = model["rows"]
# index the entities by their ID field
entities = {e["id"].replace('"', ""): {"tags": e} for e in entities}
entities = {_extract_id(e).replace('"', ""): {"tags": e} for e in entities}
# TODO: add e['dis'] for a descriptive label?
brickgraph = Graph(load_brick=False)

Expand All @@ -744,7 +744,7 @@ def infer_model(self, model):
# translate tags
entity_tagset = list(self._translate_tags(marker_tags))

equip_ref = entity["tags"].get("equipRef")
equip_ref = _extract_ref(entity["tags"], "equipRef")
# infer tags for single entity
triples, _ = self.infer_entity(
entity_tagset, identifier=entity_id, equip_ref=equip_ref
Expand All @@ -760,7 +760,8 @@ def infer_model(self, model):
if "equipRef" not in relships:
continue
reffed_equip = (
relships["equipRef"].replace(" ", "_").replace('"', "") + "_equip"
_extract_ref(relships, "equipRef").replace(" ", "_").replace('"', "")
+ "_equip"
)
if self._BLDG[point_entity_id] in brickgraph.nodes:
triple = (
Expand Down Expand Up @@ -803,3 +804,23 @@ def _to_tag_case(x):
x (str): transformed string
"""
return x[0].upper() + x[1:]


def _extract_id(entity):
"""
Extracts the 'id' field from Haystack entity
"""
return _extract_ref(entity, "id")


def _extract_ref(entity, ref):
"""
Extracts the indicated 'ref' field from Haystack entity
"""
if ref not in entity:
return None
if isinstance(entity[ref], str):
return entity[ref].replace('"', "") # strip quotes
elif isinstance(entity[ref], dict):
return entity[ref]["val"].replace('"', "")
return None