Skip to content

Instantly share code, notes, and snippets.

@ftrain
Last active April 30, 2021 16:54
Show Gist options
  • Save ftrain/f9ed5ee4d14984ec33aaa0711c1b4a12 to your computer and use it in GitHub Desktop.
Save ftrain/f9ed5ee4d14984ec33aaa0711c1b4a12 to your computer and use it in GitHub Desktop.
Convert n3 triples to SQL in SQLite3, your own Very Shitty Triplestore.
#!/usr/bin/perl
while (<>) {
s/'/''/g;
/^([^\s]+)\s*([^\s]+)\s*(.+)\s+\.\s*$/;
my $s = $1;
my $p = $2;
my $o = $3;
$s=~s/[<>]//g;
$p=~s/[<>]//g;
# Is it an object literal?
if ($o=~/^\"/) {
$o=~s/"//g;
print "INSERT INTO triples VALUES ('$s', '$p', NULL, '$o');\n";
}
else {
$o=~s/[<>]//g;
print "INSERT INTO triples VALUES ('$s', '$p', '$o', NULL);\n";
}
}
CREATE TABLE triples (
s TEXT NOT NULL,
p TEXT NOT NULL,
o TEXT DEFAULT NULL,
olit TEXT DEFAULT NULL
);
-- also add a bunch of indexes especially around p and o
# something like this, I can't remember exactly
sqlite3 triples < triples.sql
cat n3file.n3 | n3tosql.pl | sqlite triples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment