Last active
April 30, 2021 16:54
-
-
Save ftrain/f9ed5ee4d14984ec33aaa0711c1b4a12 to your computer and use it in GitHub Desktop.
Convert n3 triples to SQL in SQLite3, your own Very Shitty Triplestore.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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