updating Agrovoc and LCSH implementations per vivo-1011

This commit is contained in:
hudajkhan 2015-04-06 15:49:59 -04:00
parent bade3f147d
commit 03d1b34770
3 changed files with 46 additions and 34 deletions

View file

@ -25,13 +25,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.fao.www.webservices.AgrovocWS.ACSWWebService; import org.fao.www.webservices.AgrovocWS.ACSWWebService;
import org.fao.www.webservices.AgrovocWS.ACSWWebServiceServiceLocator; import org.fao.www.webservices.AgrovocWS.ACSWWebServiceServiceLocator;
import org.semanticweb.skos.SKOSAnnotation;
import org.semanticweb.skos.SKOSConcept;
import org.semanticweb.skos.SKOSDataset;
import org.semanticweb.skos.SKOSEntity;
import org.semanticweb.skos.SKOSLiteral;
import org.semanticweb.skos.SKOSUntypedLiteral;
import org.semanticweb.skosapibinding.SKOSManager;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
@ -53,20 +46,20 @@ import edu.cornell.mannlib.semservices.bo.Concept;
import edu.cornell.mannlib.semservices.service.ExternalConceptService; import edu.cornell.mannlib.semservices.service.ExternalConceptService;
import edu.cornell.mannlib.semservices.util.SKOSUtils; import edu.cornell.mannlib.semservices.util.SKOSUtils;
import edu.cornell.mannlib.semservices.util.XMLUtils; import edu.cornell.mannlib.semservices.util.XMLUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class AgrovocService implements ExternalConceptService { public class AgrovocService implements ExternalConceptService {
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private java.lang.String AgrovocWS_address = "http://agrovoc.fao.org/axis/services/SKOSWS"; private java.lang.String AgrovocWS_address = "http://agrovoc.fao.org/axis/services/SKOSWS";
private final String schemeUri = "http://aims.fao.org/aos/agrovoc/agrovocScheme"; private final String schemeUri = "http://aims.fao.org/aos/agrovoc/agrovocScheme";
private final String baseUri = "http://aims.fao.org/aos/agrovoc/";
private final String ontologyName = "agrovoc"; private final String ontologyName = "agrovoc";
private final String format = "SKOS"; private final String format = "SKOS";
private final String lang = "en"; private final String lang = "en";
private final String codeName = "hasCodeAgrovoc";
private final String searchMode = "Exact Match"; private final String searchMode = "Exact Match";
protected final String dbpedia_endpoint = " http://dbpedia.org/sparql"; protected final String dbpedia_endpoint = " http://dbpedia.org/sparql";
//URL to get all the information for a concept
protected final String conceptSkosMosURL = "http://aims.fao.org/skosmos/rest/v1/agrovoc/data?";
@Override @Override
public List<Concept> getConcepts(String term) throws Exception { public List<Concept> getConcepts(String term) throws Exception {
@ -94,25 +87,25 @@ public class AgrovocService implements ExternalConceptService {
} }
//Returns concept information in the format specified, which is currently XML //Returns concept information in the format specified, which is currently XML
//This will return //Utilizing Agrovoc's getConceptInfo returns alternate and preferred labels but
String conceptInfo = this.getConceptInfoByURI(this.ontologyName, conceptUri, this.format); //none of the exact match or close match descriptions
if(StringUtils.isNotEmpty(conceptInfo)) {
Concept c = this.createConcept("true", conceptUri, conceptInfo); Concept c = this.createConcept("true", conceptUri);
if(c != null) { if(c != null) {
//Get definition from dbpedia references stored in the close Match list //Get definition from dbpedia references stored in the close Match list
List<String> closeMatches = c.getCloseMatchURIList(); List<String> closeMatches = c.getCloseMatchURIList();
for(String closeMatch: closeMatches) { for(String closeMatch: closeMatches) {
if (closeMatch.startsWith("http://dbpedia.org")) { if (closeMatch.startsWith("http://dbpedia.org")) {
String description = getDbpediaDescription(closeMatch); String description = getDbpediaDescription(closeMatch);
//System.out.println("description: "+ description); //System.out.println("description: "+ description);
c.setDefinition(description); c.setDefinition(description);
} }
}
conceptList.add(c);
} }
conceptList.add(c);
} }
//Get the concept itself using Agrovoc's own service or OWL ontology manager //Get the concept itself using Agrovoc's own service or OWL ontology manager
@ -191,7 +184,7 @@ public class AgrovocService implements ExternalConceptService {
} }
public Concept createConcept(String bestMatch, String skosConceptURI, String results) { public Concept createConcept(String bestMatch, String skosConceptURI) {
Concept concept = new Concept(); Concept concept = new Concept();
//System.out.println("Concept: " + skosConcept.getURI()); //System.out.println("Concept: " + skosConcept.getURI());
@ -201,12 +194,15 @@ public class AgrovocService implements ExternalConceptService {
concept.setDefinedBy(schemeUri); concept.setDefinedBy(schemeUri);
concept.setSchemeURI(this.schemeUri); concept.setSchemeURI(this.schemeUri);
concept.setType(""); concept.setType("");
String lang = "";
//Will need to get the language attribute String encodedURI = URLEncoder.encode(skosConceptURI);
String encodedFormat = URLEncoder.encode("application/rdf+xml");
String url = conceptSkosMosURL + "uri=" + encodedURI + "&format="+ encodedFormat;
//Utilize the XML directly instead of the SKOS API //Utilize the XML directly instead of the SKOS API
try { try {
concept = SKOSUtils.createConceptUsingXML(concept, results, "xmlns", "en");
concept = SKOSUtils.createConceptUsingXMLFromURI(concept, url, "abbreviated", "en");
} catch(Exception ex) { } catch(Exception ex) {
logger.debug("Error occurred for creating concept " + skosConceptURI, ex); logger.debug("Error occurred for creating concept " + skosConceptURI, ex);

View file

@ -149,7 +149,8 @@ public class LCSHService implements ExternalConceptService {
//Utilize the XML directly instead of the SKOS API //Utilize the XML directly instead of the SKOS API
try { try {
concept = SKOSUtils.createConceptUsingXMLFromURI(concept, skosConceptURI, "abbreviated"); //LCSH doesn't need a language tag right now as results in english
concept = SKOSUtils.createConceptUsingXMLFromURI(concept, skosConceptURI, "abbreviated", null);
} catch(Exception ex) { } catch(Exception ex) {
log.debug("Error occurred for annotation retrieval for skos concept " + skosConceptURI, ex); log.debug("Error occurred for annotation retrieval for skos concept " + skosConceptURI, ex);

View file

@ -71,12 +71,12 @@ public class SKOSUtils {
// Downloading the XML from the URI itself // Downloading the XML from the URI itself
//No language tag support here but can be specified if need be at this level as well //No language tag support here but can be specified if need be at this level as well
public static Concept createConceptUsingXMLFromURI(Concept concept, public static Concept createConceptUsingXMLFromURI(Concept concept,
String conceptUriString, String relationshipScheme) { String conceptUriString, String relationshipScheme, String langTagValue) {
String results = getConceptXML(conceptUriString); String results = getConceptXML(conceptUriString);
if (StringUtils.isEmpty(results)) { if (StringUtils.isEmpty(results)) {
return null; return null;
} }
return createConceptUsingXML(concept, results, relationshipScheme, null); return createConceptUsingXML(concept, results, relationshipScheme, langTagValue);
} }
@ -169,6 +169,10 @@ public class SKOSUtils {
// Attribute name returns the value for the attribute on the node // Attribute name returns the value for the attribute on the node
// MatchAttributeValue: returns NODE values that MATCH this value for // MatchAttributeValue: returns NODE values that MATCH this value for
// attributeName // attributeName
//Extending this based on specific SKOSMos search for Agrovoc, sometimes
//results in format <skos:closeMatch rdf:resource "x">, other times in format <skos:closeMatch> <rfd:Description rdf:about="matchURI">..
//closeMatch and exactMatch use these patterns
//broader and narrower may be either rdf:resource pattern above or <skos:broader><skos:Concept rdf:about="...">...etc.
public static List<String> getValuesFromXML(NodeList nodes, public static List<String> getValuesFromXML(NodeList nodes,
String attributeName, String matchAttributeValue) { String attributeName, String matchAttributeValue) {
int len = nodes.getLength(); int len = nodes.getLength();
@ -265,4 +269,15 @@ public class SKOSUtils {
return relationshipHash.get("narrower"); return relationshipHash.get("narrower");
} }
//Custom cases for Agrovoc and/or similar patterns if they exist
//get about URI from <tag> <rdf:Description about="x"> - returns "x"
public static String getTagNestedAbout(Node n) {
return null;
}
//get about URI from <tag><skos:Concept about="x">, returns "x"
public static String getTagNestedSKOSConceptAbout(Node n) {
return null;
}
} }