Skip to content

Commit

Permalink
Improve filter keywords pretty printing etc #72
Browse files Browse the repository at this point in the history
1. Improve rewriting for `FILTER KEYWORDS` (was previously called
   `FILTER CONTAINS`, but this now collides with the built-in function
   `CONTAINS`, which has been implemented for QLever in the meantime).

2. Improve code for pretty-printing the entries of the result table
   (it's still somewhat hacky and, above all, inefficient, but that's
   not new and work for another PR).

3. Show the `Map view` button also for the dblp-related instances.
  • Loading branch information
hannahbast authored and Hannah Bast committed Dec 10, 2023
2 parents 0925824 + 77f2bcc commit 70f4a63
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 60 deletions.
65 changes: 48 additions & 17 deletions backend/static/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,22 @@ async function rewriteQuery(query, kwargs = {}) {
function rewriteQueryNoAsyncPart(query) {
var query_rewritten = query;

// HACK 1: Rewrite FILTER CONTAINS(?title, "info* retr*") using
// HACK 1: Rewrite FILTER KEYWORDS(?title, "info* retr*") using
// ql:contains-entity and ql:contains-word.
var num_rewrites_filter_contains = 0;
var m_var = "?qlm_";
var filter_contains_re = /FILTER\s+CONTAINS\((\?[\w_]+),\s*(\"[^\"]+\")\)\s*\.?\s*/i;
while (query_rewritten.match(filter_contains_re)) {
query_rewritten = query_rewritten.replace(filter_contains_re,
m_var + ' ql:contains-entity $1 . ' + m_var + ' ql:contains-word $2 . ');
m_var = m_var + "i";
num_rewrites_filter_contains += 1;
}
if (num_rewrites_filter_contains > 0) {
console.log("Rewrote query with \"FILTER CONTAINS\"");
const rewriteFilterKeywords = true;
if (rewriteFilterKeywords) {
var num_rewrites_filter_contains = 0;
var m_var = "?qlm_";
var filter_contains_re = /FILTER\s+KEYWORDS\((\?[\w_]+),\s*(\"[^\"]+\")\)\s*\.?\s*/i;
while (query_rewritten.match(filter_contains_re)) {
query_rewritten = query_rewritten.replace(filter_contains_re,
m_var + ' ql:contains-entity $1 . ' + m_var + ' ql:contains-word $2 . ');
m_var = m_var + "i";
num_rewrites_filter_contains += 1;
}
if (num_rewrites_filter_contains > 0) {
console.log("Rewrote query with \"FILTER KEYWORDS\"");
}
}

// HACK 2: Rewrite query to use ql:has-predicate if it fits a certain pattern
Expand Down Expand Up @@ -854,6 +857,38 @@ function htmlEscape(str) {
// table.
function getFormattedResultEntry(str, maxLength, column = undefined) {

// Get the variable name from the table header. TODO: it is inefficient to do
// this for every table entry.
var var_name = $($("#resTable").find("th")[column + 1]).html();

// If the entry is or contains a link, make it clickable (see where these
// variables are set in the following code).
let isLink = false;
let linkStart = "";
let linkEnd = "";

// HACK: If the variable ends in "_sparql" or "_mapview", consider the value
// as a SPARQL query, and show it in the QLever UI or on a map, respectively.
if (var_name.endsWith("_sparql") || var_name.endsWith("_mapview")) {
isLink = true;
if (var_name.endsWith("_sparql")) {
mapview_url = `https://qlever.cs.uni-freiburg.de/${SLUG}/` +
`?query=${encodeURIComponent(str)}`;
icon_class = "glyphicon glyphicon-search";
str = "Query view";
} else {
mapview_url = `https://qlever.cs.uni-freiburg.de/mapui-petri/` +
`?query=${encodeURIComponent(str)}` +
`&mode=objects&backend=${BASEURL}`;
icon_class = "glyphicon glyphicon-globe";
str = "Map view";
}
linkStart = `<span style="white-space: nowrap;">` +
`<i class="${icon_class}"></i> ` +
`<a href="${mapview_url}" target="_blank">`;
linkEnd = '</a></span>';
}

// TODO: Do we really want to replace each _ by a space right in the
// beginning?
str = str.replace(/_/g, ' ');
Expand Down Expand Up @@ -913,7 +948,6 @@ function getFormattedResultEntry(str, maxLength, column = undefined) {

// HACK Hannah 16.09.2021: Custom formatting depending on the variable name in
// the column header.
var var_name = $($("#resTable").find("th")[column + 1]).html();
// console.log("Check if \"" + str + "\" in column \"" + var_name + "\" is a float ...");
if (var_name.endsWith("?note") || var_name.endsWith("_note")) str = parseFloat(str).toFixed(2).toString();
if (var_name.endsWith("_per_paper")) str = parseFloat(str).toFixed(2).toString();
Expand All @@ -924,9 +958,6 @@ function getFormattedResultEntry(str, maxLength, column = undefined) {

pos = cpy.lastIndexOf("^^")
pos_http = cpy.indexOf("http");
let isLink = false;
let linkStart = "";
let linkEnd = "";

// For typed literals (with a ^^ part), prepend icon to header that links to
// that type.
Expand Down Expand Up @@ -956,7 +987,7 @@ function getFormattedResultEntry(str, maxLength, column = undefined) {

// For IRIs that start with http display the item depending on the link type.
// For images, a thumbnail of the image is shown. For other links, prepend a
// symbol that depends on the link tpye and links to the respective URL.
// symbol that depends on the link type and links to the respective URL.
//
// TODO: What if http occur somewhere inside a literal or a link?
} else if (pos_http > 0) {
Expand Down
Loading

0 comments on commit 70f4a63

Please sign in to comment.