包详细信息

@zazuko/prefixes

zazuko64.6kMIT2.4.0

null

自述文件

@zazuko/vocabularies -- Zazuko's Default Ontologies & Prefixes

GitHub Workflow Status Codecov npm

This package contains a distribution of the most commonly used RDF ontologies (schema/vocab, whatever you call it) including their default prefixes, together with a set of utility functions to work with prefixes.

It is extending RDFa Core Initial Context and contains what we consider commonly used prefixes. Some popular prefixes do not resolve to dereferencable RDF and are thus skipped.

The package is built for use in Node.js projects. We ship N-Quads files of the vocabularies so it could be useful for other programming languages as well as you do not have to take care of downloading the ontologies yourself.

Installation

This is a new release of the previous monolithic package which grew quite a lot.

It is recommended to install the prefixes package and select vocabulary packages individually to reduce the size of node_modules.

$ npm install @zazuko/prefixes @vocabulary/rdf @vocabulary/schema ...

You may still choose to install a one package to get them all.

$ npm install @zazuko/vocabularies

It remains pretty much compatible with the previous @zazuko/rdf-vocabularies package. The main difference is that now it is ESM-only.

Usage

(Read below and take a look at some examples.)

Dataset-as-code modules

All vocabularies published from this package are also exported as JS modules so that then can be imported synchronously (no parsing required) and without additional dependencies when in web app setting (see the raw-loader instructions below).

Modules @rdf-vocabularies/datasets exports factories which returns an array of quads Quad and take RDF/JS DataFactory as parameter.

import $rdf from 'rdf-ext'
import schema from '@vocabulary/schema'

const dataset = $rdf.dataset(schema({ factory: $rdf }))

Vocabularies Metadata

See _index.nq.

vocabularies()

Exported from @zazuko/vocabularies', the vocabularies() function accepts an optional options object:

  • options.only: Array?, default: undefined, a subset of all available prefixes, will only load these.
  • options.factory: RDF/JS DatasetFactory, default: rdf-ext, a dataset factory abiding by the RDF/JS Dataset Specification, used to create the returned datasets.
  • options.stream: Boolean, default: false, whether to return a RDF/JS quad stream instead of regular objects/datasets.

Loading all Vocabularies as Datasets

In browser environment this will cause a request for each individual dataset. It is thus recommended to always only load the needed ontologies to reduce the unnecessary traffic and save bandwidth.

import { vocabularies } from '@zazuko/vocabularies'

vocabularies()
  .then((datasets) => {
    /* `datasets` is:
    {
      "csvw": Dataset,
      "sd": Dataset,
      "ldp": Dataset,
      "schema": Dataset,
      "owl": Dataset,
      "void": Dataset,
      "sioc": Dataset,
      "foaf": Dataset,
      "time": Dataset,
      "dcat": Dataset,
      "oa": Dataset,
      "gr": Dataset,
      "rdf": Dataset,
      "cc": Dataset,
      "ssn": Dataset,
      "rr": Dataset,
      "rdfa": Dataset,
      "org": Dataset,
      "sosa": Dataset,
      "dc11": Dataset,
      "skos": Dataset,
      "dqv": Dataset,
      "prov": Dataset,
      "og": Dataset,
      "qb": Dataset,
      "rdfs": Dataset,
      "dc": Dataset,
      "ma": Dataset,
      "vcard": Dataset,
      "grddl": Dataset,
      "dcterms": Dataset,
      "skosxl": Dataset,
      "wgs": Dataset,
      "dbo": Dataset,
      "dbpedia": Dataset,
      "dbpprop": Dataset,
      "rss": Dataset,
      "cnt": Dataset,
      "vs": Dataset,
      "hydra": Dataset,
      "gn": Dataset,
      "gtfs": Dataset,
      "geo": Dataset,
      "geof": Dataset,
      "geor": Dataset
    }
    */
  })

Loading only some Vocabularies as Datasets

import { vocabularies } from '@zazuko/vocabularies'

vocabularies({ only: ['rdfs', 'owl', 'skos'] })
  .then((datasets) => {
    /* `datasets` is:
    {
      "owl": Dataset,
      "skos": Dataset,
      "rdfs": Dataset
    }
    */
  })

Getting a Readable Stream (Quad Stream)

import { vocabularies } from '@zazuko/vocabularies'
const stream = await vocabularies({ stream: true, only: ['rdfs', 'owl', 'skos'] })

Using vocabularies function in browser

The preferred usage in browser projects is to avoid importing from @zazuko/vocabularies because that will require additional bundling of dynamic n-quads modules.

Instead, import from the partial modules (without even installing the big package, if possible):

  • import { expand } from '@zazuko/prefixes/expand'
  • import { prefixes } from '@zazuko/prefixes/prefixes'
  • import { shrink } from '@zazuko/prefixes/shrink'

The module @zazuko/vocabularies/expandWithCheck requires rdf-ext and parses datasets. See the instructions below for examples how to configure the application.

The package's main module can also be used in browser albeit it needs a bundler such as webpack and additional steps to configure it:

The package can be used in browser albeit it needs a bundler such as webpack and additional steps to configure it:

  • Enable dynamic imports. In webpack it is done with @babel/plugin-syntax-dynamic-import
  • Extend the bundler setup to have it load the contents of vocabulary files (all n-triples). In In webpack it can be done with raw-loader:

    module: {
      rules: [
        {
          test: /\.nq$/,
          use: ['raw-loader']
        }
      ]
    }
  • Be careful with prefetching chunks. Some applications may generate prefetch links for dynamically loaded chunks. Some of the ontology files are quite large and their number will grow over time. Hence, it may be desired to exclude certain chunks from the being eagerly loaded. Check the wiki for examples.

Expanding a Prefix

expanding means: 'xsd:dateTime' → 'http://www.w3.org/2001/XMLSchema#dateTime'. It is the opposite of shrinking:
expand(shrink('http://www.w3.org/2001/XMLSchema#dateTime')) === 'http://www.w3.org/2001/XMLSchema#dateTime'

There are two ways of expanding a prefix:

  • vocabularies.expand(prefixedTerm: String): String synchronous

    Expand without checks. It is similar to prefix.cc in the sense that prefix.cc would expand schema:ImNotInSchemaDotOrg to http://schema.org/ImNotInSchemaDotOrg.

  • vocabularies.expand(prefixedTerm: String, types: Array<String|NamedNode>): Promise<String> asynchronous

    Expand with type checks. types is an array of strings or NamedNodes. See this example:

      const { expand } = require('@zazuko/rdf-vocabularies')
      const Class = expand('rdfs:Class')
      const Property = expand('rdf:Property')
    
      // Will return <schema:person> expanded to `http://schema.org/Person`
      // iff the dataset contains either:
      //   <schema:Person> <rdf:type> <rdfs:Class>
      // or
      //   <schema:Person> <rdf:type> <rdf:Property>
      await expand('schema:Person', [Class, Property])

Shrinking an IRI

shrinking means: 'http://www.w3.org/2001/XMLSchema#dateTime' → 'xsd:dateTime'. It is the opposite of expanding:
shrink(expand('xsd:dateTime')) === 'xsd:dateTime'

  • vocabularies.shrink(iri: String): String

    Note: returns empty string when there is no corresponding prefix. Always check the output when using shrink with user-provided strings.

      import assert from 'assert'
      import { shrink } from '@zazuko/prefixes'
    
      assert(shrink('http://www.w3.org/2001/XMLSchema#dateTime') === 'xsd:dateTime')
      assert(shrink('http://example.com#nothing') === '')
    
      const iri = 'http://example.com#nothing'
      const stringToDisplay = shrink(iri) || iri
      console.log(stringToDisplay) // 'http://example.com#nothing'

Accessing Prefixes: vocabularies.prefixes

Getting an object with prefixes and their base URI:
(Returns this object.)

import { prefixes } from '@zazuko/prefixes'

console.log(prefixes)
/*
 {
  v: 'http://rdf.data-vocabulary.org/#',
  csvw: 'http://www.w3.org/ns/csvw#',
  sd: 'http://www.w3.org/ns/sparql-service-description#',
  …
}
*/

Accessing Data Files from the Package

Accessing the N-Quads files:

import path from 'path'
import module from 'module'

const { resolve } = module.createRequire(import.meta.url)

console.log(path.resolve(resolve('@vocabulary/skos/skos.nq')))

Command line

The package also includes a simple command line interface which forwards the vocabulary datasets to standard output. It can be used in two ways.

By prefix:

rdf-vocab prefix foaf

By namespace URI:

rdf-vocab prefix http://schema.org/

Versioning Scheme

The packages follow semver but that is to indicate library code changes rather than vocabulary changes. Usually, that is. If we identify significant changes to the source RDF for any given vocabulary, we may decide to bump a major version.

Adding new prefixes

New prefixes can be added by opening a pull request on Github. For new requests, first check if the creator/owner of the namespace defined a prefix. If not check prefix.cc. In case prefix.cc is ambiguous a discussion should be raised before the pull-requests gets integrated. Last thing to check are the predefined namespaces in the DBpedia SPARQL endpoint or other popular RDF resources like LOV. If you find one please refer to it in the pull request.

Steps to add a prefix

  1. Add a new directory under ontologies with a package.json. For a minimal example see ACL vocabulary
  2. If necessary, add overrides to the vocabulary key, similar to the others
    • for the file option, a file: scheme IRI can be used, with path relative to the repository root
  3. Run npm run fetch in the vocabulary's dir to process the triples.
  4. Add a dependency to vocabularies meta package
  5. Commit changes and submit a PR

Project-specific prefixes

It is also possible to add prefix within a project so that it can be used with the functions expand and shrink.

import { prefixes, expand, shrink } from '@zazuko/prefixes'

prefixes['foo'] = 'http://example.com/'

// 'http://example.com/bar'
const foobar = expand('foo:bar')

// 'foo:bar'
const prefixed = shrink(foobar)

更新日志

v2023.01.19

Fix

Remove Record<string, string> from type of prefixes

v2023.01.18

New prefix

Prefix #Quads
crm 3892

Feature

Tweak the type of prefixes so that it maps prefixes precisely to their respective namespace. This allows creating type-safe interfaces such as

import type { Prefixes } from '@zazuko/rdf-vocabularies/prefixes'

type JsonLdContext = Partial<Prefixes> & Record<string, string>

const context: JsonLdContext = {
  // this is fine, also auto-completed by IDE  
  rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
  // a missing traiing slash here will cause an error
  schema: 'http://schema.org',
  // other allowed by the union
  foo: 'bar'
}

v2023.01.17

Feature

Added a second parameter to shrink so that a different set of prefixes can be used for prefixing

v2022.11.28

New prefix

Prefix #Quads
mads 1091

v2022.11.25

Fixes

Trying to use the module vocabularies.mjs would fail with error

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".nq"

v2022.6.29

Fixes

Broken dash.nq dataset

v2022.6.28

Updated vocabularies

Prefix #Quads diff
constants 5385 🔻2
dash 1368 🔻98
dbo 40763 🔺9713
dcat 1342 🔻11
prefix 1342 0
qkdv 3405 🔺242
quentitykind 17063 🔺555
qudt 6813 0
schema 16204 🔺198
shex 474 0
sou 210 0
unit 22360 🔺1070
xkos 328 🔺1
xsd 199 🔺0

v2021.9.22

Updated vocabularies

Fix exif base URL: http://www.w3.org/2003/12/exif/ns -> http://www.w3.org/2003/12/exif/ns#

Fix xml base URL: http://www.w3.org/XML/1998/namespace -> http://www.w3.org/XML/1998/namespace/

Fix gs1 base URL: https://www.gs1.org/voc/ -> https://gs1.org/voc/

Prefix #Quads
exif 891

v2021.9.20

New prefix

Prefix #Quads
bibo 1224
dcam 26
dcmitype 89
ebucore 5780
exif 891
gs1 7434
locn 206

Updated vocabularies

Fix ebucore base URL: https://www.ebu.ch/metadata/ontologies/ebucore/ebucore.rdf -> http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#

Prefix #Quads
constant 5387
prefix 305
qkdv 3163
quantitykind 16508
qudt 6813
sou 210
unit 21290

v2021.9.15

New prefix

Prefix #Quads
ebucore 5780

Updated vocabularies

Prefix #Quads
constant 5387
dbo 31050
hydra 467
org 748
prefix 305
qkdv 3135
quantitykind 16433
qudt 6813
schema 16006
shex 474
sou 210
unit 21225
void 208

v2021.6.10

  • Support file: URIs in overrides to have contents generated from local sources
  • Export interface of prefixes, which allows type augmentation

v2021.5.3

Updated vocabularies

Fix prefix base URL: http://qudt.org/2.1/vocab/prefix/ -> http://qudt.org/vocab/prefix/

Prefix #Quads
as 939
constant 5387
geof 37
geor 26
hydra 467
prefix 305
qkdv 3119
quantitykind 16262
qudt 6815
sou 210
unit 21203

v2021.3.31

Updated vocabularies

Fix as base URL.

Prefix #Quads
as 939

v2021.3.30

Updated vocabularies

Prefix #Quads
as 939

v2021.3.17

New prefix

Prefix #Quads
acl 92

v2021.3.9

Removed prefix

Prefix #Quads
qudtv 13797

New prefix

Prefix #Quads
unit 21164

Updated vocabularies

Prefix #Quads
prefix 305
qudt 6815
schema 15400

v2021.3.8

New prefix

Prefix #Quads
constant 5387
discipline 1815
prefix 336
qkdv 3119
quantitykind 16036
sou 210

Updated vocabularies

Prefix #Quads
qudt 7827
rico 7511

v2021.3.2

New prefix

Add shex: prefix and vocabulary.

Prefix #Quads
shex 474

v2021.2.8

New prefix

Add dpv: prefix and vocabulary.

Prefix #Quads
dpv 3294

v2021.1.25

New prefix

Add rico: prefix and vocabulary.

Prefix #Quads
rico 6952

Updated vocabularies

Prefix #Quads
dash 1466
dbo 39297
hydra 431

v2020.12.16

Changes

  • The module @zazuko/rdf-vocabularies/expand now only does simple expansion (no second parameter).
  • Fixed an issue where buildPath returned incorrect path

Updated vocabularies

Prefix #Quads
dcat 1353
dcterms 700
lvont 212
schema 14936

v2020.11.2

Changes

v2020.8.17

Changes

  • Fixed an issue where prefixes were not applied by specificity.

v2020.8.3

New prefixes

Add doap:, earl:, test: prefixes and vocabularies.

Prefix #Quads
doap: 722
earl: 162
test: 116

Changes

  • Fixes to the way a vocab label and description are extracted.

v2020.7.23

Updated vocabularies

Prefix #Quads
dash 1561
schema 15163

v2020.7.13

Make it possible to use this package directly in a browser, for instance via unpkg.com.

By splitting the main module and re-exporting it, it is now possible to cherry-pick the necessary pieces (expand, prefixes and shrink are natively usable) while the general usage in node or existing bundled code remains unchanged.

v2020.6.29

Changes

  • Revamped build to change the way we publish this as a dual, commonjs/esm package.

Updated vocabularies

Prefix #Quads
dash 1524

v2020.6.17

New prefix

Add dash: prefix and vocabulary.

Prefix #Quads
dash: 1271

Updated vocabularies

Prefix #Quads
hydra 408
schema 8885
time 1296

v2020.3.23

Changes

  • Add CLI (rdf-vocab -h)

v2020.3.17

Updated vocabularies

Prefix #Quads
schema 8861

v2020.2.10-2

Fix lvont: base IRI.

v2020.2.10

Changes

  • Possibility to fetch a single vocab with npm run fetch prefix, eg. npm run fetch schema
    • Replaces index triples for that vocab only
    • Does not regenerate the entire index dataset

New vocabulary

Prefix #Quads
lvont 212

v2020.2.3

Changes

  • Strict typings
  • Package only includes a single copy of the types definition, at the root

Updated vocabularies

Prefix #Quads
dc11 107
dcat 1170
dcterms 619
hydra 407
prov 1664
schema 8858

v2019.12.23

New feature

Prefixes Index / Vocabularies metadata: ontologies/_index.nq holds an index of all provided vocabularies with some metadata, as N-Quads.

Updated vocabularies

Prefix #Quads
dbo 39069
rdf 127
schema 8827
xsd 199

v2019.10.22

New prefix

Add sh: prefix and vocabulary.

Prefix #Quads
sh: 1128

Updated vocabularies

Prefix #Quads
hydra 404
qudtv 13797
schema 8824

v2019.9.24

New prefix

Add sem: prefix and vocabulary.

Prefix #Quads
sem: 520

Updated vocabularies

Prefix #Quads (#quads in previous version)
qudtv 13797 (13791)
rdau 15722 (15722)
schema 8845 (8833)

v2019.7.9-2

Bugfix

Fix: as: nq file had the wrong graph: it should be http: not https:.

v2019.7.9

New prefixes

Add as: prefix and vocabulary. Add http: prefix and vocabulary. Add ical: prefix and vocabulary. Add vann: prefix and vocabulary. Add xhv: prefix and vocabulary. Add xkos: prefix and vocabulary.

Prefix #Quads
as: 951
http: 230
ical: 1255
vann: 49
xhv: 208
xkos: 327

Updated vocabularies

Prefix #Quads (#quads in previous version)
qudtv 13791 (13790)
schema 8833 (8809)

Misc

dtype and vaem had incorrect base IRI, this release fixes them:

- dtype: 'http://www.linkedmodel.org/schema/dtype',
+ dtype: 'http://www.linkedmodel.org/schema/dtype#',
- vaem: 'http://www.linkedmodel.org/schema/vaem',
+ vaem: 'http://www.linkedmodel.org/schema/vaem#',

v2019.6.27

New prefixes

Add dtype: prefix and vocabulary. Add vaem: prefix and vocabulary. Add qudt: prefix and vocabulary. Add qudtv: prefix and vocabulary.

Prefix #Quads
dtype 231
vaem 564
qudt 7738
qudtv 13790

v2019.6.17

Feature

Add bundler-friendly build.

BREAKING CHANGE: The default export is replaced with a named one

- const rdfVocabularies = require('@zazuko/rdf-vocabularies')
+ const { vocabularies } = require('@zazuko/rdf-vocabularies')
- const prefixes = require('@zazuko/rdf-vocabularies/prefixes')
+ const prefixes = require('@zazuko/rdf-vocabularies/lib/node/prefixes').default

New prefixes

Add frbr: prefix and vocabulary. Add rdau: prefix and vocabulary. Add smdx: prefix and vocabulary.

Prefix #Quads
frbr 914
rdau 15722
smdx 90

Updated vocabularies

Prefix #Quads (#quads in previous version)
geof 82 (46)
geor 62 (36)
hydra 400 (386)
schema 8809 (8809)

v2019.5.13

New prefix

Add xsd: prefix and vocabulary.

Prefix #Quads
xsd 199

v2019.5.6

Initial release:

Prefix #Quads
cc 115
cnt 108
csvw 632
dbo 8035
dc11 138
dcat 425
dcterms 866
dqv 152
duv 56
foaf 620
geo 518
geof 46
geor 36
gn 6846
gr 1834
grddl 74
gtfs 870
hydra 386
ldp 200
ma 340
oa 334
og 231
org 748
owl 450
prov 1662
qb 265
rdf 102
rdfa 68
rdfs 87
rr 297
rss 44
schema 8809
sd 165
sioc 669
skos 252
skosxl 60
sosa 345
ssn 520
time 1044
vcard 870
void 216
vs 27
wgs 33