use of mom.trd.opentheso.bdd.helper.nodes.NodeAlignment in project opentheso by miledrousset.
the class ImportRdf4jHelper method finalizeAddConceptStruct.
private void finalizeAddConceptStruct(AddConceptsStruct acs) throws SQLException {
acs.termHelper.insertTerm(ds, acs.nodeTerm, idUser);
RelationsHelper relationsHelper = new RelationsHelper();
for (HierarchicalRelationship hierarchicalRelationship : acs.hierarchicalRelationships) {
if (!relationsHelper.insertHierarchicalRelation(ds, hierarchicalRelationship.getIdConcept1(), hierarchicalRelationship.getIdThesaurus(), hierarchicalRelationship.getRole(), hierarchicalRelationship.getIdConcept2())) {
// System.out.println("Erreur sur la relation = " + acs.concept.getIdConcept() + " ## " + hierarchicalRelationship.getRole());
message.append(System.getProperty("line.separator"));
message.append("Erreur sur la relation = ");
message.append(acs.concept.getIdConcept());
message.append(" ## ");
message.append(hierarchicalRelationship.getRole());
}
}
// For Term : definition; editorialNote; historyNote;
for (NodeNote nodeNoteList1 : acs.nodeNotes) {
if (nodeNoteList1.getNotetypecode().equals("customnote") || nodeNoteList1.getNotetypecode().equals("scopeNote") || nodeNoteList1.getNotetypecode().equals("historyNote") || nodeNoteList1.getNotetypecode().equals("note")) {
acs.noteHelper.addConceptNote(ds, acs.concept.getIdConcept(), nodeNoteList1.getLang(), idTheso, nodeNoteList1.getLexicalvalue(), nodeNoteList1.getNotetypecode(), idUser);
}
if (nodeNoteList1.getNotetypecode().equals("definition") || nodeNoteList1.getNotetypecode().equals("editorialNote")) {
acs.noteHelper.addTermNote(ds, acs.nodeTerm.getIdTerm(), nodeNoteList1.getLang(), idTheso, nodeNoteList1.getLexicalvalue(), nodeNoteList1.getNotetypecode(), idUser);
}
}
for (NodeAlignment nodeAlignment : acs.nodeAlignments) {
acs.alignmentHelper.addNewAlignment(ds, nodeAlignment);
}
for (NodeEM nodeEMList1 : acs.nodeEMList) {
acs.term.setId_concept(acs.concept.getIdConcept());
acs.term.setId_term(acs.nodeTerm.getIdTerm());
acs.term.setLexical_value(nodeEMList1.getLexical_value());
acs.term.setLang(nodeEMList1.getLang());
acs.term.setId_thesaurus(thesaurus.getId_thesaurus());
acs.term.setSource(nodeEMList1.getSource());
acs.term.setStatus(nodeEMList1.getStatus());
acs.termHelper.addNonPreferredTerm(ds, acs.term, idUser);
}
if (acs.nodeGps.getLatitude() != null && acs.nodeGps.getLongitude() != null) {
// insertion des données GPS
acs.gpsHelper.insertCoordonees(ds, acs.concept.getIdConcept(), thesaurus.getId_thesaurus(), acs.nodeGps.getLatitude(), acs.nodeGps.getLongitude());
}
for (String idTopConcept1 : idTopConcept) {
if (!acs.conceptHelper.setTopConcept(ds, idTopConcept1, thesaurus.getId_thesaurus())) {
// erreur;
}
}
// initialisation des variables
acs.concept = new Concept();
acs.nodeTerm = new NodeTerm();
acs.nodeTermTraductionList = new ArrayList<>();
acs.nodeEMList = new ArrayList<>();
acs.nodeNotes = new ArrayList<>();
acs.nodeAlignments = new ArrayList<>();
acs.hierarchicalRelationships = new ArrayList<>();
acs.idGrps = new ArrayList<>();
acs.isTopConcept = false;
acs.nodeGps = new NodeGps();
}
use of mom.trd.opentheso.bdd.helper.nodes.NodeAlignment in project opentheso by miledrousset.
the class AlignmentQuery method queryDBPedia.
/**
* Cette fonction permet de récupérer les alignements présents sur DBPedia
* pour un concept passé en paramètre
*
* @param idC
* @param idTheso
* @param lexicalValue
* @param lang
* @param source
* @return
*/
public ArrayList<NodeAlignment> queryDBPedia(String idC, String idTheso, String lexicalValue, String lang, String source) {
listeAlign = new ArrayList<>();
if (lexicalValue.contains(" ")) {
lexicalValue = lexicalValue.substring(0, lexicalValue.indexOf(" "));
}
lexicalValue = String.valueOf(lexicalValue.charAt(0)).toUpperCase() + lexicalValue.substring(1);
String sparqlQueryString1 = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" + "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT * WHERE {" + " ?s rdfs:label ?label ." + " ?s rdfs:comment ?comment ." + " ?s dbpedia-owl:thumbnail ?thumbnail ." + " ?s foaf:isPrimaryTopicOf ?primaryTopicOf ." + " FILTER(regex(?s,\"resource/" + lexicalValue + "*\"))" + " FILTER(lang(?label) = \"" + lang + "\")" + " FILTER(lang(?comment) = \"" + lang + "\")" + " }";
// System.out.println(sparqlQueryString1);
Query query = QueryFactory.create(sparqlQueryString1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.next();
NodeAlignment na = new NodeAlignment();
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
na.setConcept_target(qs.get("label").toString());
na.setDef_target(qs.get("comment").toString());
na.setThesaurus_target(source);
na.setUri_target(qs.get("primaryTopicOf").toString());
listeAlign.add(na);
}
qexec.close();
return listeAlign;
}
use of mom.trd.opentheso.bdd.helper.nodes.NodeAlignment in project opentheso by miledrousset.
the class AlignmentQuery method queryWikipedia.
// //////////////////////////////////////
// //////////////////////////////////////
// //// /////////////
// //// REST /////////////
// //// /////////////
// //////////////////////////////////////
// //////////////////////////////////////
/**
* Cette fonction permet de récupérer les alignements présents sur Wikipedia
* pour un concept passé en paramètre
*
* @param idC
* @param idTheso
* @param lexicalValue
* @param lang
* @param requete
* @param source
* @return
*/
public ArrayList<NodeAlignment> queryWikipedia(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
listeAlign = new ArrayList<>();
lexicalValue = lexicalValue.replaceAll(" ", "%20");
requete = requete.replace("##lang##", lang);
requete = requete.replace("##value##", lexicalValue);
try {
// URL url = new URL("https://" + lang + ".wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=xml&srsearch=" + lexicalValue + "&srnamespace=0");
URL url = new URL(requete);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
String xmlRecord = "";
while ((output = br.readLine()) != null) {
xmlRecord += output;
}
byte[] bytes = xmlRecord.getBytes();
xmlRecord = new String(bytes, Charset.forName("UTF-8"));
conn.disconnect();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecord));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("search");
// iterate the employees
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList p = element.getElementsByTagName("p");
if (p != null && p.getLength() > 0) {
for (int j = 0; j < p.getLength(); j++) {
String title = "", snippet = "";
Element el = (org.w3c.dom.Element) p.item(j);
if (el.hasAttribute("title")) {
title = el.getAttribute("title");
}
if (el.hasAttribute("snippet")) {
snippet = el.getAttribute("snippet") + "...";
}
NodeAlignment na = new NodeAlignment();
// si le titre est équivalent, on le place en premier
if (lexicalValue.trim().equalsIgnoreCase(title.trim())) {
na.setConcept_target(title);
na.setDef_target(snippet);
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
na.setThesaurus_target(source);
na.setUri_target("https://" + lang + ".wikipedia.org/wiki/" + title.replaceAll(" ", "_"));
listeAlign.add(0, na);
} else {
na.setConcept_target(title);
na.setDef_target(snippet);
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
na.setThesaurus_target(source);
na.setUri_target("https://" + lang + ".wikipedia.org/wiki/" + title.replaceAll(" ", "_"));
listeAlign.add(na);
}
}
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return listeAlign;
}
use of mom.trd.opentheso.bdd.helper.nodes.NodeAlignment in project opentheso by miledrousset.
the class AlignmentQuery method queryBNF.
// private getAlignementFromSkos(){
// }
// //////////////////////////////////////
// //////////////////////////////////////
// //// /////////////
// //// SPARQL /////////////
// //// /////////////
// //////////////////////////////////////
// //////////////////////////////////////
/**
* cette fonction permet de récuperer les alignements de la BNF
* requête de type Sparql
* @param idC
* @param idTheso
* @param lexicalValue
* @param lang
* @param requete
* @param source
* @return
*/
public ArrayList<NodeAlignment> queryBNF(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
listeAlign = new ArrayList<>();
// lexicalValue = lexicalValue.replaceAll(" ", "%20");
requete = requete.replace("##lang##", "\"" + lang + "\"");
if (lexicalValue.contains(" ")) {
String[] valueTemp = lexicalValue.split(" ");
boolean first = true;
for (String valuetemp : valueTemp) {
requete = requete.substring(0, requete.length() - 1);
if (first) {
requete = requete.replace("##value##", valuetemp.trim());
first = false;
} else {
requete = requete.concat(" && regex(?value, \"" + valuetemp.trim() + "\",\"i\")");
}
}
requete = requete + ")";
} else {
requete = requete.replace("##value##", "\"" + lexicalValue + "\"");
}
/*requete = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>" +
" PREFIX xml: <http://www.w3.org/XML/1998/namespace>" +
" SELECT ?instrument ?prop ?value" +
" where { <http://data.bnf.fr/ark:/12148/cb119367821> skos:narrower+ ?instrument. ?instrument ?prop ?value. FILTER( (regex(?prop,skos:prefLabel) || regex(?prop,skos:altLabel)) && regex(?value, \"cornemuse\",\"i\")) filter(lang(?value) =\"fr\")} LIMIT 10";
*/
// System.out.println(requete);
Query query = QueryFactory.create(requete);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://data.bnf.fr/sparql", query);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.next();
NodeAlignment na = new NodeAlignment();
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
na.setConcept_target(qs.get("value").toString());
// qs.get("comment").toString());
na.setDef_target("");
na.setThesaurus_target(source);
na.setUri_target(qs.get("instrument").toString());
listeAlign.add(na);
}
qexec.close();
return listeAlign;
}
use of mom.trd.opentheso.bdd.helper.nodes.NodeAlignment in project opentheso by miledrousset.
the class AlignmentQuery method queryOpentheso.
/**
* Cette fonction permet de récupérer les alignements présents sur Opentheso
* pour un concept passé en paramètre et un thésaurus donné
*
* @param idC
* @param idTheso
* @param lexicalValue
* @param lang
* @param requete
* @param source
* @return
*/
public ArrayList<NodeAlignment> queryOpentheso(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
if (requete.trim().equals("")) {
return null;
}
if (lexicalValue.trim().equals("")) {
return null;
}
listeAlign = new ArrayList<>();
// construction de la requête de type (webservices Opentheso)
lexicalValue = lexicalValue.replaceAll(" ", "%20");
requete = requete.replace("##lang##", lang);
requete = requete.replace("##value##", lexicalValue);
try {
URL url = new URL(requete);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
String xmlRecord = "";
while ((output = br.readLine()) != null) {
xmlRecord += output;
}
byte[] bytes = xmlRecord.getBytes();
xmlRecord = new String(bytes, Charset.forName("UTF-8"));
conn.disconnect();
StringBuffer sb = new StringBuffer(xmlRecord);
try {
SKOSXmlDocument sxd = new ReadFileSKOS().readStringBuffer(sb);
for (SKOSResource resource : sxd.getResourcesList()) {
NodeAlignment na = new NodeAlignment();
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
// "Pactols");
na.setThesaurus_target(source);
na.setUri_target(resource.getUri());
for (SKOSLabel label : resource.getLabelsList()) {
switch(label.getProperty()) {
case SKOSProperty.prefLabel:
if (label.getLanguage().equals(lang)) {
na.setConcept_target(label.getLabel());
}
break;
case SKOSProperty.altLabel:
if (label.getLanguage().equals(lang)) {
if (na.getConcept_target_alt().isEmpty()) {
na.setConcept_target_alt(label.getLabel());
} else {
na.setConcept_target_alt(na.getConcept_target_alt() + ";" + label.getLabel());
}
}
break;
default:
break;
}
}
for (SKOSDocumentation sd : resource.getDocumentationsList()) {
if (sd.getProperty() == SKOSProperty.definition && sd.getLanguage().equals(lang)) {
na.setDef_target(sd.getText());
}
}
listeAlign.add(na);
}
} catch (Exception ex) {
Logger.getLogger(AlignmentQuery.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return listeAlign;
}
Aggregations