implemenazione ricerca testo, in progress

This commit is contained in:
cesare 2024-02-09 16:41:36 +01:00
parent a83f97ef9f
commit 673f0b167d
2 changed files with 227 additions and 22 deletions

View File

@ -1556,13 +1556,13 @@ function resetFrasi(){
//aggiunge simbolo di chiusura dialogo
for (const india in dialoghi){
if (dialoghi[india]['Cnt'].replace('_so','')==sentenceid){
console.log(elementoverso.children().slice(-2).text())
//PER GESTIONE PUNTEGGIATURA IN DIALOGHI, DA FARE
//console.log(elementoverso.children().slice(-2).text())
if (elementoverso.children().slice(-2).text().includes(',')){
console.log(' virgola ' +elementoverso.children().slice(-1).html())
//console.log(' virgola ' +elementoverso.children().slice(-1).html())
//let paro=elementoverso.children().slice(-2).text().replace(',', '')
//elementoverso.children().slice(-2).text(paro)
console.log(' virgola dopo' +elementoverso.children().slice(-1).html())
//console.log(' virgola dopo' +elementoverso.children().slice(-1).html())
}
elementoverso.children().slice(-1).html('» ')
@ -1712,7 +1712,6 @@ function resetFrasi(){
}
//execute query
async function executeQuerySearch(query) {
seachbBindingsStream = await myEngine.queryBindings(query, { sources: [{ type: 'sparql', value: sparqlEndpoint },], });
//console.log(query)

View File

@ -1,4 +1,5 @@
var hdnquerygrammar = `
Expression
= head:((_)?"("(_)?("AND "/"OR ") Filtro+ (_)?(")")?){return '{"'+head[3]+'": ['+head[4]+"]}"}//{ return head}.join("").replaceAll(", ,","").replaceAll("(,","(").replaceAll(",("," (")}
@ -31,7 +32,7 @@ var testmm = 0
//query per le ricerche
ontoSparqlQuery = `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
var ontoSparqlQuery = `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX hdn: <http://dantenetwork.it/data/commedia/>
PREFIX lemon: <http://lemon-model.net/lemon#>
@ -87,7 +88,7 @@ WHERE {
### end
}`
ontoSparqlQueryMultCond = `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
var ontoSparqlQueryMultCond = `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX hdn: <http://dantenetwork.it/data/commedia/>
PREFIX lemon: <http://lemon-model.net/lemon#>
@ -130,7 +131,7 @@ WHERE {
}`
ontoSparqlQuerySynt = `
var ontoSparqlQuerySynt = `
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX orl: <https://dantenetwork.it/ontology/orl/current/>
PREFIX ontolex: <http://www.w3.org/ns/lemon/ontolex#>
@ -195,9 +196,9 @@ const maptypes = {
orlprefix = 'https://dantenetwork.it/ontology/orl/current/'
var orlprefix = 'https://dantenetwork.it/ontology/orl/current/'
syntitprefix = 'https://dantenetwork.it/ontology/syntit/current/'
var syntitprefix = 'https://dantenetwork.it/ontology/syntit/current/'
//var stringInSparql= false;
@ -406,7 +407,6 @@ function buildSQ(stquery) {
var parsedquery = parser.parse(ontoSparqlQuery);
console.log(stquery)
if (stquery['EsprLogica'] != null && stquery['EsprLogica'].length > 1) {
parsedquery = parser.parse(ontoSparqlQueryMultCond)
buildSPQuery(stquery)
@ -448,9 +448,82 @@ function buildSQ(stquery) {
return (myquery)
}
//Simple query
filtroContestoJson=getFiltroContestoJson(JSON.parse(stquery['Clausola0']['contesto']))
if (filtroContestoJson.length>0){
for (tmpc of filtroContestoJson){
//console.log(filtroContesto[0])
parsedquery.where.splice(1, 0, tmpc)
}
}
if (stquery['Clausola0']['queryText'] != "") {
searchtext = stquery['Clausola0']['queryText']
strval = searchtext
let strval = searchtext
if (stquery['Clausola0']['lemma_forma'] == 'testo'){
let prefixes=new Object()
prefixes['orl']= 'https://dantenetwork.it/ontology/orl/current/'
prefixes['ontolex']= 'http://www.w3.org/ns/lemon/ontolex#'
prefixes['olires']= 'https://dantenetwork.it/ontology/olires/current/'
prefixes['xsd']='http://www.w3.org/2001/XMLSchema#'
let qry=new Object()
let variables=[]
let where=[]
let whereobject=new Object()
whereobject["type"]= "bgp"
let triples=[]
qry['queryType']='SELECT'
let testo_tkns=strval.split(' ')
var longest = testo_tkns.reduce(
function(a, b) {
return a.length > b.length ? a : b;
}
);
optind=testo_tkns.indexOf(longest)
for (tknid in testo_tkns){
if (tknid>0 && tknid==optind){
triples.unshift(getTestoWhereTriple('reg'+tknid, 'https://dantenetwork.it/ontology/olires/current/fragmentRegionFrom', 'pos'+tknid))
triples.unshift(getTestoWhereTriple('fa'+tknid, 'https://dantenetwork.it/ontology/olires/current/OccursInRegion', 'reg'+tknid))
triples.unshift(getTestoWhereTriple('fa'+tknid, 'https://dantenetwork.it/ontology/orl/current/occurrenceOf', 'cat'+tknid))
triples.unshift(getTestoWhereTriple('cat'+tknid, 'http://www.w3.org/ns/lemon/ontolex#writtenRep', 'Rappresentazione'+tknid))
}
else{
triples.push(getTestoWhereTriple('cat'+tknid, 'http://www.w3.org/ns/lemon/ontolex#writtenRep', 'Rappresentazione'+tknid))
triples.push(getTestoWhereTriple('fa'+tknid, 'https://dantenetwork.it/ontology/orl/current/occurrenceOf', 'cat'+tknid))
triples.push(getTestoWhereTriple('fa'+tknid, 'https://dantenetwork.it/ontology/olires/current/OccursInRegion', 'reg'+tknid))
triples.push(getTestoWhereTriple('reg'+tknid, 'https://dantenetwork.it/ontology/olires/current/fragmentRegionFrom', 'pos'+tknid))
}
where.push(getTestoWhereTextFilter('Rappresentazione'+tknid, testo_tkns[tknid]))
}
for (tknid in testo_tkns){
let variable=new Object()
variable['termType']="Variable"
variable['value']="pos"+tknid
variables.push(variable);
if (tknid==0)
continue
where.push(getTestoWherePosFilter('pos0', 'pos'+tknid, tknid))
}
//testo_tkns.sort((a,b)=> {return b.length-a.length})
whereobject["triples"]=triples
where.push(whereobject)
qry['variables']=variables
qry['where']=where
qry['type']="query"
qry['prefixes']=prefixes
const sparqlGenerator = sparqljs.Generator;
console.log(new sparqlGenerator().stringify(qry))
}
if (stquery['Clausola0']['opzioni_testo'] == 'parola') {
strval = "^" + searchtext.trim() + "$";
}
@ -465,16 +538,8 @@ function buildSQ(stquery) {
}
filtroContestoJson=getFiltroContestoJson(JSON.parse(stquery['Clausola0']['contesto']))
if (filtroContestoJson.length>0){
for (tmpc of filtroContestoJson){
//console.log(filtroContesto[0])
parsedquery.where.splice(1, 0, tmpc)
}
}
parsedquery.where[0].expression.args[1].value = strval;
if (stquery['Clausola0']['lemma_forma'] == 'lemma')
parsedquery.where[0].expression.args[0].value = "lm";
@ -484,11 +549,152 @@ function buildSQ(stquery) {
parsedquery.where.splice(0, 1)
}
return (parsedquery)
}
//Crea le triple per WHERE se cerco il testo
/*
{
"type": "filter",
"expression": {
"type": "operation",
"operator": "=",
"args": [
{
"type": "functionCall",
"function": {
"termType": "NamedNode",
"value": "http://www.w3.org/2001/XMLSchema#int"
},
"args": [
{
"type": "operation",
"operator": "+",
"args": [
{
"termType": "Variable",
"value": "pos"
},
{
"termType": "Literal",
"value": "1",
"language": "",
"datatype": {
"termType": "NamedNode",
"value": "http://www.w3.org/2001/XMLSchema#integer"
}
}
]
}
],
"distinct": false
},
{
"termType": "Variable",
"value": "Num1"
}
]
}
}
*/
function getTestoWherePosFilter(pos1, pos2, dist){
let filterpos=new Object()
filterpos['type']='filter'
let filterexpr= new Object()
filterexpr['type']='operation'
filterexpr['operator']='='
let exprargs=[]
let arg1= new Object()
arg1['type']="functionCall"
let arg1func=new Object()
arg1func['termType']="NamedNode"
arg1func['value']="http://www.w3.org/2001/XMLSchema#int"
arg1['function']=arg1func
let arg1args= []
let arg1argsarg1= new Object()
arg1argsarg1['type']="operation"
arg1argsarg1['operator']="+"
let arg1argsarg1args=[]
let arg1argsarg1argsarg1=new Object()
arg1argsarg1argsarg1['termType']="Variable"
arg1argsarg1argsarg1['value']=pos1
let arg1argsarg1argsarg2=new Object()
arg1argsarg1argsarg2['termType']="Literal"
arg1argsarg1argsarg2['value']=dist
let datatype=new Object()
datatype['termType']='NamedNode'
datatype['value']="http://www.w3.org/2001/XMLSchema#integer"
arg1argsarg1argsarg2["datatype"]=datatype
arg1argsarg1args.push(arg1argsarg1argsarg1)
arg1argsarg1args.push(arg1argsarg1argsarg2)
arg1argsarg1['type']='operation'
arg1argsarg1['operator']='+'
arg1argsarg1['args']=arg1argsarg1args
arg1args.push(arg1argsarg1)
arg1['args']=arg1args
arg1['distinct']='false'
let arg2= new Object()
arg2["termType"]= "Variable",
arg2["value"]= pos2
exprargs.push(arg1)
exprargs.push(arg2)
filterexpr['args']=exprargs
filterpos['expression']=filterexpr
return filterpos
}
function getTestoWhereTextFilter(term, val){
let filtertext=new Object()
filtertext['type']='filter'
let filterexpr= new Object()
filterexpr['type']='operation'
filterexpr['operator']='regex'
let exprargs=[]
let arg1= new Object()
arg1['termType']="Variable"
arg1['value']=term
let arg2= new Object()
arg2['termType']="Literal"
arg2['value']=val
let arg3= new Object()
arg3['termType']="Literal"
arg3['value']='i'
let datatype=new Object()
datatype['termType']='NamedNode'
datatype['value']="http://www.w3.org/2001/XMLSchema#string"
arg2['datatype']=datatype
arg3['datatype']=datatype
exprargs.push(arg1)
exprargs.push(arg2)
exprargs.push(arg3)
filterexpr['args']=exprargs
filtertext['expression']=filterexpr
return filtertext
}
function getTestoWhereTriple(s, p, o){
let tri=new Object()
let sub=new Object()
let pred=new Object()
let ob=new Object()
sub["termType"]="Variable"
sub["value"]=s
pred["termType"]="NamedNode"
pred["value"]=p
ob["termType"]="Variable"
ob["value"]=o
tri["subject"]= sub
tri["predicate"]= pred
tri["object"]= ob
return (tri)
}
//Filtro Contesto