labelingsystem-server  Version 0.1.0.0
Retcat_Dbpedia 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
73  {
74  try {
75  String outputUrl = url;
76  url = url.replace("resource", "data");
77  url = url + ".json";
78  URL obj = new URL(url);
79  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
80  con.setRequestMethod("GET");
81  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
82  String inputLine;
83  StringBuilder response = new StringBuilder();
84  while ((inputLine = in.readLine()) != null) {
85  response.append(inputLine);
86  }
87  in.close();
88  // parse json
89  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
90  JSONObject thisValue = (JSONObject) jsonObject.get(outputUrl);
91  JSONArray nameArray = (JSONArray) thisValue.get("http://www.w3.org/2000/01/rdf-schema#label");
92  String name = "";
93  String lang = "";
94  for (Object element : nameArray) {
95  JSONObject tmpObj = (JSONObject) element;
96  String langTmp = (String) tmpObj.get("lang");
97  if (langTmp.equals("en")) {
98  name = (String) tmpObj.get("value");
99  lang = (String) tmpObj.get("lang");
100  }
101  }
102  if (name.equals("")) {
103  JSONObject tmpObj = (JSONObject) nameArray.get(0);
104  name = (String) tmpObj.get("value");
105  lang = (String) tmpObj.get("lang");
106  }
107  JSONArray abstractArray = (JSONArray) thisValue.get("http://dbpedia.org/ontology/abstract");
108  String desc = "";
109  if (abstractArray != null) {
110  for (Object element : abstractArray) {
111  JSONObject tmpObj = (JSONObject) element;
112  String langTmp = (String) tmpObj.get("lang");
113  if (langTmp.equals("en")) {
114  desc = (String) tmpObj.get("value");
115  }
116  }
117  if (desc.equals("")) {
118  JSONObject tmpObj = (JSONObject) abstractArray.get(0);
119  desc = (String) tmpObj.get("value");
120  }
121  }
122  // output
123  JSONObject jsonOut = new JSONObject();
124  jsonOut.put("label", name);
125  jsonOut.put("lang", lang);
126  // get retcat info
127  String type = "dbpedia";
128  String quality = "";
129  String group = "";
130  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
131  if (item.getType().equals(type)) {
132  quality = item.getQuality();
133  group = item.getGroup();
134  type = item.getType();
135  }
136  }
137  jsonOut.put("type", type);
138  jsonOut.put("quality", quality);
139  jsonOut.put("group", group);
140  jsonOut.put("description", desc);
141  jsonOut.put("uri", outputUrl);
142  jsonOut.put("scheme", "DBpedia");
143  // broader and narrower
144  JSONArray broaderTerms = new JSONArray();
145  JSONArray narrowerTerms = new JSONArray();
146  jsonOut.put("broaderTerms", broaderTerms);
147  jsonOut.put("narrowerTerms", narrowerTerms);
148  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
149  return jsonOut;
150  } else {
151  throw new RetcatException("no label for this uri available");
152  }
153  } catch (Exception e) {
154  return new JSONObject();
155  }
156  }

References LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getInfoDBpedia(), and RetcatResource.getQueryResultsDBPEDIA().

◆ 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://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + searchword + "&MaxHits=" + RetcatResource.getLimit();
30  URL url = new URL(url_string);
31  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
32  conn.setRequestProperty("Accept", "application/json");
33  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
34  String inputLine;
35  StringBuilder response = new StringBuilder();
36  while ((inputLine = br.readLine()) != null) {
37  response.append(inputLine);
38  }
39  br.close();
40  // fill objects
41  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
42  JSONArray resultsArray = (JSONArray) jsonObject.get("results");
43  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
44  for (Object element : resultsArray) {
45  JSONObject tmpElement = (JSONObject) element;
46  String uriValue = (String) tmpElement.get("uri");
47  autosuggests.put(uriValue, new SuggestionItem(uriValue));
48  SuggestionItem tmpAutosuggest = autosuggests.get(uriValue);
49  String labelValue = (String) tmpElement.get("label");
50  tmpAutosuggest.setLabel(labelValue);
51  String descriptionValue = (String) tmpElement.get("description");
52  if (descriptionValue != null) {
53  tmpAutosuggest.setDescription(descriptionValue);
54  }
55  tmpAutosuggest.setSchemeTitle("DBpedia");
56  // get retcat info
57  String type = "dbpedia";
58  String quality = "";
59  String group = "";
60  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
61  if (item.getType().equals(type)) {
62  quality = item.getQuality();
63  group = item.getGroup();
64  }
65  }
66  tmpAutosuggest.setType(type);
67  tmpAutosuggest.setQuality(quality);
68  tmpAutosuggest.setGroup(group);
69  }
70  return autosuggests;
71  }

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

Referenced by RetcatResource.getQueryResultsDBPEDIA().

Exception