labelingsystem-server  Version 0.1.0.0
Retcat_Wikidata Class Reference

Static Public Member Functions

static Map< String, SuggestionItem > query (String searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
 
static JSONObject info (String url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, RetcatException
 

Member Function Documentation

◆ info()

static JSONObject info ( String  url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, RetcatException
static
113  {
114  try {
115  String sparqlendpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql";
116  String sparql = "PREFIX schema: <http://schema.org/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
117  + "SELECT DISTINCT ?prefLabel ?desc { "
118  + "<" + url + "> rdfs:label ?prefLabel . "
119  + "<" + url + "> schema:description ?desc . "
120  + "FILTER(langMatches(lang(?prefLabel), \"EN\")) "
121  + "FILTER(langMatches(lang(?desc), \"EN\")) "
122  + "} LIMIT 1";
123  URL obj = new URL(sparqlendpoint);
124  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
125  con.setRequestMethod("POST");
126  con.setRequestProperty("Accept", "application/sparql-results+json");
127  String urlParameters = "query=" + sparql;
128  con.setDoOutput(true);
129  DataOutputStream wr = new DataOutputStream(con.getOutputStream());
130  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
131  writer.write(urlParameters);
132  writer.close();
133  wr.close();
134  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
135  String inputLine;
136  StringBuilder response = new StringBuilder();
137  while ((inputLine = in.readLine()) != null) {
138  response.append(inputLine);
139  }
140  in.close();
141  // parse SPARQL results json
142  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
143  JSONObject resultsObject = (JSONObject) jsonObject.get("results");
144  JSONArray bindingsArray = (JSONArray) resultsObject.get("bindings");
145  // output
146  JSONObject jsonOut = new JSONObject();
147  for (Object element : bindingsArray) {
148  JSONObject tmpElement = (JSONObject) element;
149  // get Label
150  JSONObject labelObject = (JSONObject) tmpElement.get("prefLabel");
151  String labelValue = (String) labelObject.get("value");
152  String labelLang = (String) labelObject.get("xml:lang");
153  jsonOut.put("label", labelValue);
154  jsonOut.put("lang", labelLang);
155  // get description
156  JSONObject descriptionObject = (JSONObject) tmpElement.get("desc");
157  if (descriptionObject != null) {
158  String descriptionValue = (String) descriptionObject.get("value");
159  jsonOut.put("description", descriptionValue);
160  }
161  }
162  jsonOut.put("uri", url);
163  // get retcat info
164  String type = "wikidata";
165  String quality = "";
166  String group = "";
167  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
168  if (item.getType().equals(type)) {
169  quality = item.getQuality();
170  group = item.getGroup();
171  }
172  }
173  jsonOut.put("type", type);
174  jsonOut.put("quality", quality);
175  jsonOut.put("group", group);
176  jsonOut.put("scheme", "wikidata");
177  jsonOut.put("broaderTerms", new JSONArray());
178  jsonOut.put("narrowerTerms", new JSONArray());
179  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
180  return jsonOut;
181  } else {
182  throw new RetcatException("no label for this uri available");
183  }
184  } catch (Exception e) {
185  return new JSONObject();
186  }
187  }

References LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getInfoWikidata(), and RetcatResource.getQueryResultsWIKIDATA().

◆ query()

static Map<String, SuggestionItem> query ( String  searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
static
29  {
30  String url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql";
31  String sparql = "PREFIX schema: <http://schema.org/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
32  + "SELECT DISTINCT ?Subject ?item ?prefLabel ?desc { "
33  + "?Subject rdfs:label ?prefLabel . "
34  + "?Subject schema:description ?desc . "
35  + "FILTER(CONTAINS(LCASE(?prefLabel),'" + searchword + "')) "
36  + "FILTER(langMatches(lang(?prefLabel), \"EN\")) "
37  + "FILTER(langMatches(lang(?desc), \"EN\")) "
38  + "} LIMIT 20";
39  URL obj = new URL(url);
40  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
41  con.setRequestMethod("POST");
42  con.setRequestProperty("Accept", "application/sparql-results+json");
43  String urlParameters = "query=" + sparql;
44  con.setDoOutput(true);
45  DataOutputStream wr = new DataOutputStream(con.getOutputStream());
46  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
47  writer.write(urlParameters);
48  writer.close();
49  wr.close();
50  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
51  String inputLine;
52  StringBuilder response = new StringBuilder();
53  while ((inputLine = in.readLine()) != null) {
54  response.append(inputLine);
55  }
56  in.close();
57  // parse SPARQL results json
58  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
59  JSONObject resultsObject = (JSONObject) jsonObject.get("results");
60  JSONArray bindingsArray = (JSONArray) resultsObject.get("bindings");
61  // create unique list of ids
62  HashSet<String> uris = new HashSet<String>();
63  for (Object element : bindingsArray) {
64  JSONObject tmpElement = (JSONObject) element;
65  JSONObject subject = (JSONObject) tmpElement.get("Subject");
66  String subjectValue = (String) subject.get("value");
67  uris.add(subjectValue);
68  }
69  // create list of autosuggest objects
70  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
71  for (String element : uris) {
72  autosuggests.put(element, new SuggestionItem(element));
73  }
74  // fill objects
75  for (Object element : bindingsArray) {
76  JSONObject tmpElement = (JSONObject) element;
77  // get Subject
78  JSONObject subject = (JSONObject) tmpElement.get("Subject");
79  String subjectValue = (String) subject.get("value");
80  // for every subject value get object from list and write values in it
81  SuggestionItem tmpAutosuggest = autosuggests.get(subjectValue);
82  // get Label
83  JSONObject labelObject = (JSONObject) tmpElement.get("prefLabel");
84  String labelValue = (String) labelObject.get("value");
85  String labelLang = (String) labelObject.get("xml:lang");
86  tmpAutosuggest.setLabel(labelValue);
87  tmpAutosuggest.setLanguage(labelLang);
88  // get description
89  JSONObject descriptionObject = (JSONObject) tmpElement.get("desc");
90  if (descriptionObject != null) {
91  String descriptionValue = (String) descriptionObject.get("value");
92  tmpAutosuggest.setDescription(descriptionValue);
93  }
94  // get Scheme
95  tmpAutosuggest.setSchemeTitle("wikidata");
96  // get retcat info
97  String type = "wikidata";
98  String quality = "";
99  String group = "";
100  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
101  if (item.getType().equals(type)) {
102  quality = item.getQuality();
103  group = item.getGroup();
104  }
105  }
106  tmpAutosuggest.setType(type);
107  tmpAutosuggest.setQuality(quality);
108  tmpAutosuggest.setGroup(group);
109  }
110  return autosuggests;
111  }

References LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getQueryResultsWIKIDATA().

Exception