Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Query {
  6. public static void main(String[] args) {
  7. String sparql1 = "prefix value: <http://dbpedia.org/resource/>\n"
  8. + "prefix foaf: <http://xmlns.com/foaf/0.1/>\n"
  9. + "prefix : <http://dbpedia.org/>\n"
  10. + "select ?v5 ?v6 ?v9 ?v4 ?v8 where { \n"
  11. + "{ value:AbboT ?v5 ?v6. ?v6 foaf:name ?v8. } \n"
  12. + "UNION { ?v9 ?v5 value:AbboT; foaf:name ?v4.}\n";
  13.  
  14. String sparql2 = "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
  15. + "SELECT ?v1 "
  16. + "WHERE {"
  17. + "{ ?v1 rdfs:label %%v%% } UNION { ?v1 rdfs:label %%v%% }."
  18. + "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')) }";
  19.  
  20. String sparql3 = "Prefix dbpedia: <http://dbpedia.org/resource/>\n"
  21. + "Prefix dbp-owl: <http://dbpedia.org/ontology/>\n"
  22. + "Prefix dbp-prop: <http://dbpedia.org/property/>\n"
  23. + "Prefix dbp-yago: <http://dbpedia.org/class/yago/>\n"
  24. + "Prefix dbp-cat: <http://dbpedia.org/resource/Category/>\n"
  25. + "Prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax -ns#>\n"
  26. + "Prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
  27. + "Prefix owl: <http://www.w3.org/2002/07/owl#>\n"
  28. + "Prefix xsd: <http://www.w3.org/2001/ XMLSchema#>\n"
  29. + "Prefix skos: <http://www.w3.org/2004/02/skos/core#>\n"
  30. + "Prefix foaf: <http://xmlns.com/foaf/0.1/>\n"
  31. + "Prefix georss: <http://www.georss.org/georss/>\n"
  32. + "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') )";
  33.  
  34. sparqlParser(sparql3);
  35. }
  36.  
  37. public static ArrayList<String[]> sparqlParser(String q) {
  38. // Replace prefixes
  39. Pattern prefixPattern = Pattern.compile(
  40. "prefix\\s+([a-z\\-]*:)\\s+<(.+)>", Pattern.CASE_INSENSITIVE);
  41. Matcher m = prefixPattern.matcher(q);
  42.  
  43. int start = 0;
  44. while (m.find(start)) {
  45. start = m.end();
  46. q = q.replace(m.group(), "");
  47. q = q.replaceAll("\\s+" + m.group(1), " " + m.group(2));
  48. }
  49. // ************ END Replace Prefixes ************** //
  50.  
  51. // Some Pre-processing to query //
  52.  
  53. // Remplace all brackets, and extra white space to one space
  54. q = q.replaceAll("[{}\\s+]", " ");
  55.  
  56. // Remove the terms union and optional with anycase
  57. q = q.replaceAll(
  58. "[uU][nN][iI][oO][nN]|[oO][pP][tT][iI][oO][nN][aA][lL]", "");
  59.  
  60. // Place a space before any semi-colon
  61. q = q.replaceAll(";", " ;");
  62.  
  63. // Make sure any extra white space is removed
  64. q = q.replaceAll("\\s+", " ");
  65.  
  66. // Only get the where clause that we need to work on.
  67. Pattern where = Pattern.compile("where\\s*(.*)",
  68. Pattern.CASE_INSENSITIVE);
  69. m = where.matcher(q);
  70. if (m.find())
  71. q = m.group(1);
  72.  
  73. // Remove any white space between "FILTER" and "("
  74. q = q.replaceAll("([Ff][Ii][Ll][Tt][Ee][Rr])\\s+(\\()", "$1$2");
  75.  
  76. // Remove any white space before any .
  77. q = q.replaceAll("\\s+\\.", "\\.");
  78.  
  79. // ******* End of Pre-Processing Query ******** //
  80.  
  81. // Remove Filters //
  82. Pattern filterPattern = Pattern.compile("FILTER\\(",
  83. Pattern.CASE_INSENSITIVE);
  84. m = filterPattern.matcher(q);
  85. start = 0;
  86. while (m.find(start)) {
  87. q = removeFilterFromSparql(q, m.start());
  88. start = m.end();
  89. }
  90. // ********** Remove Filters *********** \\
  91. // System.out.println(q);
  92.  
  93. // Split query to parts
  94. String[] queryArray = q.split(" ");
  95. ArrayList<String[]> parts = new ArrayList<>();
  96. for (int i = 0; i < queryArray.length; i++) {
  97. if (i % 3 == 0)
  98. parts.add(new String[3]);
  99. parts.get(i / 3)[i % 3] = queryArray[i];
  100. if (queryArray[i].matches(";"))
  101. parts.get(i / 3)[i % 3] = parts.get(i / 3 - 1)[0];
  102.  
  103. if (i % 3 == 2)
  104. parts.get(i / 3)[2] = parts.get(i / 3)[2].replaceAll(
  105. "\\s*\\.$", "");
  106. }
  107. // **** End Split Query to Parts ****** //
  108.  
  109. for (int i = 0; i < parts.size(); i++) {
  110. for (int j = i + 1; j < parts.size(); j++) {
  111. if (parts.get(i)[0].equals(parts.get(j)[0])
  112. && parts.get(i)[1].equals(parts.get(j)[1])
  113. && parts.get(i)[2].equals(parts.get(j)[2])) {
  114. parts.remove(j);
  115. j--;
  116. }
  117. }
  118. }
  119.  
  120. return parts;
  121. }
  122.  
  123. public static String removeFilterFromSparql(String q, int filterStart) {
  124. // First character after "filter("
  125. int i = filterStart + 6;
  126.  
  127. int brackets = 1;
  128. while (brackets > 0 && ++i < q.length()) {
  129. if (q.charAt(i) == '(')
  130. brackets++;
  131. else if (q.charAt(i) == ')')
  132. brackets--;
  133. }
  134.  
  135. q = q.substring(0, filterStart).concat(
  136. q.substring(i - 1, q.length() - 1));
  137. return q;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement