Skip to content

Commit

Permalink
add context and unit parameters to ESOR concept lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
leinfelder committed Apr 5, 2016
1 parent 0ad6920 commit 3d078a1
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void initializeCache() {
OntDocumentManager.getInstance().addModel(dcterms, ModelFactory.createOntologyModel().read(dcterms_source));
OntDocumentManager.getInstance().addModel(foaf, ModelFactory.createOntologyModel().read(foaf_source));
OntDocumentManager.getInstance().addModel(prov, ModelFactory.createOntologyModel().read(prov));
OntDocumentManager.getInstance().addModel(cito, ModelFactory.createOntologyModel().read(cito));
//OntDocumentManager.getInstance().addModel(cito, ModelFactory.createOntologyModel().read(cito));
cacheInitialized = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ private Map<Identifier, String> generateAnnotationsFromEML(Identifier metadataPi
DataPackage dataPackage = this.getDataPackage(metadataPid);
SystemMetadata sysMeta = D1Client.getCN().getSystemMetadata(null, metadataPid);

// TODO: use abstract content for context
String context = dataPackage.getTitle();

Map<Identifier, String> annotations = new HashMap<Identifier, String>();

// loop through the tables and attributes
Expand Down Expand Up @@ -184,7 +187,7 @@ private Map<Identifier, String> generateAnnotationsFromEML(Identifier metadataPi


// look up concepts for all the attribute text we have
List<ConceptItem> concepts = conceptMatcher.getConcepts(attributeText.toString());
List<ConceptItem> concepts = conceptMatcher.getConcepts(attributeText.toString(), attributeUnit, context);
if (concepts != null && concepts.size() > 0) {

// debug
Expand Down Expand Up @@ -238,7 +241,7 @@ private Map<Identifier, String> generateAnnotationsFromEML(Identifier metadataPi

// use an orcid if we can find one from their system
String creatorText = creators.get(0).getOrganization() + " " + creators.get(0).getSurName() + " " + creators.get(0).getGivenNames();
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText);
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText, null, null);
if (concepts != null) {
JSONObject annotation = createAnnotationTemplate(sysMeta);
JSONArray creatorTags = new JSONArray();
Expand Down Expand Up @@ -370,7 +373,9 @@ private Map<Identifier, String> generateAnnotationsFromIndex(Identifier metadata
}

SystemMetadata sysMeta = D1Client.getCN().getSystemMetadata(null, metadataPid);


String context = solrDoc.get("abstract").toString();

Map<Identifier, String> annotations = new HashMap<Identifier, String>();

// loop through the attributes
Expand Down Expand Up @@ -423,7 +428,7 @@ private Map<Identifier, String> generateAnnotationsFromIndex(Identifier metadata
annotation.put("ranges", ranges);

// look up concepts for all the attribute text we have
List<ConceptItem> concepts = conceptMatcher.getConcepts(attributeName);
List<ConceptItem> concepts = conceptMatcher.getConcepts(attributeName, null, context);
if (concepts != null && concepts.size() > 0) {
// add the tags
JSONArray tags = new JSONArray();
Expand Down Expand Up @@ -462,7 +467,7 @@ private Map<Identifier, String> generateAnnotationsFromIndex(Identifier metadata
String creator = creators.get(0).toString();
// use an orcid if we can find one from their system
String creatorText = creator;
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText);
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText, null, null);
if (concepts != null && concepts.size() > 0) {
JSONObject annotation = createAnnotationTemplate(sysMeta);
JSONArray creatorTags = new JSONArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Map<Identifier, String> generateAnnotations(Identifier metadataPid) throw
if (creators != null && creators.size() > 0) {
// use an orcid if we can find one from their system
String creatorText = creators.get(0).getOrganization() + " " + creators.get(0).getSurName() + " " + creators.get(0).getGivenNames();
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText);
List<ConceptItem> concepts = orcidMatcher.getConcepts(creatorText, null, null);
if (concepts != null) {
String orcidUri = concepts.get(0).getUri().toString();
p1 = m.createIndividual(orcidUri, personClass);
Expand Down Expand Up @@ -259,7 +259,7 @@ private Resource lookupStandard(OntClass standardClass, Attribute attribute) thr
String unit = attribute.getUnit().toLowerCase();

// look up the concept using the matcher
List<ConceptItem> concepts = conceptMatcher.getConcepts(unit);
List<ConceptItem> concepts = conceptMatcher.getConcepts(unit, null, null);
return ResourceFactory.createResource(concepts.get(0).getUri().toString());
//return BioPortalService.lookupAnnotationClass(standardClass, unit, OBOE_SBC);
}
Expand All @@ -271,7 +271,7 @@ private Resource lookupCharacteristic(OntClass characteristicClass, Attribute at
String text = label + " " + definition;

// look up the concept using the matcher
List<ConceptItem> concepts = conceptMatcher.getConcepts(text);
List<ConceptItem> concepts = conceptMatcher.getConcepts(text, null, null);
return ResourceFactory.createResource(concepts.get(0).getUri().toString());
//return BioPortalService.lookupAnnotationClass(characteristicClass, text, OBOE_SBC);

Expand All @@ -283,7 +283,7 @@ private Resource lookupEntity(OntClass entityClass, Entity entity) throws Except
String definition = entity.getDefinition();

// look up the concept using the matcher
List<ConceptItem> concepts = conceptMatcher.getConcepts(definition);
List<ConceptItem> concepts = conceptMatcher.getConcepts(definition, null, null);
return ResourceFactory.createResource(concepts.get(0).getUri().toString());
//return BioPortalService.lookupAnnotationClass(entityClass, definition, OBOE_SBC);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface ConceptMatcher {

public List<ConceptItem> getConcepts(String fullText) throws Exception;
public List<ConceptItem> getConcepts(String fullText, String unit, String context) throws Exception;

public List<ConceptItem> getConcepts(Map<String, String> terms) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class ManualConceptMatcher implements ConceptMatcher {

@Override
public List<ConceptItem> getConcepts(String fullText) throws Exception {
public List<ConceptItem> getConcepts(String fullText, String unit, String context) throws Exception {
// TODO Auto-generated method stub
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BioPortalService() {


@Override
public List<ConceptItem> getConcepts(String text) throws Exception {
public List<ConceptItem> getConcepts(String text, String unit, String context) throws Exception {

// limit suggested annotations to MeasurementType subclasses.
//OntClass measurementTypeClass = mtg.getMeasurementTypeClass();
Expand All @@ -62,7 +62,7 @@ public List<ConceptItem> getConcepts(Map<String, String> queryItems) throws Exce
sb.append(value);
sb.append(" ");
}
return getConcepts(sb.toString());
return getConcepts(sb.toString(), null, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EsorClient {
public static void main(String[] args) throws Exception{
EsorService esorS = new EsorService();

List<ConceptItem> res = esorS.getConcepts("Litterfall");
List<ConceptItem> res = esorS.getConcepts("Litterfall", null, null);
//List<ConceptItem> res = esorS.getConcepts("carbon%20mass");
//List<ConceptItem> res = esorS.getConcepts("carbon,mass");

Expand Down
31 changes: 23 additions & 8 deletions src/main/java/org/dataone/annotator/matcher/esor/EsorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.dataone.annotator.matcher.ConceptItem;
import org.dataone.annotator.matcher.ConceptMatcher;
import org.dataone.annotator.matcher.QueryItem;
Expand All @@ -38,17 +41,17 @@ public EsorService() {


@Override
public List<ConceptItem> getConcepts(String fullText) throws Exception {
public List<ConceptItem> getConcepts(String fullText, String unit, String context) throws Exception {
//merge two results with different escape condition
String query = parseTerm(fullText);
String escapedSpaceQuery = escapeToSpace(query);
String escapedCommaQuery = escapeToComma(query);

if(true){
return lookupEsor(escapedSpaceQuery);
return lookupEsor(escapedSpaceQuery, unit, context);
}else{
List<ConceptItem> res_escapedSpace = lookupEsor(escapedSpaceQuery);
List<ConceptItem> res_escapedComma = lookupEsor(escapedCommaQuery);
List<ConceptItem> res_escapedSpace = lookupEsor(escapedSpaceQuery, unit, context);
List<ConceptItem> res_escapedComma = lookupEsor(escapedCommaQuery, unit, context);
return mergeRes(res_escapedSpace, res_escapedComma);
}

Expand All @@ -63,10 +66,10 @@ public List<ConceptItem> getConcepts(Map<String, String> queryItems) throws Exce
sb.append(" ");
}
//return lookupEsor(sb.toString());
return getConcepts(sb.toString());
return getConcepts(sb.toString(), null, null);
}

private List<ConceptItem> lookupEsor(String query) throws Exception {
private List<ConceptItem> lookupEsor(String query, String unit, String context) throws Exception {

HttpClient client = HttpClients.createDefault();
// remove quotes for now
Expand All @@ -75,11 +78,23 @@ private List<ConceptItem> lookupEsor(String query) throws Exception {
//String uriStr = REST_URL + "?query=" + URLEncoder.encode(query, "UTF-8");
String uriStr = REST_URL;
//uriStr += "?minScore=2&numResult=10";
uriStr += "?query=" + query;
//uriStr += "?query=" + query;
log.debug("uriStr=" + uriStr);

HttpGet method = new HttpGet(uriStr);
// use post to handle potentially long parameter values
HttpPost method = new HttpPost(uriStr);
method.setHeader("Accept", "*/*");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("query", query));
if (context != null) {
postParameters.add(new BasicNameValuePair("context", context));
}
if (unit != null) {
postParameters.add(new BasicNameValuePair("unit", unit));
}
//postParameters.add(new BasicNameValuePair("minScore", "2"));
//postParameters.add(new BasicNameValuePair("numResult", "10"));

HttpResponse response = client.execute(method);
int code = response.getStatusLine().getStatusCode();
if (2 != code / 100) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class OrcidService implements ConceptMatcher {
private static final String REST_URL = "http://pub.orcid.org/v1.1/search/orcid-bio";

@Override
public List<ConceptItem> getConcepts(String text) throws Exception {
public List<ConceptItem> getConcepts(String text, String unit, String context) throws Exception {
return lookupOrcid(text, null, null, null);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testLookup() {
String text = "carbon flux";
BioPortalService service = new BioPortalService();
List<ConceptItem> results;
results = service.getConcepts(text);
results = service.getConcepts(text, null, null);
String retConcept = results.get(0).getUri().toString();
assertEquals("http://purl.dataone.org/odo/ECSO_00000011", retConcept);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testLookup() {
String text = "carbon flux";
EsorService service = new EsorService();
List<ConceptItem> results;
results = service.getConcepts(text);
results = service.getConcepts(text, null, null);
String retConcept = results.get(0).getUri().toString();
System.out.println("retConcept=" + retConcept);
assertEquals("http://purl.dataone.org/odo/ECSO_00000011", retConcept);
Expand Down

0 comments on commit 3d078a1

Please sign in to comment.