The following code shows you how to extend your existing code to process the new relevance information in Calais RDF output. The attached zip file has the changed code of the JSON Java package, which includes relevance information in the full JSON representation.
The method that parses the relevance information is shown below (taken from com.clearforest.calais.full.CalaisJSONIf in the attached zip file).
/**
* Parse a description containing relevance information
* for an entity identified by Calais - add to
* the appropriate element in m_entities
* @param desc - the description containing the relevance info
*/
private void parseRelevanceInfo(Description desc) {
Element elem = null;
IdentifiedElement ielem = null;
float relevance = -1.0F;
Attribute attr =null;
int i = 0;
if (m_isLastErr)
{
return;
}
/*
* Go over all description elements and extract relevance.
* When c:subject is found (the id of
* the element whose relevance is described here) -
* set the relevance of the entity
*/
for (i = 0; i < desc.getNumChildren(); i++)
{
elem = desc.getChild(i);
if (elem.getTag().equals("c:subject"))
{
/*
* subject - the entity whose instance
* is being processed here
*/
attr = elem.getAttribute("rdf:resource");
if (attr == null)
{
err("Failed to parse RDF: c:subject element missing rdf:resource attribute");
return;
}
ielem = m_elementsHash.get(attr.getValue());
if (ielem == null)
{
err("Failed to parse RDF: RelevanceInfo refers to id " +
attr.getValue() + " - not found in hash");
return;
}
}
else if (elem.getTag().equals("c:relevance"))
{
/*
* Get the relevance value
*/
if (elem.getValue() == null)
{
err("Failed to parse RDF: RelevanceInfo with invalid relevance");
return;
}
relevance = Float.parseFloat(elem.getValueNotNull().trim());
}
}
/*
* Make sure all fields are there
*/
if (ielem == null || relevance == -1.0F)
{
err("Failed to parse RDF: RelevanceInfo with missing fields");
return;
}
((Entity)ielem).setRelevance(relevance);
}
| Attachment | Size |
|---|---|
| relevance_R4.zip | 9.05 KB |
