Skip to content

Commit db094c7

Browse files
committed
add export_entities to yaml for embedding
1 parent 034a888 commit db094c7

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/pyirk/core.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from rdflib import Literal
2020
import pydantic
2121
import re
22+
import json, yaml
2223

2324
from pyirk import auxiliary as aux
2425
from pyirk import settings
@@ -2867,3 +2868,43 @@ def is_subproperty(item: Item, parent_property: Item):
28672868
return True
28682869
else:
28692870
return is_subproperty(item.R17, parent_property)
2871+
2872+
def export_entities(path: str, to_file=True, uris=True):
2873+
d = {}
2874+
entities = [ds.items, ds.relations]
2875+
for entity in entities:
2876+
for k, v in entity.items():
2877+
if "a" in k.split("#")[-1]:
2878+
continue
2879+
out = v.R1.value + "\n"
2880+
for items in [v.get_relations().items(), v.get_inv_relations().items()]:
2881+
for rk, stmts in items:
2882+
if rk.endswith("#R1"):
2883+
continue
2884+
for stm in stmts:
2885+
for e in stm.relation_tuple:
2886+
# add uri
2887+
if uris and hasattr(e, "uri"):
2888+
out += f"'{e.uri} "
2889+
else:
2890+
out += "'"
2891+
# normal items
2892+
if hasattr(e, "R1"):
2893+
out += f"{e.R1.value}'"
2894+
# Literals
2895+
elif hasattr(e, "value"):
2896+
out += f"{e.value}'"
2897+
# other literals
2898+
elif isinstance(e, str):
2899+
out += f"{e}'"
2900+
# numbers and others
2901+
else:
2902+
out += f"{str(e)}'"
2903+
out += " "
2904+
out += "\n"
2905+
d[k] = out
2906+
# todo do we want uris in these statements?
2907+
if to_file:
2908+
with open(path, "w") as f:
2909+
yaml.dump(d, f)
2910+
return d

src/pyirk/script.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ def create_parser():
106106
metavar="reportconf-path",
107107
)
108108

109+
parser.add_argument(
110+
"-ee",
111+
"--export-entities",
112+
help="generate text file with all entities for embedding",
113+
metavar="reportconf-path",
114+
)
115+
109116
parser.add_argument(
110117
"-vis",
111118
"--visualize",
@@ -239,6 +246,8 @@ def main():
239246
core.script_main(args.inputfile)
240247
elif reportconf_path := args.generate_report:
241248
reportgenerator.generate_report(reportconf_path)
249+
elif export_path := args.export_entities:
250+
core.export_entities(export_path)
242251
elif key := args.visualize:
243252
if key == "__all__":
244253
visualization.visualize_all_entities(write_tmp_files=True)

0 commit comments

Comments
 (0)