How can I set up a transformation that creates a new field based on a literal value of another fi...

...eld and a lookup map?

My original data source contains ISO country codes that I would like to expand into full country names and add as an additional property to my original data source to facilitate concordance to Organization Authority.

Example data:


CTRYISOCD

CHN

CHN

JPN

SGP

ARG

DEU

BRA

MEX

Tagged:

Best Answer

  • The following SPARQL and transformation scripts would work in this case.

    SPARQL query

    SELECT * FROM NAMED <http://rdf.entagen.com/context/context_name&gt; WHERE 
    {
    GRAPH ?g { ?s <http://rdf.entagen.com/context/context_name/property/ISOCD&gt; ?o .
    }
    }

    Transformation code

    import org.openrdf.model.*
    import org.openrdf.model.impl.*
    import com.entagen.masterindex.api.*
    import com.entagen.masterindex.search.*

    /** Generates new RDF based on the Sparql query results.
    *
    */

    public Collection<Statement> transformAsRdf(Map<String, Value> values) {
    def countryMap = [
    ABW:"Aruba",AFG:"Afghanistan",AGO:"Angola",AIA:"Anguilla",ALA:"Åland Islands",ALB:"Albania",AND:"Andorra",ARE:"United Arab Emirates",ARG:"Argentina",ARM:"Armenia",ASM:"American Samoa",ATA:"Antarctica"
    ]

    def subject = values.s
    def predicate = new URIImpl("http://rdf.entagen.com/context/property/country_name")
    def object = new LiteralImpl(countryMap[values.o.stringValue()].toUpperCase()) ?: new LiteralImpl("UNKNOWN")
    def context = values.g

    return [new ContextStatementImpl(subject, predicate, object, context)]

    }

    /** Generates new Annotations based on the Sparql query results.
    *
    */

    public Collection<AnnotationDto> transformAsAnnotations(Map<String, Value> values) {
    return null
    }

    Explanation

    Values.o is an implementation of org.openrdf.model.Value

    It won’t work directly as a key lookup in the countryMap. We need to retrieve its string value by calling the values.o.stringValue() method first.

    Finally, we need to push it back into a Literal, so wrapping it in a new LiteralImpl().