You have two options with raptor:
Option 1: Use the rapper command line, it is very fast. Below is the function below in python to complete the command. The -c option is to simply count the number of triples. The lang parameter is just an option to specify ntriples of the RDF, rdfxml, turtle, ... format. The function checks the return code and throws an exception in case something goes wrong.
def rapper_count(f,lang): p=subprocess.Popen(["rapper","-i",lang,"-c",f],stdout=subprocess.PIPE,stderr=subprocess.PIPE) output, err = p.communicate() ret = p.poll() if ret <> 0: raise Exception, "Error parsing with rapper\n%s"%err return int(err.split()[-2])
Option 2: Use Python bindings. The following will work:
import RDF test_file = "/some/file" uri=RDF.Uri(string="file:"+test_file) parser=RDF.Parser(name="turtle") if parser is None: raise Exception("Failed to create RDF.Parser raptor") count=0 for s in parser.parse_as_stream(uri,uri): count=count+1 print "Parsing added",count,"statements"
This code has been extracted from example.py , check it out and you will see more examples.
source share