Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Query {
- public static void main(String[] args) {
- String sparql1 = "prefix value: <http://dbpedia.org/resource/>\n"
- + "prefix foaf: <http://xmlns.com/foaf/0.1/>\n"
- + "prefix : <http://dbpedia.org/>\n"
- + "select ?v5 ?v6 ?v9 ?v4 ?v8 where { \n"
- + "{ value:AbboT ?v5 ?v6. ?v6 foaf:name ?v8. } \n"
- + "UNION { ?v9 ?v5 value:AbboT; foaf:name ?v4.}\n";
- String sparql2 = "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
- + "SELECT ?v1 "
- + "WHERE {"
- + "{ ?v1 rdfs:label %%v%% } UNION { ?v1 rdfs:label %%v%% }."
- + "FILTER (regex(str(?v1),'http://dbpedia.org/resource/') || regex(str(?v1,'http://dbpedia.org/ontology/') || regex(str(?v1),'http://www.w3.org/2002/07/owl') || regex(str(?v1),'http://www.w3.org/2001/ XMLSchema') || regex(str(?v1),'http://www.w3.org/2000/01/rdf-schema') || regex(str(?v1),'http://www.w3.org/1999/02/22-rdf-syntax -ns')) }";
- String sparql3 = "Prefix dbpedia: <http://dbpedia.org/resource/>\n"
- + "Prefix dbp-owl: <http://dbpedia.org/ontology/>\n"
- + "Prefix dbp-prop: <http://dbpedia.org/property/>\n"
- + "Prefix dbp-yago: <http://dbpedia.org/class/yago/>\n"
- + "Prefix dbp-cat: <http://dbpedia.org/resource/Category/>\n"
- + "Prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax -ns#>\n"
- + "Prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
- + "Prefix owl: <http://www.w3.org/2002/07/owl#>\n"
- + "Prefix xsd: <http://www.w3.org/2001/ XMLSchema#>\n"
- + "Prefix skos: <http://www.w3.org/2004/02/skos/core#>\n"
- + "Prefix foaf: <http://xmlns.com/foaf/0.1/>\n"
- + "Prefix georss: <http://www.georss.org/georss/>\n"
- + "SELECT * WHERE { ?v6 a dbp-owl:PopulatedPlace; dbp-owl:abstract ?v1; rdfs:label ?v2; geo:lat ?v3; geo:long ?v4. {?v6 rdfs:label %%v%%.} UNION { ?v5 dbp-prop:redirect ?v6; rdfs:label %%v%%. } OPTIONAL { ?v6 foaf:depiction ?v8 } OPTIONAL { ?v6 foaf:homepage ?v10 } OPTIONAL { ?v6 dbp-owl:populationTotal ?v12 } OPTIONAL { ?v6 dbp-owl:thumbnail ?v14 } FILTER ( langMatches( lang(?v1), 'de') && langMatches( lang(?v2), 'de') )";
- sparqlParser(sparql3);
- }
- public static ArrayList<String[]> sparqlParser(String q) {
- // Replace prefixes
- Pattern prefixPattern = Pattern.compile(
- "prefix\\s+([a-z\\-]*:)\\s+<(.+)>", Pattern.CASE_INSENSITIVE);
- Matcher m = prefixPattern.matcher(q);
- int start = 0;
- while (m.find(start)) {
- start = m.end();
- q = q.replace(m.group(), "");
- q = q.replaceAll("\\s+" + m.group(1), " " + m.group(2));
- }
- // ************ END Replace Prefixes ************** //
- // Some Pre-processing to query //
- // Remplace all brackets, and extra white space to one space
- q = q.replaceAll("[{}\\s+]", " ");
- // Remove the terms union and optional with anycase
- q = q.replaceAll(
- "[uU][nN][iI][oO][nN]|[oO][pP][tT][iI][oO][nN][aA][lL]", "");
- // Place a space before any semi-colon
- q = q.replaceAll(";", " ;");
- // Make sure any extra white space is removed
- q = q.replaceAll("\\s+", " ");
- // Only get the where clause that we need to work on.
- Pattern where = Pattern.compile("where\\s*(.*)",
- Pattern.CASE_INSENSITIVE);
- m = where.matcher(q);
- if (m.find())
- q = m.group(1);
- // Remove any white space between "FILTER" and "("
- q = q.replaceAll("([Ff][Ii][Ll][Tt][Ee][Rr])\\s+(\\()", "$1$2");
- // Remove any white space before any .
- q = q.replaceAll("\\s+\\.", "\\.");
- // ******* End of Pre-Processing Query ******** //
- // Remove Filters //
- Pattern filterPattern = Pattern.compile("FILTER\\(",
- Pattern.CASE_INSENSITIVE);
- m = filterPattern.matcher(q);
- start = 0;
- while (m.find(start)) {
- q = removeFilterFromSparql(q, m.start());
- start = m.end();
- }
- // ********** Remove Filters *********** \\
- // System.out.println(q);
- // Split query to parts
- String[] queryArray = q.split(" ");
- ArrayList<String[]> parts = new ArrayList<>();
- for (int i = 0; i < queryArray.length; i++) {
- if (i % 3 == 0)
- parts.add(new String[3]);
- parts.get(i / 3)[i % 3] = queryArray[i];
- if (queryArray[i].matches(";"))
- parts.get(i / 3)[i % 3] = parts.get(i / 3 - 1)[0];
- if (i % 3 == 2)
- parts.get(i / 3)[2] = parts.get(i / 3)[2].replaceAll(
- "\\s*\\.$", "");
- }
- // **** End Split Query to Parts ****** //
- for (int i = 0; i < parts.size(); i++) {
- for (int j = i + 1; j < parts.size(); j++) {
- if (parts.get(i)[0].equals(parts.get(j)[0])
- && parts.get(i)[1].equals(parts.get(j)[1])
- && parts.get(i)[2].equals(parts.get(j)[2])) {
- parts.remove(j);
- j--;
- }
- }
- }
- return parts;
- }
- public static String removeFilterFromSparql(String q, int filterStart) {
- // First character after "filter("
- int i = filterStart + 6;
- int brackets = 1;
- while (brackets > 0 && ++i < q.length()) {
- if (q.charAt(i) == '(')
- brackets++;
- else if (q.charAt(i) == ')')
- brackets--;
- }
- q = q.substring(0, filterStart).concat(
- q.substring(i - 1, q.length() - 1));
- return q;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement