nquads

NQuad parser in Go

UNLICENSE License

Stars
7
Committers
2

nquads

A basic nquads parser in Go

Overview

N-Quads is a serialisation format for RDF datasets. A dataset consists of a default graph with no name and zero or more named graphs where a graph is a composed of a set of triples. The default graph may be empty.

An N-Quads file is a line-oriented format where each triple or quad statement is terminated by a period ..

  • IRIs are enclosed by < and >
  • Literals have a lexical value enclosed by " followed by an optional language tag using @ as a delimiter, or a data type IRI using ^^ as a delimiter
  • Blank nodes have a lexical label prefixed by _: and the same label denotes the same blank node throughout the file.

A triple in a named graph may be written as a statement using four terms, the last of which is the name of the graph:

<http://example/s> <http://example/p> <http://example/o> <http://example/g> .

A triple in the default graph omits the fourth term:

<http://example/s> <http://example/p> <http://example/o> .

Getting Started

Example of parsing an nquads file and printing out every 5000th quad

	package main

	import (
		"fmt"
		"os"
		"github.com/iand/nquads"
	)	

	func main() {
		nqfile, err := os.Open("myquads.nq")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
			os.Exit(1)
		}
		defer nqfile.Close()


		count := 0
		r := nquads.NewReader(nqfile)
		
		for r.Next()
			count++
			if count % 5000 == 0{
				fmt.Printf("%s\n", r.Quad())
			}
			
		}

		if r.Err() != nil {
			fmt.Printf("Unexpected error encountered: %v\n", r.Err())
		}

	}

Author

License

This is free and unencumbered software released into the public domain. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

Credits

The design and logic is inspired by Go's standard csv parsing library