The NT parser follows a paradigm of using a "sink" (not a sink vertex) to store triples as they are parsed. I believe the tokens you are looking for are actually triples because the default NT Parser uses the NTriplesParser.
You could use the same method as the example below to override NTSink.
This example will load up a test NT formatted file then print
a line of text each time a line is parsed. Instead of printing a line, you could execute the method you are missing.
example.py
: Requires a file in the same directory named ./anons-01.nt
from rdflib.plugins.parsers.ntriples import NTriplesParser, Sink
# The NTriplesParser is what is used for a format="nt" parsing as found:
# https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/rdflib/plugins/parsers/nt.py#L2
# Example NT file from:
# https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/test/nt/anons-01.nt
class StreamSink(Sink):
"""
A sink is used to store the results of parsing, this almost matches the sink
example shown in ntriples:
https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/rdflib/plugins/parsers/ntriples.py#L43
"""
def triple(self, s, p, o):
self.length += 1
print "Stream of triples s={s}, p={p}, o={o}".format(s=s, p=p, o=o)
if __name__ == "__main__":
# Create a new parser and try to parse the example NT file.
n = NTriplesParser(StreamSink())
with open("./anons-01.nt", "r") as anons:
n.parse(anons)
output
:
Stream of triples s=N33bb017ce2c340999d2aa6a071d79678, p=http://example.org/#p, o=http://example.org/#q
Stream of triples s=N33bb017ce2c340999d2aa6a071d79678, p=http://example.org/#r, o=http://example.org/#s
Stream of triples s=Nb8d195e0586f42c4bcc703be897c74fa, p=http://example.org/#p, o=http://example.org/#q
Stream of triples s=Nb8d195e0586f42c4bcc703be897c74fa, p=http://example.org/#r, o=N235a8c8b4f91453892da284cb0c490e0
Stream of triples s=N235a8c8b4f91453892da284cb0c490e0, p=http://example.org/#s, o=http://example.org/#t