..

NQUADS -> TRIG … A Noddy Perl Script

I keep coming across nquads files, and libraptor doesn’t support this serialisation, it only supports quad-based TriG format. I knocked together a dirty perl file which will parse a nquads file to TriG if ever need be.

You can find the perl file on my site :

https://mmt.me.uk/blog/examples/perl/nquads_to_trig.pl

`
#!/usr/bin/perl
use strict;</p>

my $files = scalar(@ARGV);
if ($files != 1) {
  print "NQuads importer. ./nquads_importer nquads_file\n";
  exit();
}

my $input_filename = @ARGV[0];

open (INPUT,$input_filename) || die { print "Input file does not exist\n"};
open (OUTPUT,">$input_filename.trig") || die { print "Failed to open output file\n"};

my $line = "";
my $model_uri = "";
my $triple = "";
my $count = 0;
my %quads = ();

while () {
  $line = $_;
  if ($line =~ m/^(.*?)(<[^>]+>?)\s*\.$/) {
   $model_uri = $2;
   $triple = $1.".\n";

   $quads{$model_uri} .= $triple;

  } else {
   print ERROR "boo this nquad doesn't pass regex\n$line\n*************\n";
   }
  $count++;
}

foreach my $forth (keys %quads) {
  print OUTPUT "$forth { ".$quads{$forth}." }\n";
}

print "Finished\n";
close(INPUT);
close(OUTPUT);

# vi:set ts=8 sts=4 sw=4 et:
`