Skip to content

Instantly share code, notes, and snippets.

@bergos

bergos/common.js Secret

Created October 2, 2022 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bergos/9a80d41415ad1e7a5cffa0bbe8aec6fe to your computer and use it in GitHub Desktop.
Save bergos/9a80d41415ad1e7a5cffa0bbe8aec6fe to your computer and use it in GitHub Desktop.
Examples for @rdfjs/score
import housemd from 'housemd'
import rdf from 'rdf-ext'
const ns = {
rdf: rdf.namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
schema: rdf.namespace('http://schema.org/')
}
// import he House M.D. dataset
const dataset = rdf.dataset(housemd({ factory: rdf }))
// get all subject named nodes in the dataset
const subjects = rdf.termSet(
rdf.clownface({ dataset })
.in()
.filter(ptr => ptr.term.termType === 'NamedNode')
.terms
)
// get all literal objects in the dataset
const literals = rdf.termSet(
rdf.clownface({ dataset })
.has(ns.rdf.type, ns.schema.Person)
.out(ns.schema.givenName)
.filter(ptr => ptr.term.termType === 'Literal')
.terms
)
// the input structures that will be handed over to the score functions
const input = { dataset, terms: subjects }
const inputLiterals = { dataset, terms: literals }
export {
input,
inputLiterals,
ns
}
import rdf from 'rdf-ext'
import { input } from './common.js'
function example () {
const results = rdf.score.sort(rdf.score.pageRank()(input))
for (const result of results) {
console.log(`${result.term.value} (${result.score})`)
}
}
example()
import rdf from 'rdf-ext'
import { input, ns } from './common.js'
function example () {
const results = rdf.score.sort(
rdf.score.fallback([
rdf.score.exists({ subject: rdf.namedNode('https://housemd.rdf-ext.org/person/lisa-cuddy') }),
rdf.score.product([
rdf.score.type(ns.schema.Person),
rdf.score.pageRank()
])
])(input)
)
for (const result of results) {
console.log(`${result.term.value} (${result.score})`)
}
}
example()
import rdf from 'rdf-ext'
import { inputLiterals } from './common.js'
function example () {
const results = rdf.score.sort(rdf.score.language(['en', 'de', '*'])(inputLiterals))
for (const result of results) {
console.log(`${result.term.value} (${result.score})`)
}
}
example()
{
"name": "rdfjs-score-examples",
"version": "0.0.0",
"description": "Examples for @rdfjs/score",
"type": "module",
"main": "example1.js",
"author": "Thomas Bergwinkl <bergi@axolotlfarm.org> (https://www.bergnet.org/people/bergi/card#me)",
"license": "MIT",
"dependencies": {
"housemd": "^0.1.0",
"rdf-ext": "^2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment