Skip to content

Commit

Permalink
Improved error message for lambda:of
Browse files Browse the repository at this point in the history
Formatting of a macro
  • Loading branch information
Aklakan committed Oct 29, 2024
1 parent 90dd169 commit 4d1c5ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.aksw.jena_sparql_api.sparql.ext.sys;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.aksw.jenax.arq.datatype.lambda.Lambda;
import org.aksw.jenax.arq.datatype.lambda.NodeValueLambda;
Expand Down Expand Up @@ -90,7 +90,17 @@ public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv en
List<Expr> scopedExprs = args.getList();
List<Expr> scopedArgList = scopedExprs.subList(0, n - 1);

List<Var> scopedArgVars = scopedArgList.stream().map(e -> e.asVar()).collect(Collectors.toList());
List<Var> scopedArgVars = new ArrayList<>(scopedArgList.size());
int argIdx = 1;
for (Expr arg : scopedArgList) {
Var v = arg.asVar();
if (v == null) {
throw new RuntimeException("Argument #" + argIdx + " is not a variable");
}
scopedArgVars.add(v);
++argIdx;
}

Set<Var> scopedArgVarsSet = new HashSet<>(scopedArgVars);
Expr scopedRawExpr = scopedExprs.get(n - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,22 @@ ollama:embed
norse:json.cosSimilarity
rdfs:comment "Computes the cosinus similarity (1=similar, -1 not similar) between two vectors represented as JSON arrays of numbers. Uses components starting from 0 up to that of the shorter vector." ;
sh:prefixes eg:prefixMapping ;
udf:simpleDefinition ("""
norse:json.js('(v1, v2) => { var v1Sq = 0; var v2Sq = 0; var dot = 0; for (i = 0; i < Math.min(v1.length, v2.length); ++i) { v1Sq += v1[i] * v1[i]; v2Sq += v2[i] * v2[i]; dot += v1[i] * v2[i]; } v1Len = Math.sqrt(v1Sq); v2Len = Math.sqrt(v2Sq); return dot / (v1Len * v2Len); }', ?v1, ?v2)
""" "v1" "v2") ;
udf:simpleDefinition ('''
norse:json.js("""
(v1, v2) => {
var v1Sq = 0;
var v2Sq = 0;
var dot = 0;
for (i = 0; i < Math.min(v1.length, v2.length); ++i) {
v1Sq += v1[i] * v1[i];
v2Sq += v2[i] * v2[i];
dot += v1[i] * v2[i];
}
v1Len = Math.sqrt(v1Sq);
v2Len = Math.sqrt(v2Sq);
return dot / (v1Len * v2Len);
}
""", ?v1, ?v2)
''' "v1" "v2") ;
.

0 comments on commit 4d1c5ff

Please sign in to comment.