Skip to content

Commit

Permalink
DRY Wrap concept 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 497e290 commit ddd7a1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
8 changes: 8 additions & 0 deletions src/main/java/at/medunigraz/imi/bst/lexigram/Concept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package at.medunigraz.imi.bst.lexigram;

import java.util.ArrayList;

public class Concept {
public String label;
public ArrayList<String> synonyms = new ArrayList<>();
}
45 changes: 20 additions & 25 deletions src/main/java/at/medunigraz/imi/bst/lexigram/Lexigram.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,7 @@ public static String getPreferredTerm(String label) throws UnirestException {
return label;
}

try {
/* Get info (label and synonyms) of concept */
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ search.get());
return cleanUpString(body.getString("label"));
}
catch (Exception e) {
throw e;
}
return concept(search.get()).label;
}

/**
Expand All @@ -100,26 +93,29 @@ public static List<String> addSynonymsFromBestConceptMatch(String label) throws
return Collections.singletonList(label);
}

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

/* Get info (label and synonyms) of concept */
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/concepts/"+ search.get());
Concept concept = concept(search.get());
keywordAndSynonyms.add(concept.label);
keywordAndSynonyms.addAll(concept.synonyms);

keywordAndSynonyms.add(body.getString("label"));
return cleanUpList(keywordAndSynonyms);
}

JSONArray synonymsJson = body.getJSONArray("synonyms");
public static Concept concept(String conceptId) throws UnirestException {
/* Get info (label and synonyms) of concept */
JSONObject body = get("https://api.lexigram.io/v1/lexigraph/concepts/" + conceptId);

for(int i = 0; i < synonymsJson.length(); i++) {
keywordAndSynonyms.add(synonymsJson.get(i).toString());
}
Concept concept = new Concept();
concept.label = cleanUpString(body.getString("label"));

return cleanUpList(keywordAndSynonyms);
}
catch (Exception e) {
throw e;
JSONArray synonymsJson = body.getJSONArray("synonyms");
for(int i = 0; i < synonymsJson.length(); i++) {
concept.synonyms.add(cleanUpString(synonymsJson.get(i).toString()));
}

return concept;
}

public static Optional<String> search(String label) throws UnirestException {
Expand All @@ -137,9 +133,8 @@ public static Optional<String> search(String label) throws UnirestException {

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()));
cleanLabels = cleanLabels.stream().map(l -> l.replaceAll("\\[.*\\]", "")).collect(Collectors.toSet());
cleanLabels = cleanLabels.stream().map(l -> l.split(",")[0]).collect(Collectors.toSet());
cleanLabels.addAll(labels.stream().map(Lexigram::cleanUpString).collect(Collectors.toSet()));

List<String> cleanLabelsArrayList = new ArrayList<>();
cleanLabelsArrayList.addAll(cleanLabels);
return cleanLabelsArrayList;
Expand Down

0 comments on commit ddd7a1f

Please sign in to comment.