Skip to content

Instantly share code, notes, and snippets.

@albertmeronyo
Last active April 30, 2016 08:25
Show Gist options
  • Save albertmeronyo/c6ab285d0b73b05392e2f9b8a5bbea82 to your computer and use it in GitHub Desktop.
Save albertmeronyo/c6ab285d0b73b05392e2f9b8a5bbea82 to your computer and use it in GitHub Desktop.
Implementations for built-in STDEV in Virtuoso, Fuseki, Stardog, Sesame
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
PREFIX bsbmi: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
SELECT ?vendor
(COUNT(?review) AS ?count)
(AVG(?rating) AS ?mean)
(afn:sqrt( ( ( sum(?rating*?rating)-sum(?rating)*sum(?rating)/count(?rating) ) / (count(?rating)-1) ) ) AS ?stdev)
WHERE {
GRAPH <http://scry.rocks/bsbm1> {
?review bsbm:reviewFor ?product ;
bsbm:rating1 ?rating .
?offer bsbm:product ?product ;
bsbm:vendor ?vendor .
}
} GROUP BY ?vendor
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
PREFIX bsbmi: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/>
PREFIX func: <http://example.org/custom-function/>
SELECT ?vendor ?count ?mean ?stdev WHERE {
{
SELECT ?vendor
(COUNT(?review) AS ?count)
(AVG(?rating) AS ?mean)
(( ( sum(?rating*?rating)-sum(?rating)*sum(?rating)/count(?rating) ) / (count(?rating)-1) ) AS ?var)
WHERE {
?review bsbm:reviewFor ?product ;
bsbm:rating1 ?rating .
?offer bsbm:product ?product ;
bsbm:vendor ?vendor .
} GROUP BY ?vendor
}
bind (func:sqrt(?var) as ?stdev)
}
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
PREFIX bsbmi: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/>
PREFIX math: <http://www.w3.org/2005/xpath-functions/math#>
SELECT ?vendor
(COUNT(?review) AS ?count)
(AVG(?rating) AS ?mean)
(math:sqrt( ( ( sum(?rating*?rating)-sum(?rating)*sum(?rating)/count(?rating) ) / (count(?rating)-1) ) ) AS ?stdev)
WHERE {
?review bsbm:reviewFor ?product ;
bsbm:rating1 ?rating .
?offer bsbm:product ?product ;
bsbm:vendor ?vendor .
} GROUP BY ?vendor
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
PREFIX bsbmi: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/>
SELECT ?vendor
(COUNT(?review) AS ?count)
(AVG(?rating) AS ?mean)
(sql:STDDEV(?rating) as ?sd)
WHERE {
GRAPH <http://scry.rocks/bsbm1> {
?review bsbm:reviewFor ?product ;
bsbm:rating1 ?rating .
?offer bsbm:product ?product ;
bsbm:vendor ?vendor .
}
} GROUP BY ?vendor
package org.example.customfunction;
import org.openrdf.query.algebra.evaluation.function.Function;
import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
import org.openrdf.model.impl.*;
import org.openrdf.model.*;
import java.lang.*;
/**
* a custom SPARQL function that determines whether an input literal string is
* a palindrome.
*/
public class SqrtFunc implements Function {
// define a constant for the namespace of our custom function
public static final String NAMESPACE = "http://example.org/custom-function/";
/**
* return the URI 'http://example.org/custom-function/palindrome' as a String
*/
public String getURI() {
return NAMESPACE + "sqrt";
}
/**
* Executes the sqrt function.
*
* @return A double with the sqrt() of the parameter
*
* @throws ValueExprEvaluationException
* if more than one argument is supplied or if the supplied argument is
* not a literal.
*/
public Value evaluate(ValueFactory valueFactory, Value... args)
throws ValueExprEvaluationException
{
// our sqrt function expects only a single argument, so throw an error
// if there's more than one
if (args.length != 1) {
throw new ValueExprEvaluationException("sqrt function requires" +
"exactly 1 argument, got " + args.length);
}
Value arg = args[0];
// check if the argument is a double, if not, we throw an error
if (! (arg instanceof Literal)) {
throw new ValueExprEvaluationException(
"invalid argument (literal expected): " + arg);
}
else {
// get the integer value that we want to calculate its sqrt
double num = Double.parseDouble(((Literal)arg).getLabel());
// the resulting sqrt
double sqrt = Math.sqrt(num);
// a function is always expected to return a Value object, so we
// return our boolean result as a Literal
return valueFactory.createLiteral(sqrt);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment