Search in sources :

Example 1 with EndpointException

use of com.bordercloud.sparql.EndpointException in project opentheso by miledrousset.

the class AlignmentQuery method queryWikidata.

// private getAlignementFromSkos(){
// }
// //////////////////////////////////////
// //////////////////////////////////////
// ////                     /////////////
// ////        SPARQL       /////////////
// ////                     /////////////
// //////////////////////////////////////
// //////////////////////////////////////
/**
 * Aligenement du thésaurus vers la source Wikidata en Sparql et en retour du Json
 * @param idC
 * @param idTheso
 * @param lexicalValue
 * @param lang
 * @param requete
 * @param source
 * @return
 */
public ArrayList<NodeAlignment> queryWikidata(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
    try {
        Endpoint sp = new Endpoint("https://query.wikidata.org/sparql", false);
        /*       String querySelect = "SELECT ?item ?itemLabel ?itemDescription WHERE {" +
                                    "  ?item rdfs:label \"fibula\"@en." +
                                    "  SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". }" +
                                    "}";*/
        requete = requete.replaceAll("##value##", lexicalValue);
        requete = requete.replaceAll("##lang##", lang);
        HashMap<String, HashMap> rs = sp.query(requete);
        if (rs == null)
            return null;
        listeAlign = new ArrayList<>();
        ArrayList<HashMap<String, Object>> rows_queryWikidata = (ArrayList) rs.get("result").get("rows");
        for (HashMap<String, Object> hashMap : rows_queryWikidata) {
            NodeAlignment na = new NodeAlignment();
            na.setInternal_id_concept(idC);
            na.setInternal_id_thesaurus(idTheso);
            // label ou Nom
            if (hashMap.get("itemLabel") != null)
                na.setConcept_target(hashMap.get("itemLabel").toString());
            else
                continue;
            // description
            if (hashMap.get("itemDescription") != null)
                na.setDef_target(hashMap.get("itemDescription").toString());
            else
                na.setDef_target("");
            na.setThesaurus_target(source);
            // URI
            if (hashMap.get("item") != null)
                na.setUri_target(hashMap.get("item").toString());
            else
                continue;
            listeAlign.add(na);
        /*  System.out.println("URI : " + hashMap.get("item"));
                System.out.println("URI : " + hashMap.get("itemLabel"));
                System.out.println("URI : " + hashMap.get("itemDescription"));*/
        }
    } catch (EndpointException eex) {
        message = eex.toString();
        return null;
    } catch (Exception e) {
        message = "pas de connexion internet !!";
        return null;
    }
    return listeAlign;
}
Also used : NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) Endpoint(com.bordercloud.sparql.Endpoint) EndpointException(com.bordercloud.sparql.EndpointException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) MalformedURLException(java.net.MalformedURLException) EndpointException(com.bordercloud.sparql.EndpointException) SAXException(org.xml.sax.SAXException)

Example 2 with EndpointException

use of com.bordercloud.sparql.EndpointException in project opentheso by miledrousset.

the class WikidataHelper method queryWikidata.

/**
 * Alignement du thésaurus vers la source Wikidata en Sparql et en retour du Json
 * @param idC
 * @param idTheso
 * @param lexicalValue
 * @param lang
 * @param requete
 * @param source
 * @return
 */
public ArrayList<NodeAlignment> queryWikidata(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
    ArrayList<NodeAlignment> listAlignValues = new ArrayList<>();
    try {
        Endpoint sp = new Endpoint("https://query.wikidata.org/sparql", false);
        /*       String querySelect = "SELECT ?item ?itemLabel ?itemDescription WHERE {" +
                                    "  ?item rdfs:label \"fibula\"@en." +
                                    "  SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". }" +
                                    "}";*/
        requete = requete.replaceAll("##value##", lexicalValue);
        requete = requete.replaceAll("##lang##", lang);
        HashMap<String, HashMap> rs = sp.query(requete);
        if (rs == null)
            return null;
        ArrayList<HashMap<String, Object>> rows_queryWikidata = (ArrayList) rs.get("result").get("rows");
        for (HashMap<String, Object> hashMap : rows_queryWikidata) {
            NodeAlignment na = new NodeAlignment();
            na.setInternal_id_concept(idC);
            na.setInternal_id_thesaurus(idTheso);
            // label ou Nom
            if (hashMap.get("itemLabel") != null)
                na.setConcept_target(hashMap.get("itemLabel").toString());
            else
                continue;
            // description
            if (hashMap.get("itemDescription") != null)
                na.setDef_target(hashMap.get("itemDescription").toString());
            else
                na.setDef_target("");
            na.setThesaurus_target(source);
            // URI
            if (hashMap.get("item") != null)
                na.setUri_target(hashMap.get("item").toString());
            else
                continue;
            listAlignValues.add(na);
        }
    } catch (EndpointException eex) {
        messages.append(eex.toString());
        return null;
    } catch (Exception e) {
        messages.append("pas de connexion internet !!");
        return null;
    }
    return listAlignValues;
}
Also used : NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) Endpoint(com.bordercloud.sparql.Endpoint) EndpointException(com.bordercloud.sparql.EndpointException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) EndpointException(com.bordercloud.sparql.EndpointException)

Example 3 with EndpointException

use of com.bordercloud.sparql.EndpointException in project opentheso by miledrousset.

the class WikidataTest method searchValue.

// récupération des URI qui parlent du concept avec l'entité codé exp : http://www.wikidata.org/entity/Q324926 pour fibule
// SELECT ?item ?itemLabel WHERE {
// ?item rdfs:label "fibule"@fr.
// SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }.
// }
// @Test
public void searchValue() {
    try {
        Endpoint sp = new Endpoint("https://query.wikidata.org/sparql", false);
        String querySelect = "SELECT ?item ?itemLabel ?itemDescription WHERE {\n" + "  ?item rdfs:label \"fibula\"@fr.\n" + "  SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],fr\". }\n" + "}";
        HashMap rs = sp.query(querySelect);
        HashMap<String, HashMap> rs3_queryPopulationInFrance = sp.query(querySelect);
        ArrayList<HashMap<String, Object>> rows_queryPopulationInFrance = (ArrayList) rs3_queryPopulationInFrance.get("result").get("rows");
        for (HashMap<String, Object> hashMap : rows_queryPopulationInFrance) {
            System.out.println("URI : " + hashMap.get("item"));
            System.out.println("URI : " + hashMap.get("itemLabel"));
            System.out.println("URI : " + hashMap.get("itemDescription"));
        }
    // printResult(rs, 30);
    } catch (EndpointException eex) {
        System.out.println(eex);
        eex.printStackTrace();
    }
}
Also used : Endpoint(com.bordercloud.sparql.Endpoint) EndpointException(com.bordercloud.sparql.EndpointException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject)

Aggregations

Endpoint (com.bordercloud.sparql.Endpoint)3 EndpointException (com.bordercloud.sparql.EndpointException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JsonObject (javax.json.JsonObject)2 NodeAlignment (mom.trd.opentheso.bdd.helper.nodes.NodeAlignment)2 MalformedURLException (java.net.MalformedURLException)1 JSONObject (org.json.JSONObject)1 SAXException (org.xml.sax.SAXException)1