-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from pietercolpaert/development
Hardf Parser and Writer functions
- Loading branch information
Showing
13 changed files
with
4,818 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
language: php | ||
php: | ||
- "7.0" | ||
- '5.6' | ||
- '7.0' | ||
- '7.1' | ||
- nightly | ||
sudo: false | ||
env: | ||
before_script: | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/php | ||
<?php | ||
/** Converts TriG, Turtle, N3, N-QUADS or N-TRIPLES input to TriG, Turtle, N-QUADS or N-TRIPLES*/ | ||
include_once(__DIR__ . '/../vendor/autoload.php'); | ||
use pietercolpaert\hardf\TriGParser; | ||
use pietercolpaert\hardf\TriGWriter; | ||
$informat = "turtle"; | ||
if (isset($argv[1])) | ||
$informat = $argv[1]; | ||
|
||
$outformat = "n-triples"; | ||
if (isset($argv[2])) | ||
$outformat = $argv[2]; | ||
|
||
$writer = new TriGWriter(["format" => $outformat]); | ||
$parser = new TriGParser(["format" => $informat], function ($error, $triple) use (&$writer) { | ||
if (!isset($error) && !isset($triple)) { //flags end | ||
echo $writer->end(); | ||
} else if (!$error) { | ||
$writer->addTriple($triple); | ||
echo $writer->read(); | ||
} else { | ||
fwrite(STDERR, $error->getMessage() . "\n"); | ||
} | ||
}); | ||
|
||
while ($line = fgets(STDIN)) { | ||
$parser->parseChunk($line); | ||
} | ||
$parser->end(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/php | ||
<?php | ||
/** Validates TriG, Turtle, N3, N-QUADS or N-TRIPLES input */ | ||
include_once(__DIR__ . '/../vendor/autoload.php'); | ||
use pietercolpaert\hardf\TriGParser; | ||
use pietercolpaert\hardf\TriGWriter; | ||
$format = "trig"; | ||
if (isset($argv[1])) | ||
$format = $argv[1]; | ||
$parser = new TriGParser(["format" => $format]); | ||
$errored = false; | ||
$finished = false; | ||
$tripleCount = 0; | ||
$line = true; | ||
while (!$finished && $line) { | ||
try { | ||
$line = fgets(STDIN); | ||
if ($line) | ||
$tripleCount += sizeof($parser->parseChunk($line)); | ||
else { | ||
$tripleCount += sizeof($parser->end()); | ||
$finished = true; | ||
} | ||
} catch (\Exception $e) { | ||
echo $e->getMessage() . "\n"; | ||
$errored = true; | ||
} | ||
} | ||
if (!$errored) { | ||
echo "Parsed " . $tripleCount . " triples successfully.\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
include_once(__DIR__ . '/../vendor/autoload.php'); | ||
use pietercolpaert\hardf\TriGParser; | ||
use pietercolpaert\hardf\TriGWriter; | ||
|
||
echo "--- First, simple implementation ---\n"; | ||
$parser = new TriGParser([]); | ||
$writer = new TriGWriter(["format"=>"trig"]); | ||
$triples = $parser->parse("(<x>) <a> <b>. <b> <c> \"\"\"\n\"\"\"."); | ||
$writer->addTriples($triples); | ||
echo $writer->end(); | ||
|
||
//Or, option 2, the streaming version | ||
echo "--- Second streaming implementation with callbacks ---\n"; | ||
$parser = new TriGParser(); | ||
$writer = new TriGWriter(["format"=>"trig"]); | ||
$error = null; | ||
$parser->parse("@prefix ex: <http://ex.org/> . <http://A> <https://B> <http://C> <http://G> . <A2> <https://B2> <http://C2> <http://G3> . ex:s ex:p ex:o . ", function ($e, $triple) use (&$writer) { | ||
if (!$e && $triple) | ||
$writer->addTriple($triple); | ||
else if (!$triple) | ||
echo $writer->end(); | ||
else | ||
echo "Error occured: " . $e; | ||
}, function ($prefix, $iri) use (&$writer) { | ||
$writer->addPrefix($prefix,$iri); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
include_once(__DIR__ . '/../vendor/autoload.php'); | ||
use pietercolpaert\hardf\TriGWriter; | ||
|
||
//Add prefixes in the constructor | ||
$writer = new TriGWriter([ | ||
"prefixes" => [ | ||
"schema" =>"http://schema.org/", | ||
"dct" =>"http://purl.org/dc/terms/", | ||
"geo" =>"http://www.w3.org/2003/01/geo/wgs84_pos#", | ||
"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | ||
"rdfs"=> "http://www.w3.org/2000/01/rdf-schema#" | ||
] | ||
]); | ||
|
||
$writer->addPrefix("ex","http://example.org/"); | ||
$writer->addTriple("schema:Person","dct:title","\"Person\"@en","http://example.org/#test"); | ||
$writer->addTriple("schema:Person","schema:label","\"Person\"@en","http://example.org/#test"); | ||
$writer->addTriple("ex:1","dct:title","\"Person1\"@en","http://example.org/#test"); | ||
$writer->addTriple("ex:1","http://www.w3.org/1999/02/22-rdf-syntax-ns#type","schema:Person","http://example.org/#test"); | ||
$writer->addTriple("ex:2","dct:title","\"Person2\"@en","http://example.org/#test"); | ||
$writer->addTriple("schema:Person","dct:title","\"Person\"@en","http://example.org/#test2"); | ||
echo $writer->end(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
include_once(__DIR__ . '/../vendor/autoload.php'); | ||
use pietercolpaert\hardf\TriGParser; | ||
|
||
if (sizeof($argv) !== 2) { | ||
echo 'Usage: parser-perf.php filename'; | ||
exit; | ||
} | ||
|
||
$filename = $argv[1]; | ||
$base = 'file://' . $filename; | ||
|
||
$TEST = microtime(true); | ||
|
||
$count = 0; | ||
$parser = new TriGParser([ "documentIRI" => $base ]); | ||
$callback = function ($error, $triple) use (&$count, $TEST, $filename) { | ||
if ($triple) { | ||
$count++; | ||
} | ||
else { | ||
echo '- Parsing file ' . $filename . ': ' . (microtime(true) - $TEST) . "s\n"; | ||
echo '* Triples parsed: ' . $count . "\n"; | ||
echo '* Memory usage: ' . (memory_get_usage() / 1024 / 1024) . "MB\n"; | ||
} | ||
}; | ||
|
||
$handle = fopen($filename, "r"); | ||
if ($handle) { | ||
while (($line = fgets($handle)) !== false) { | ||
$parser->parseChunk($line, $callback); | ||
} | ||
$parser->end($callback); | ||
fclose($handle); | ||
} else { | ||
// error opening the file. | ||
echo "File not found " . $filename; | ||
} |
Oops, something went wrong.