Feature/tidy kg load save (#198)

* Clean exit on ctrl-C
* More functionality in dump
* Dump some metadata
This commit is contained in:
cybermaggedon 2024-12-06 15:16:09 +00:00 committed by GitHub
parent 55c5c398b6
commit fd3db3c925
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 177 additions and 37 deletions

View file

@ -10,7 +10,7 @@ import msgpack
import sys
import argparse
def run(input_file):
def dump(input_file, action):
with open(input_file, 'rb') as f:
@ -19,6 +19,43 @@ def run(input_file):
for unpacked in unpacker:
print(unpacked)
def summary(input_file, action):
vector_dim = None
triples = set()
max_records = 1000000
with open(input_file, 'rb') as f:
unpacker = msgpack.Unpacker(f, raw=False)
rec_count = 0
for msg in unpacker:
if msg[0] == "ge":
vector_dim = len(msg[1]["v"][0])
if msg[0] == "t":
for elt in msg[1]["m"]["m"]:
triples.add((
elt["s"]["v"],
elt["p"]["v"],
elt["o"]["v"],
))
if rec_count > max_records: break
rec_count += 1
print("Vector dimension:", vector_dim)
for t in triples:
if t[1] == "http://www.w3.org/2000/01/rdf-schema#label":
print("-", t[2])
def main():
parser = argparse.ArgumentParser(
@ -32,9 +69,24 @@ def main():
help=f'Input file'
)
parser.add_argument(
'-s', '--summary', action="store_const", const="summary",
dest="action",
help=f'Show a summary'
)
parser.add_argument(
'-r', '--records', action="store_const", const="records",
dest="action",
help=f'Dump individual records'
)
args = parser.parse_args()
run(**vars(args))
if args.action == "summary":
summary(**vars(args))
else:
dump(**vars(args))
main()