Skip to Navigation
Home
OpenCalais
  • How Does Calais Work?
  • News Room
  • Blog
  • Showcase
  • Documentation
  • Calais Community
  • Register
  • Calais Products
  • Home
  • Feeds
  • FAQ
  • Forums
  • Contact Us
Login | Request API Key
Home ›
Code Sample for Relevance in JSON

Submitted by FranSan on Tue, 09/02/2008 - 09:28.
in
  • Java
  • JSON
  • Official Gallery Item
  • Relevance

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);
            }

AttachmentSize
relevance_R4.zip9.05 KB

Home | Privacy | Terms of Service | We're in Beta+ | Site Map

© 2008, 2009, 2010, 2011 Thomson Reuters