updating search for vivo-1011

This commit is contained in:
hudajkhan 2015-04-07 12:37:20 -04:00
parent 03d1b34770
commit d2312a8e15
3 changed files with 162 additions and 127 deletions

View file

@ -104,78 +104,7 @@ public class AgrovocService implements ExternalConceptService {
} }
conceptList.add(c); conceptList.add(c);
} }
//Get the concept itself using Agrovoc's own service or OWL ontology manager
//System.out.println("uri: "+uri);
/*
SKOSDataset dataset = manager.loadDataset(uri);
for (SKOSConcept skosConcept : dataset.getSKOSConcepts()) {
Concept concept = new Concept();
//System.out.println("Concept: " + skosConcept.getURI());
concept.setUri(skosConcept.getURI().toString());
concept.setConceptId(stripConceptId(skosConcept.getURI().toString()));
concept.setBestMatch("true");
concept.setDefinedBy(schemeUri);
concept.setSchemeURI(this.schemeUri);
concept.setType("");
String lang = "";
for (SKOSLiteral literal : skosConcept.getSKOSRelatedConstantByProperty(dataset, manager.getSKOSDataFactory().getSKOSPrefLabelProperty())) {
if(literal != null) {
if (!literal.isTyped()) {
// if it has language
SKOSUntypedLiteral untypedLiteral = literal.getAsSKOSUntypedLiteral();
if (untypedLiteral.hasLang()) {
lang = untypedLiteral.getLang();
} else {
lang = "";
}
}
if (lang.equals("en")) {
//System.out.println("prefLabel: " + literal.getLiteral());
concept.setLabel(literal.getLiteral());
}
} else {
logger.debug("Literal returned for preferred label was null and was ignored");
}
}
// get altLabels
List<String> altLabelList = new ArrayList<String>();
for (SKOSLiteral literal : skosConcept
.getSKOSRelatedConstantByProperty(dataset, manager
.getSKOSDataFactory().getSKOSAltLabelProperty())) {
if(literal != null) {
if (!literal.isTyped()) {
// if it has language
SKOSUntypedLiteral untypedLiteral = literal
.getAsSKOSUntypedLiteral();
if (untypedLiteral.hasLang()) {
lang = untypedLiteral.getLang();
} else {
lang = "";
}
}
//System.out.println("literal: "+ literal.getLiteral());
if (lang.equals("en")) {
//System.out.println("altLabel: " + literal.getLiteral());
altLabelList.add(literal.getLiteral());
}
} else {
logger.debug("Literal retrieved for altlabel was null and was ignored");
}
}
}*/
return conceptList; return conceptList;
} }
@ -187,7 +116,6 @@ public class AgrovocService implements ExternalConceptService {
public Concept createConcept(String bestMatch, String skosConceptURI) { public Concept createConcept(String bestMatch, String skosConceptURI) {
Concept concept = new Concept(); Concept concept = new Concept();
//System.out.println("Concept: " + skosConcept.getURI());
concept.setUri(skosConceptURI); concept.setUri(skosConceptURI);
concept.setConceptId(stripConceptId(skosConceptURI)); concept.setConceptId(stripConceptId(skosConceptURI));
concept.setBestMatch(bestMatch); concept.setBestMatch(bestMatch);
@ -202,7 +130,7 @@ public class AgrovocService 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, url, "abbreviated", "en"); concept = SKOSUtils.createConceptUsingXMLFromURI(concept, url, "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

@ -150,7 +150,7 @@ 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 {
//LCSH doesn't need a language tag right now as results in english //LCSH doesn't need a language tag right now as results in english
concept = SKOSUtils.createConceptUsingXMLFromURI(concept, skosConceptURI, "abbreviated", null); concept = SKOSUtils.createConceptUsingXMLFromURI(concept, skosConceptURI, 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 langTagValue) { String conceptUriString, 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, langTagValue); return createConceptUsingXML(concept, results, langTagValue);
} }
@ -85,9 +85,9 @@ public class SKOSUtils {
// Lang tag value, if populated, will return pref label and alt label which // Lang tag value, if populated, will return pref label and alt label which
// match that language tag value // match that language tag value
public static Concept createConceptUsingXML(Concept concept, public static Concept createConceptUsingXML(Concept concept,
String results, String relationshipScheme, String langTagValue) { String results, String langTagValue) {
HashMap<String, String> relationshipHash = getRelationshipHash(relationshipScheme); HashMap<String, String> relationshipHash = getRelationshipHash();
try { try {
Document doc = XMLUtils.parse(results); Document doc = XMLUtils.parse(results);
// Preferred label // Preferred label
@ -124,18 +124,19 @@ public class SKOSUtils {
//Broder, narrower, exact match, and close match properties //Broder, narrower, exact match, and close match properties
List<String> broaderURIList = getValuesFromXMLNodes(doc, String conceptURI = concept.getUri();
getBroaderTag(relationshipHash), "rdf:resource"); List<String> broaderURIList = getBroaderOrNarrowerURIs(doc, getBroaderTag(relationshipHash));
broaderURIList = removeConceptURIFromList(broaderURIList, conceptURI);
concept.setBroaderURIList(broaderURIList); concept.setBroaderURIList(broaderURIList);
List<String> narrowerURIList = getValuesFromXMLNodes(doc, List<String> narrowerURIList = getBroaderOrNarrowerURIs(doc, getNarrowerTag(relationshipHash));
getNarrowerTag(relationshipHash), "rdf:resource"); narrowerURIList = removeConceptURIFromList(narrowerURIList, conceptURI);
concept.setNarrowerURIList(narrowerURIList); concept.setNarrowerURIList(narrowerURIList);
List<String> exactMatchURIList = getValuesFromXMLNodes(doc, List<String> exactMatchURIList = getCloseOrExactMatchURIs(doc, getExactMatchTag(relationshipHash));
getExactMatchTag(relationshipHash), "rdf:resource"); exactMatchURIList = removeConceptURIFromList(exactMatchURIList, conceptURI);
concept.setExactMatchURIList(exactMatchURIList); concept.setExactMatchURIList(exactMatchURIList);
List<String> closeMatchURIList = getValuesFromXMLNodes(doc, List<String> closeMatchURIList = getCloseOrExactMatchURIs(doc, getCloseMatchTag(relationshipHash));
getCloseMatchTag(relationshipHash), "rdf:resource"); closeMatchURIList = removeConceptURIFromList(closeMatchURIList, conceptURI);
concept.setCloseMatchURIList(closeMatchURIList); concept.setCloseMatchURIList(closeMatchURIList);
} catch (IOException e) { } catch (IOException e) {
@ -149,6 +150,15 @@ public class SKOSUtils {
return concept; return concept;
} }
//Because of the fact the xml returns matches by tag name, and the XML may look like <skos:narrower><skos:Concept ..><skos:broader rdf:resource:"conceptURI">
//where conceptURI is the concept that is the subject of skos:narrower, we need to ensure we are not returning the same uri as that of the main concept
public static List<String> removeConceptURIFromList(List<String> uris, String conceptURI) {
//remove will return a boolean if the value exists in the list and is removed
//if/when it returns false, the URI is not in the list
while(uris.remove(conceptURI)) {};
return uris;
}
// Default to English for search results but this should be made // Default to English for search results but this should be made
// configurable // configurable
@ -160,7 +170,7 @@ public class SKOSUtils {
public static List<String> getValuesFromXMLNodes(Document doc, public static List<String> getValuesFromXMLNodes(Document doc,
String tagName, String attributeName, String matchAttributeValue) { String tagName, String attributeName, String matchAttributeValue) {
NodeList nodes = doc.getElementsByTagName(tagName); NodeList nodes = doc.getElementsByTagName(tagName);
return getValuesFromXML(nodes, attributeName, matchAttributeValue); return getValuesFromXML(nodes, attributeName, matchAttributeValue);
} }
@ -181,32 +191,41 @@ public class SKOSUtils {
List<String> values = new ArrayList<String>(); List<String> values = new ArrayList<String>();
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
Node node = nodes.item(i); Node node = nodes.item(i);
// If no attribute name specified, then get the node content String nodeValue = getNodeValue(node, attributeName, matchAttributeValue);
if (StringUtils.isEmpty(attributeName)) { if(StringUtils.isNotEmpty(nodeValue)) {
values.add(node.getTextContent()); values.add(nodeValue);
} else {
// Attribute name is specified
// Get the value for the attribute itself
String attributeValue = getAttributeValue(attributeName, node);
// If no matching value for attribute specified, return the
// value of the attribute itself
// e.g. value of "lang" attribute which is "en"
if (StringUtils.isEmpty(matchAttributeValue)) {
values.add(attributeValue);
} else {
// match attribute and match value are both specified, so
// return NODE value that matches attribute value for given
// attribute name
// e.g. preferred label node value where lang = "en"
if (attributeValue.equals(matchAttributeValue)) {
values.add(node.getTextContent());
}
}
} }
} }
return values; return values;
} }
public static String getNodeValue(Node node, String attributeName, String matchAttributeValue) {
String value = null;
if (StringUtils.isEmpty(attributeName)) {
value = node.getTextContent();
} else {
// Attribute name is specified
// Get the value for the attribute itself
String attributeValue = getAttributeValue(attributeName, node);
// If no matching value for attribute specified, return the
// value of the attribute itself
// e.g. value of "lang" attribute which is "en"
if (StringUtils.isEmpty(matchAttributeValue)) {
value = attributeValue;
} else {
// match attribute and match value are both specified, so
// return NODE value that matches attribute value for given
// attribute name
// e.g. preferred label node value where lang = "en"
if (attributeValue.equals(matchAttributeValue)) {
value = node.getTextContent();
}
}
}
return value;
}
public static String getAttributeValue(String attributeName, Node node) { public static String getAttributeValue(String attributeName, Node node) {
NamedNodeMap attrs = node.getAttributes(); NamedNodeMap attrs = node.getAttributes();
Attr a = (Attr) attrs.getNamedItem(attributeName); Attr a = (Attr) attrs.getNamedItem(attributeName);
@ -218,26 +237,18 @@ public class SKOSUtils {
// The Hash will depend on the particular RDF results // The Hash will depend on the particular RDF results
// TODO: Refactor this in a better method // TODO: Refactor this in a better method
public static HashMap<String, String> getRelationshipHash(String tagset) { public static HashMap<String, String> getRelationshipHash() {
HashMap<String, String> relationshipHash = new HashMap<String, String>(); HashMap<String, String> relationshipHash = new HashMap<String, String>();
String[] tagsArray = { "prefLabel", "altLabel", "broader", "narrower", String[] tagsArray = { "prefLabel", "altLabel", "broader", "narrower",
"exactMatch", "closeMatch" }; "exactMatch", "closeMatch" };
List<String> tags = Arrays.asList(tagsArray); List<String> tags = Arrays.asList(tagsArray);
switch (tagset) {
case "xmlns":
for (String tag : tags) {
relationshipHash.put(tag, tag);
}
break;
case "abbreviated":
for (String tag : tags) { for (String tag : tags) {
relationshipHash.put(tag, "skos:" + tag); relationshipHash.put(tag, "skos:" + tag);
} }
break;
default:
break;
}
return relationshipHash; return relationshipHash;
} }
@ -270,14 +281,110 @@ public class SKOSUtils {
} }
//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) { * Broader, narrower, close match, and exact match may be nested values - e.g. Agrovoc
return null; * Even with Agrovoc, they may be nested sometimes and not be nested other times
* The code below handles both situations so the URIs can be returned
*/
//Broader and narrower values
//Attribute name will be language tag
public static List<String> getBroaderOrNarrowerURIs(Document doc,
String tagName) {
NodeList nodes = doc.getElementsByTagName(tagName);
List<String> uris = getPossiblyNestedValuesFromXML(nodes, "rdf:resource", "skos:Concept", "rdf:about");
return uris;
}
//Close and exact match
public static List<String> getCloseOrExactMatchURIs(Document doc,
String tagName) {
NodeList nodes = doc.getElementsByTagName(tagName);
List<String> uris = getPossiblyNestedValuesFromXML(nodes, "rdf:resource", "rdf:Description", "rdf:about");
return uris;
} }
//get about URI from <tag><skos:Concept about="x">, returns "x"
public static String getTagNestedSKOSConceptAbout(Node n) {
return null; public static List<String> getPossiblyNestedValuesFromXML(NodeList nodes, String nodeAttributeName, String childNodeTagName, String childNodeAttributeName ) {
int len = nodes.getLength();
int i;
List<String> values = new ArrayList<String>();
for (i = 0; i < len; i++) {
Node node = nodes.item(i);
//String nodeValue = getNodeValue(node, attributeName, matchAttributeValue);
String nodeValue = getPossiblyNestedNodeValue(node, nodeAttributeName, childNodeTagName, childNodeAttributeName);
if(StringUtils.isNotEmpty(nodeValue)) {
values.add(nodeValue);
}
}
return values;
} }
//Given node = <tag attrb="attrbvalue">
//If tag has no attribute that matches attributeName with attributevalue
//and tag has nested children with a given tag name, i.e. <tag><nestedtag nestedattributename=nestedattributevalue>
//then retrieve the nested attribute value
//For example:
//if the node looks like <skos:closeMatch rdf:resource="x"> then get x
//but if the node looks like <skos:closeMatch><rdf:description rdf:about="x"> then get x
public static String getPossiblyNestedNodeValue(Node node, String nodeAttributeName, String childNodeTagName,
String childNodeAttributeName) {
String value = null;
String attributeValue = getAttributeValue(nodeAttributeName, node);
if(StringUtils.isNotEmpty(attributeValue)) {
value = attributeValue;
} else {
//Check child nodes and see if any of those have the same name as childNodeTagName
NodeList childNodes = node.getChildNodes();
int numberNodes = childNodes.getLength();
int i;
for(i = 0; i < numberNodes; i++) {
Node childNode = childNodes.item(i);
String nodeName = childNode.getNodeName();
if(nodeName.equals(childNodeTagName)) {
value = getAttributeValue(childNodeAttributeName, childNode);
break; //will only get the first one
}
}
}
return value;
}
//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) {
NodeList childNodes = n.getChildNodes();
int numberNodes = childNodes.getLength();
int i;
for(i = 0; i < numberNodes; i++) {
Node childNode = childNodes.item(i);
String nodeName = childNode.getNodeName();
String aboutValue = getAttributeValue("about", childNode);
}
return null;
}
//get about URI from <tag><skos:Concept about="x">, returns "x"
public static String getTagNestedSKOSConceptAbout(Node n) {
NodeList childNodes = n.getChildNodes();
int numberNodes = childNodes.getLength();
int i;
for(i = 0; i < numberNodes; i++) {
Node childNode = childNodes.item(i);
String nodeName = childNode.getNodeName();
String aboutValue = getAttributeValue("about", childNode);
}
return null;
}
} }