Skip to content

Commit

Permalink
DRY Wrap search API into its own method (refs bst-mug#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelole committed Jul 24, 2018
1 parent 66c9646 commit 0dccc5b
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/main/java/at/medunigraz/imi/bst/lexigram/Lexigram.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,15 @@ private static void save() {
/* Retrieves the preferred term ("label") of the best-matching concept.
* If no match, it returns itself */
public static String getPreferredTerm(String label) throws UnirestException {
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/search?q="+ URLEncoder.encode(label));
JSONArray results = body.getJSONArray("conceptSearchHits");
Optional<String> search = search(label);
if (!search.isPresent()) {
return label;
}

try {
if (results.length() == 0)
return label;
JSONObject item = results.getJSONObject(0);
JSONObject concept = item.getJSONObject("concept");
String conceptId = concept.getString("id");

/* Get info (label and synonyms) of concept */
body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ conceptId);

JSONObject body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ search.get());
return cleanUpString(body.getString("label"));

}
catch (Exception e) {
throw e;
Expand All @@ -91,23 +85,17 @@ public static String getPreferredTerm(String label) throws UnirestException {
/* Possibly the most useful function: Given a string, it searches for the best-matching concept and adds all synonyms
* If no match, it returns a list with itself */
public static List<String> addSynonymsFromBestConceptMatch(String label) throws UnirestException {
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/search?q=" + URLEncoder.encode(label));
JSONArray results = body.getJSONArray("conceptSearchHits");
Optional<String> search = search(label);
if (!search.isPresent()) {
return Collections.singletonList(label);
}

try {
List<String> keywordAndSynonyms = new ArrayList<>();
keywordAndSynonyms.add(label);

/* Get first concept (most relevant search hit) */
if (results.length() == 0)
return Collections.singletonList(label);
JSONObject item = results.getJSONObject(0);
JSONObject concept = item.getJSONObject("concept");
String conceptId = concept.getString("id");

/* Get info (label and synonyms) of concept */

body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ conceptId);
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ search.get());

keywordAndSynonyms.add(body.getString("label"));

Expand All @@ -126,6 +114,19 @@ public static List<String> addSynonymsFromBestConceptMatch(String label) throws
//return Collections.singletonList(label);
}

public static Optional<String> search(String label) throws UnirestException {
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/search?q="+ URLEncoder.encode(label));
JSONArray results = body.getJSONArray("conceptSearchHits");
if (results.length() == 0) {
return Optional.empty();
}

/* Get first concept (most relevant search hit) */
JSONObject item = results.getJSONObject(0);
JSONObject concept = item.getJSONObject("concept");
return Optional.of(concept.getString("id"));
}

private static List<String> cleanUpList(List<String> labels) {
Set<String> cleanLabels = new HashSet<>();
cleanLabels.addAll(labels.stream().map(String::toLowerCase).map(l -> l.replaceAll("\\(.*\\)", "")).collect(Collectors.toList()));
Expand Down

0 comments on commit 0dccc5b

Please sign in to comment.