title: Generating urls for a sparql query which is using dbpedia
body: <p>I'm working on developing a front end for a service where, currently, users come up with their own SPARQL query and set their parameters within that, for example:</p>
  
  <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
  PREFIX dbpedia: &lt;http://dbpedia.org/resource/&gt;
  PREFIX dbo: &lt;http://dbpedia.org/ontology/&gt;
  PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
  PREFIX framenet: &lt;http://www.newsreader-project.eu/framenet/&gt;
  PREFIX gaf: &lt;http://groundedannotationframework.org/files/2014/01/&gt;
  PREFIX dct: &lt;http://purl.org/dc/terms/&gt;
  PREFIX sem: &lt;http://semanticweb.cs.vu.nl/2009/11/sem/&gt;
  # All allowed parameters:
  # output: html, offset: 0, limit: 100, 
  # uri.0: {uri_0}, uri.1: {uri_1}
  # filter_block: ?filterfield bif:contains "{string}" ., date_filter_block: ?d owltime:year "{datefilter}"^^xsd:int . 
  # uri_filter_block:  ?filterfield rdfs:label ?_label . ?_label bif:contains "{string}" .
  
  SELECT ?event (COUNT(*) AS ?event_size) ?datetime ?event_label
  WHERE {
    {
      SELECT DISTINCT ?event ?datetime ?event_label
      WHERE {
        { ?event sem:hasActor {uri_0} } UNION { ?event sem:hasPlace {uri_0} }
        ?event sem:hasTime ?t ; rdfs:label ?event_label .
        ?t owltime:inDateTime ?d .
        ?d owltime:year "{datefilter}"^^xsd:int . 
        ?t rdfs:label ?datetimetmp .
        FILTER (regex(?datetimetmp,"\\d{4}-\\d{2}"))
        BIND (SUBSTR(?datetimetmp,1,10) AS ?datetime)
      }
      ORDER BY ?datetime
      OFFSET 0
      LIMIT 100
    }
    ?event ?p ?o .
  }
  GROUP BY ?event ?datetime ?event_label
  ORDER BY ?datetime
  </code></pre>
  
  <p>and a page like <a href="https://newsreader.scraperwiki.com/summary_of_events_with_actor?uris.0=dbpedia:Thierry_Henry" rel="nofollow">this</a> is generated which displays the results of the query as a table.</p>
  
  <p>What I'm trying to do is create a page which the user can use to enter their desired parameters in an 'easier' way. So, instead of typing <code>limit:100</code> in the SPARQL query, I've provided them with a text box entitled 'Limit' which they then enter a value into. I also have a drop down list which lists all of the different query types (such as 'Get events mentioning a named actor' which I have shown you here, or others such as 'Get the properties of a type' or 'Get events with a specific frame net value').</p>
  
  <p>I have an index.html file and a code.js file and, up until now, I've simply had a page with text boxes and a submit button which, when clicked upon/enter is pressed, a function is ran which uses an ajax request to 'pull' the table from a url that I've generated, such as the page of results I linked to up above. This url is generated in the following way:</p>
  
  <pre><code>var runQuery = function move(actor, pageNum) {
      var initUrl = "https://newsreader.scraperwiki.com/{0}/page/{1}?uris.0={2}"
      var queryUrl = initUrl.replace("{1}", pageNum);
      var queryUrl = queryUrl.replace("{2}", "dbpedia:" + actor);
      limit = $(textInput[1]).val();
      offset = $(textInput[2]).val();
      stringFilter = $(textInput[3]).val();
      dateFilter = $(textInput[4]).val();
      framenet = $(textInput[5]).val();
      if (limit != "") {queryUrl += ("&amp;limit=" + limit)};
      if (offset != "") {queryUrl += ("&amp;offset=" + offset)};
      if (stringFilter != "") {queryUrl += ("&amp;filter=" + stringFilter)};
      if (dateFilter != "") {queryUrl += ("&amp;datefilter=" + dateFilter)};
      //...more code follows...
  </code></pre>
  
  <p>The full code can be found <a href="https://github.com/SophieBuckley/newsreaderstuff/blob/master/code.js" rel="nofollow">here</a>.</p>
  
  <p>This all works fine for what I've done so far, but I'm having trouble trying to think of ways to concisely alter the <code>var queryUrl = queryUrl.replace("{2}", "dbpedia:" + actor;</code> line in particular and the rest of the code to account for the different prefixes used for different query types (in the SPARQL query up above it is <code>PREFIX dbpedia: &lt;http...</code>, but it is sometimes <code>PREFIX dbo: &lt;http...</code>, etc).</p>
  
  <p>After that long winded explanation, what I want to know is if there is an alternative to manually manipulating <code>queryUrl</code> and using a load of conditional statements to account for all of the different query types and, if so, how can I do it? I'm very new to any kind of web stuff and I'm a bit lost! Any advice on how to approach accounting for the different query types as concisely as possible would be great. The only way I can think of how to do it at the minute is to have some sort of code that says...</p>
  
  <pre><code>if (actor != "") {
      queryUrl = queryUrl.replace("{2}", "dbpedia:" + actor);
  } else if (....) {
      queryUrl = queryUrl.replace("{2}", "dbo:" + ....);
  } else... etc
  </code></pre>
  
  <p>.. (where actor is referring to the name of a thing) but I think doing it this way would be clunky and would get confusing with all of the different formats that would need to be accounted for.</p>
  
  <p>Thanks in advance, and if any more information is needed about my problem then please don't hesitate to tell me!</p>
  
  <p><em>edit</em>: I've since came to the conclusion that dbpedia-spotlight isn't very useful for my problem that I've highlighted in this question.</p>