use of org.json.simple.parser.JSONParser in project cogcomp-nlp by CogComp.
the class QueryMQL method getCursorAndResponse.
public JSONObject getCursorAndResponse(String mqlQuery, String cursor) throws IOException, ParseException {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
url.put("query", mqlQuery);
url.put("key", apikey);
url.put("cursor", cursor);
logger.debug("QUERY URL: " + url.toString());
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
return response;
}
use of org.json.simple.parser.JSONParser in project cogcomp-nlp by CogComp.
the class QueryMQL method lookupMidFromTitle.
// public List<String> lookupType(String title) throws Exception{
// List<String> ans = new ArrayList<String>();
//
// if (this.title_types.containsKey(title)) {
// ans = title_types.get(title);
// return ans;
// } else {
// try{
// String mid = this.lookupMid(this.buildQuery(null, "/wikipedia/en", QueryMQL.encodeMQL(title)));
// MQLQueryWrapper mql = this.buildQuery(mid);
// JSONObject response;
// String mqlQuery = mql.MQLquery;
// response = getResponse(mqlQuery);
// JSONObject result = (JSONObject) response.get("result");
// if (result != null) {
// JSONArray types = (JSONArray) result.get("type");
// for (Object value : types) {
// ans.add(value.toString());
// }
// }
// } catch (HttpResponseException e){
// logger.info("title: "+title);
// e.printStackTrace();
// if(e.getStatusCode() == 403)
// System.exit(0);
// }
// }
// return ans;
// }
public String lookupMidFromTitle(MQLQueryWrapper mql) throws Exception {
JSONObject response;
String mqlQuery = mql.MQLquery;
logger.debug("QUERY IS " + mqlQuery);
String title = mql.value;
String checksum = getMD5Checksum(title);
// first check mid in cache
if (IOUtils.exists(typeCacheLocation + "/" + checksum + ".cached")) {
found++;
logger.info("Found! " + found);
JSONParser jsonParser = new JSONParser();
response = (JSONObject) jsonParser.parse(FileUtils.readFileToString(new File(typeCacheLocation + "/" + checksum + ".cached"), "UTF-8"));
} else {
response = getResponse(mqlQuery);
if (response == null)
return null;
cacheMiss++;
logger.info("Caching " + cacheMiss);
FileUtils.writeStringToFile(new File(typeCacheLocation + "/" + checksum + ".cached"), response.toString(), "UTF-8");
}
JSONObject result = (JSONObject) response.get("result");
if (result != null) {
return (String) result.get("mid");
}
return null;
}
use of org.json.simple.parser.JSONParser in project cogcomp-nlp by CogComp.
the class QueryMQL method lookupTypeFromTitle.
public List<String> lookupTypeFromTitle(MQLQueryWrapper mql) throws Exception {
List<String> ans = new ArrayList<String>();
JSONObject response;
String mqlQuery = mql.MQLquery;
logger.debug("QUERY IS " + mqlQuery);
String title = mql.value;
String checksum = getMD5Checksum(title);
// first check mid in cache
if (IOUtils.exists(typeCacheLocation + "/" + checksum + ".cached")) {
found++;
logger.info("Found! " + found);
JSONParser jsonParser = new JSONParser();
response = (JSONObject) jsonParser.parse(FileUtils.readFileToString(new File(typeCacheLocation + "/" + checksum + ".cached"), "UTF-8"));
} else {
response = getResponse(mqlQuery);
if (response == null)
return ans;
cacheMiss++;
logger.info("Caching " + cacheMiss);
FileUtils.writeStringToFile(new File(typeCacheLocation + "/" + checksum + ".cached"), response.toString(), "UTF-8");
}
JSONObject result = (JSONObject) response.get("result");
if (result != null) {
JSONArray types = (JSONArray) result.get("type");
for (Object value : types) {
ans.add(value.toString());
}
}
return ans;
}
use of org.json.simple.parser.JSONParser in project ddf by codice.
the class RrdMetricsRetrieverTest method testMetricsJsonDataWithCounter.
@Test
public void testMetricsJsonDataWithCounter() throws Exception {
String rrdFilename = TEST_DIR + "queryCount_Counter" + RRD_FILE_EXTENSION;
long endTime = new RrdFileBuilder().rrdFileName(rrdFilename).build();
MetricsRetriever metricsRetriever = new RrdMetricsRetriever();
String json = metricsRetriever.createJsonData("queryCount", rrdFilename, START_TIME, endTime);
JSONParser parser = new JSONParser();
JSONObject jsonObj = (JSONObject) parser.parse(json);
// Verify the title, totalCount, and data (i.e., samples) are present
assertThat(jsonObj.size(), equalTo(3));
assertThat(jsonObj.get("title"), not(nullValue()));
assertThat(jsonObj.get("totalCount"), not(nullValue()));
// Verify 2 samples were retrieved from the RRD file and put in the JSON fetch results
JSONArray samples = (JSONArray) jsonObj.get("data");
// 6 because that's the max num rows configured for
assertThat(samples.size(), equalTo(6));
// Verify each retrieved sample has a timestamp and value
for (int i = 0; i < samples.size(); i++) {
JSONObject sample = (JSONObject) samples.get(i);
LOGGER.debug("timestamp = {}, value= {}", (String) sample.get("timestamp"), sample.get("value"));
assertThat(sample.get("timestamp"), not(nullValue()));
assertThat(sample.get("value"), not(nullValue()));
}
}
use of org.json.simple.parser.JSONParser in project tika by apache.
the class NLTKNERecogniser method recognise.
/**
* recognises names of entities in the text
* @param text text which possibly contains names
* @return map of entity type -> set of names
*/
public Map<String, Set<String>> recognise(String text) {
Map<String, Set<String>> entities = new HashMap<>();
try {
String url = restHostUrlStr + "/nltk";
Response response = WebClient.create(url).accept(MediaType.TEXT_HTML).post(text);
int responseCode = response.getStatus();
if (responseCode == 200) {
String result = response.readEntity(String.class);
JSONParser parser = new JSONParser();
JSONObject j = (JSONObject) parser.parse(result);
Iterator<?> keys = j.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!key.equals("result")) {
ENTITY_TYPES.add(key);
entities.put(key.toUpperCase(Locale.ENGLISH), new HashSet((Collection) j.get(key)));
}
}
}
} catch (Exception e) {
LOG.debug(e.getMessage(), e);
}
return entities;
}
Aggregations