nquads

package module
v0.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Unlicense Imports: 7 Imported by: 1

README

nquads

A basic nquads parser in Go

Check Status Test Status Go Report Card go.dev reference

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

Documentation

Overview

Package nquads parses N-Quads (https://www.w3.org/TR/n-quads/)

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrUnexpectedCharacter is the error returned when a general syntax error is encountered.
	ErrUnexpectedCharacter = errors.New("unexpected character")

	// ErrInvalidCodepointExpression is the error returned when an IRI or literal is encountered with an invalid
	// unicode encoding of the form \Uxxxxxxxx or \uxxxx is encountered.
	ErrInvalidCodepointExpression = errors.New("invalid unicode codepoint expression")

	// ErrUnexpectedEOF is the error returned when an EOF was encountered while reading a quad.
	ErrUnexpectedEOF = io.ErrUnexpectedEOF

	// ErrUnterminatedQuad is the error returned when a quad appears to be missing the final period.
	ErrUnterminatedQuad = errors.New("unterminated quad, expecting '.'")

	// ErrRelativeIRI is the error returned when a relative IRI is encountered. All IRIs in an N-Quads document must
	// be written as absolute IRIs.
	ErrRelativeIRI = errors.New("relative IRI")
)

These are the errors that can be returned in ParseError.Error

Functions

This section is empty.

Types

type ParseError

type ParseError struct {
	Line   int   // Line where the error occurred
	Column int   // Column (rune index) where the error occurred
	Err    error // The actual error
}

A ParseError is returned for parsing errors. The first line is 1. The first column is 0.

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type Quad

type Quad struct {
	S rdf.Term
	P rdf.Term
	O rdf.Term
	G rdf.Term
}

A Quad consists of a subject, predicate, object and graph

func (Quad) String

func (q Quad) String() string

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) Err

func (r *Reader) Err() error

Err returns any error encountered while reading. If Err is non-nil then Next will always return false.

func (*Reader) Next

func (r *Reader) Next() bool

Next attempts to read the next quad from the underlying reader. It returns false if no quad could be read which may indicate an error has occurred or the end of the input stream has been reached.

func (*Reader) Quad

func (r *Reader) Quad() Quad

Quad returns the last quad read

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL