Skip to content

Commit

Permalink
Add support for ancestors from Lexigram API (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 f7a830d commit f7309a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/at/medunigraz/imi/bst/lexigram/Lexigram.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ public static List<String> addSynonymsFromBestConceptMatch(String label) {
return cleanUpList(keywordAndSynonyms);
}

/**
* Given a string, it searches for the best matching concept and returns all its ancestors.
* If no match is found, an empty list is returned.
*
* @param label
* @return
*/
public static List<String> getAncestorsFromBestConceptMatch(String label) {
List<String> ret = new ArrayList<>();

Optional<String> search = search(label);
if (!search.isPresent()) {
return ret;
}

return cleanUpList(ancestors(search.get()));
}

public static List<String> ancestors(String conceptId) {
// TODO pagination
JSONObject body = get(ENDPOINT + "concepts/" + conceptId + "/ancestors");

List<String> ancestors = new ArrayList<>();
JSONArray results = body.getJSONArray("results");
for(int i = 0; i < results.length(); i++) {
ancestors.add(cleanUpString(results.getJSONObject(i).getString("label")));
}

return ancestors;
}

public static Concept concept(String conceptId) {
/* Get info (label and synonyms) of concept */
JSONObject body = get(ENDPOINT + "concepts/" + conceptId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;


Expand Down Expand Up @@ -52,4 +54,20 @@ public void addSynonyms() {
Assert.assertThat(Lexigram.addSynonymsFromBestConceptMatch("notfoundlabel"),
containsInAnyOrder("notfoundlabel"));
}

@Test
public void getAncestorsFromBestConceptMatch() {
Assert.assertThat(Lexigram.getAncestorsFromBestConceptMatch("cholangiocarcinoma"),
hasItems("malignant neoplasm of digestive system",
"abdominal mass",
"epithelial neoplasm",
"disorder of biliary tract",
"neoplasm of digestive organ",
"finding of biliary tract",
"gastrointestinal tract finding")
);

Assert.assertThat(Lexigram.getAncestorsFromBestConceptMatch("notfoundlabel"),
empty());
}
}

0 comments on commit f7309a6

Please sign in to comment.