labelingsystem-server  Version 0.1.0.0
Retcat_Unesco 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
133  {
134  try {
135  String outputUrl = url;
136  url = GeneralFunctions.encodeURIComponent(url);
137  url = "http://vocabularies.unesco.org/browser/rest/v1/thesaurus/label?lang=en&uri=" + url;
138  URL obj = new URL(url);
139  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
140  con.setRequestMethod("GET");
141  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
142  String inputLine;
143  StringBuilder response = new StringBuilder();
144  while ((inputLine = in.readLine()) != null) {
145  response.append(inputLine);
146  }
147  in.close();
148  // parse json
149  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
150  String labelValue = (String) jsonObject.get("prefLabel");
151  // output
152  JSONObject jsonOut = new JSONObject();
153  jsonOut.put("label", labelValue);
154  jsonOut.put("lang", "en");
155  // get retcat info
156  String type = "unesco";
157  String quality = "";
158  String group = "";
159  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
160  if (item.getType().equals(type)) {
161  quality = item.getQuality();
162  group = item.getGroup();
163  }
164  }
165  jsonOut.put("type", type);
166  jsonOut.put("quality", quality);
167  jsonOut.put("group", group);
168  jsonOut.put("description", "");
169  jsonOut.put("uri", outputUrl);
170  jsonOut.put("scheme", "unesco");
171  // broader and narrower
172  JSONArray broaderTerms = new JSONArray();
173  JSONArray narrowerTerms = new JSONArray();
174  // broader
175  String urlB = "http://vocabularies.unesco.org/browser/rest/v1/thesaurus/broader?lang=en&uri=" + url;
176  URL objB = new URL(urlB);
177  HttpURLConnection conB = (HttpURLConnection) objB.openConnection();
178  conB.setRequestMethod("GET");
179  BufferedReader inB = new BufferedReader(new InputStreamReader(conB.getInputStream(), "UTF8"));
180  String inputLineB;
181  StringBuilder responseB = new StringBuilder();
182  while ((inputLineB = inB.readLine()) != null) {
183  responseB.append(inputLineB);
184  }
185  inB.close();
186  // parse json
187  JSONObject jsonObjectB = (JSONObject) new JSONParser().parse(responseB.toString());
188  JSONArray broaderArray = (JSONArray) jsonObjectB.get("broader");
189  for (Object bo : broaderArray) {
190  JSONObject tmp = (JSONObject) bo;
191  JSONObject tmpObject = new JSONObject();
192  String broaderURI = (String) tmp.get("uri");
193  String broaderLabel = (String) tmp.get("prefLabel");
194  tmpObject.put("label", broaderLabel);
195  tmpObject.put("uri", broaderURI);
196  broaderTerms.add(tmpObject);
197  }
198  // narrower
199  String urlN = "http://vocabularies.unesco.org/browser/rest/v1/thesaurus/narrower?lang=en&uri=" + url;
200  URL objN = new URL(urlN);
201  HttpURLConnection conN = (HttpURLConnection) objN.openConnection();
202  conN.setRequestMethod("GET");
203  BufferedReader inN = new BufferedReader(new InputStreamReader(conN.getInputStream(), "UTF8"));
204  String inputLineN;
205  StringBuilder responseN = new StringBuilder();
206  while ((inputLineN = inN.readLine()) != null) {
207  responseN.append(inputLineN);
208  }
209  inN.close();
210  // parse json
211  JSONObject jsonObjectN = (JSONObject) new JSONParser().parse(responseN.toString());
212  JSONArray narrowerArray = (JSONArray) jsonObjectN.get("narrower");
213  for (Object no : narrowerArray) {
214  JSONObject tmp = (JSONObject) no;
215  JSONObject tmpObject = new JSONObject();
216  String narrowerURI = (String) tmp.get("uri");
217  String narrowerLabel = (String) tmp.get("prefLabel");
218  tmpObject.put("label", narrowerLabel);
219  tmpObject.put("uri", narrowerURI);
220  narrowerTerms.add(tmpObject);
221  }
222  jsonOut.put("broaderTerms", broaderTerms);
223  jsonOut.put("narrowerTerms", narrowerTerms);
224  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
225  return jsonOut;
226  } else {
227  throw new RetcatException("no label for this uri available");
228  }
229  } catch (Exception e) {
230  return new JSONObject();
231  }
232  }

References GeneralFunctions.encodeURIComponent(), and LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getInfoSkosmosUnesco(), and RetcatResource.getQueryResultsSkosmosUNESCO().

◆ query()

static Map<String, SuggestionItem> query ( String  searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
static
27  {
28  searchword = GeneralFunctions.encodeURIComponent(searchword);
29  String url_string = "http://vocabularies.unesco.org/browser/rest/v1/search?query=*" + searchword + "*&lang=en&type=skos:Concept&fields=narrower%20broader&vocab=thesaurus&limit=" + RetcatResource.getLimit();
30  URL url = new URL(url_string);
31  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
32  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
33  String inputLine;
34  StringBuilder response = new StringBuilder();
35  while ((inputLine = br.readLine()) != null) {
36  response.append(inputLine);
37  }
38  br.close();
39  // init output
40  JSONArray outArray = new JSONArray();
41  // fill objects
42  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
43  JSONArray resultsArray = (JSONArray) jsonObject.get("results");
44  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
45  for (Object element : resultsArray) {
46  JSONObject tmpElement = (JSONObject) element;
47  String uriValue = (String) tmpElement.get("uri");
48  autosuggests.put(uriValue, new SuggestionItem(uriValue));
49  SuggestionItem tmpAutosuggest = autosuggests.get(uriValue);
50  String labelValue = (String) tmpElement.get("prefLabel");
51  tmpAutosuggest.setLabel(labelValue);
52  JSONArray desArray = (JSONArray) tmpElement.get("skos:scopeNote");
53  if (desArray != null) {
54  for (Object e : desArray) {
55  String descStr = e.toString();
56  tmpAutosuggest.setDescription(descStr);
57  }
58  }
59  String vocabValue = (String) tmpElement.get("vocab");
60  tmpAutosuggest.setSchemeTitle("unesco");
61  JSONArray boraderArray = (JSONArray) tmpElement.get("broader");
62  JSONArray narrowerArray = (JSONArray) tmpElement.get("narrower");
63  tmpAutosuggest.setLanguage("en");
64  // query for broader
65  if (boraderArray != null) {
66  for (Object item : boraderArray) {
67  JSONObject tmpObject = (JSONObject) item;
68  HashMap<String, String> hstmp = new HashMap();
69  String uriValueTmp = (String) tmpObject.get("uri");
70  //query for label
71  String broaderUrl = GeneralFunctions.encodeURIComponent(uriValueTmp);
72  broaderUrl = "http://vocabularies.unesco.org/browser/rest/v1/thesaurus/label?lang=en&uri=" + broaderUrl;
73  URL obj = new URL(broaderUrl);
74  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
75  con.setRequestMethod("GET");
76  BufferedReader broaderIn = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
77  String broaderInputLine;
78  StringBuilder broaderResponse = new StringBuilder();
79  while ((broaderInputLine = broaderIn.readLine()) != null) {
80  broaderResponse.append(broaderInputLine);
81  }
82  broaderIn.close();
83  // parse json
84  JSONObject broaderJsonObject = (JSONObject) new JSONParser().parse(broaderResponse.toString());
85  String broaderLabelValue = (String) broaderJsonObject.get("prefLabel");
86  hstmp.put(uriValueTmp, broaderLabelValue);
87  tmpAutosuggest.setBroaderTerm(hstmp);
88  }
89  }
90  // query for narrower
91  if (narrowerArray != null) {
92  for (Object item : boraderArray) {
93  JSONObject tmpObject = (JSONObject) item;
94  HashMap<String, String> hstmp = new HashMap();
95  String uriValueTmp = (String) tmpObject.get("uri");
96  //query for label
97  String narrowerUrl = GeneralFunctions.encodeURIComponent(uriValueTmp);
98  narrowerUrl = "http://vocabularies.unesco.org/browser/rest/v1/thesaurus/label?lang=en&uri=" + narrowerUrl;
99  URL obj = new URL(narrowerUrl);
100  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
101  con.setRequestMethod("GET");
102  BufferedReader narrowerIn = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
103  String narrowerInputLine;
104  StringBuilder narrowerResponse = new StringBuilder();
105  while ((narrowerInputLine = narrowerIn.readLine()) != null) {
106  narrowerResponse.append(narrowerInputLine);
107  }
108  narrowerIn.close();
109  // parse json
110  JSONObject broaderJsonObject = (JSONObject) new JSONParser().parse(narrowerResponse.toString());
111  String narrowerLabelValue = (String) broaderJsonObject.get("prefLabel");
112  hstmp.put(uriValueTmp, narrowerLabelValue);
113  tmpAutosuggest.setNarrowerTerm(hstmp);
114  }
115  }
116  // get retcat info
117  String type = "unesco";
118  String quality = "";
119  String group = "";
120  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
121  if (item.getType().equals(type)) {
122  quality = item.getQuality();
123  group = item.getGroup();
124  }
125  }
126  tmpAutosuggest.setType(type);
127  tmpAutosuggest.setQuality(quality);
128  tmpAutosuggest.setGroup(group);
129  }
130  return autosuggests;
131  }

References GeneralFunctions.encodeURIComponent(), RetcatResource.getLimit(), and LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getQueryResultsSkosmosUNESCO().

Exception