Skip to content
Snippets Groups Projects
Commit 017f1b77 authored by Sebastien's avatar Sebastien
Browse files

move unl packages to fr.tetras_libre.unltools.unl

+ code rearrangement
parent c4727b6a
No related branches found
No related tags found
No related merge requests found
Showing
with 140 additions and 126 deletions
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import fr.tetras_libre.unltools.unl.GraphExporter;
import fr.tetras_libre.unltools.unl.UnlDocument;
import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModel;
import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.ModelFactory;
import unl.GraphExporter;
import unl.UnlDocument;
import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.List; import java.util.List;
......
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import fr.tetras_libre.unltools.unl.*;
import fr.tetras_libre.unltools.unl.extensions.GraphExtensions; import fr.tetras_libre.unltools.unl.extensions.GraphExtensions;
import fr.tetras_libre.unltools.unl.extensions.SubGraphReferenceNodeExtensions; import fr.tetras_libre.unltools.unl.extensions.SubGraphReferenceNodeExtensions;
import org.apache.jena.ontology.Individual; import org.apache.jena.ontology.Individual;
...@@ -7,7 +8,6 @@ import org.apache.jena.ontology.OntModel; ...@@ -7,7 +8,6 @@ import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.Ontology; import org.apache.jena.ontology.Ontology;
import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.vocabulary.SKOS; import org.apache.jena.vocabulary.SKOS;
import unl.*;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -28,10 +28,8 @@ public class UnlDocumentToRdfConverter { ...@@ -28,10 +28,8 @@ public class UnlDocumentToRdfConverter {
public Collection<OntModel> convertToOntModel(Collection<UnlDocument> documents) { public Collection<OntModel> convertToOntModel(Collection<UnlDocument> documents) {
List<OntModel> result = new LinkedList<>(); List<OntModel> result = new LinkedList<>();
var index = 0;
for (var document : documents) { for (var document : documents) {
result.addAll(convertUnlDocumentToOntModel(document)); result.addAll(convertUnlDocumentToOntModel(document));
index++;
} }
return result; return result;
...@@ -71,8 +69,6 @@ public class UnlDocumentToRdfConverter { ...@@ -71,8 +69,6 @@ public class UnlDocumentToRdfConverter {
//documentNode.toString() : Le graphe UNL version chaîne de caractère //documentNode.toString() : Le graphe UNL version chaîne de caractère
sentenceOntModel.add(sentenceIndividual, SKOS.altLabel, documentNode.toString()); sentenceOntModel.add(sentenceIndividual, SKOS.altLabel, documentNode.toString());
// sentenceOntModel.add(sentenceIndividual, UNL.is_substructure_of, docIndividual);
// docModel.add(docIndividual, UNL.is_superstructure_of, sentenceIndividual);
this.setSuperstructureWithSubstructure(docModel, docIndividual, sentenceOntModel, sentenceIndividual); this.setSuperstructureWithSubstructure(docModel, docIndividual, sentenceOntModel, sentenceIndividual);
for (var sentenceNode : for (var sentenceNode :
sentencegraph.getNodes()) { sentencegraph.getNodes()) {
...@@ -81,15 +77,13 @@ public class UnlDocumentToRdfConverter { ...@@ -81,15 +77,13 @@ public class UnlDocumentToRdfConverter {
// Création de l'universalWord dans le dictionnaire // Création de l'universalWord dans le dictionnaire
var universalWordIndividual = sentenceOntModel.createIndividual(sentenceNameSpace + slugify(uw.getUniversalWord().toString()), UNL.UW_Lexeme); var universalWordIndividual = sentenceOntModel.createIndividual(sentenceNameSpace + slugify(uw.getUniversalWord().toString()), UNL.UW_Lexeme);
universalWordIndividual.addLabel(uw.getUniversalWord().toString(), "unl"); universalWordIndividual.addLabel(uw.getUniversalWord().toString(), "fr/tetras_libre/unltools/unl");
// Création de l'occurence du l'universalWord // Création de l'occurence du l'universalWord
var universalWordOccurrence = sentenceOntModel.createIndividual(UtilGraphNodeUri.constructGraphNodeUri(sentenceNameSpace, uw), var universalWordOccurrence = sentenceOntModel.createIndividual(UtilGraphNodeUri.constructGraphNodeUri(sentenceNameSpace, uw),
UNL.UW_Occurrence); UNL.UW_Occurrence);
universalWordOccurrence.addLabel(uw.getUniversalWord().toString(), "unl"); universalWordOccurrence.addLabel(uw.getUniversalWord().toString(), "fr/tetras_libre/unltools/unl");
this.setSuperstructureWithSubstructure(sentenceOntModel, sentenceIndividual, sentenceOntModel, universalWordOccurrence); this.setSuperstructureWithSubstructure(sentenceOntModel, sentenceIndividual, sentenceOntModel, universalWordOccurrence);
// sentenceOntModel.add(universalWordOccurrence, UNL.is_substructure_of, sentenceIndividual);
// sentenceOntModel.add(sentenceIndividual, UNL.is_superstructure_of, universalWordOccurrence);
sentenceOntModel.add(universalWordOccurrence, UNL.is_occurrence_of, universalWordIndividual); sentenceOntModel.add(universalWordOccurrence, UNL.is_occurrence_of, universalWordIndividual);
sentenceOntModel.add(universalWordIndividual, UNL.has_occurrence, universalWordOccurrence); sentenceOntModel.add(universalWordIndividual, UNL.has_occurrence, universalWordOccurrence);
...@@ -104,15 +98,25 @@ public class UnlDocumentToRdfConverter { ...@@ -104,15 +98,25 @@ public class UnlDocumentToRdfConverter {
UtilGraphNodeUri.constructGraphNodeUri(sentenceNameSpace, graphRefNode), UtilGraphNodeUri.constructGraphNodeUri(sentenceNameSpace, graphRefNode),
UNL.UNL_Scope); UNL.UNL_Scope);
scopeIndividual.addLabel(scopeName, "unl"); scopeIndividual.addLabel(scopeName, "fr/tetras_libre/unltools/unl");
// sentenceOntModel.add(scopeIndividual, UNL.is_substructure_of, sentenceIndividual);
// sentenceOntModel.add(sentenceIndividual, UNL.is_superstructure_of, scopeIndividual);
this.setSuperstructureWithSubstructure(sentenceOntModel, sentenceIndividual, sentenceOntModel, scopeIndividual); this.setSuperstructureWithSubstructure(sentenceOntModel, sentenceIndividual, sentenceOntModel, scopeIndividual);
/*
GraphExtensions.getRelationWithinScopeStream(scopeName, sentencegraph)
.forEach(relationWithinScope -> {
try {
var relationWithinScopeUri = RelationsUri.constructRelationUri(scopeName, relationWithinScope);
var relationWithinScopeObjectProperty = this.relationLabelToUnlPropertyConverter.getObjectProperty(relationWithinScope.getRelationLabel(), docModel);
sentenceOntModel.createIndividual(relationWithinScopeUri, relationWithinScopeObjectProperty);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
});
*/
var relationsWithinScope = GraphExtensions.getRelationsWithinScope(scopeName, sentencegraph); var relationsWithinScope = GraphExtensions.getRelationsWithinScope(scopeName, sentencegraph);
for (var relationWithinScope : relationsWithinScope) { for (var relationWithinScope : relationsWithinScope) {
try { try {
var relationWithinScopeUri = RelationsUri.constructRelationUri(scopeName, relationWithinScope, sentencegraph); var relationWithinScopeUri = RelationsUri.constructRelationUri(scopeName, relationWithinScope);
var relationWithinScopeObjectProperty = this.relationLabelToUnlPropertyConverter.getObjectProperty(relationWithinScope.getRelationLabel(), docModel); var relationWithinScopeObjectProperty = this.relationLabelToUnlPropertyConverter.getObjectProperty(relationWithinScope.getRelationLabel(), docModel);
sentenceOntModel.createIndividual(relationWithinScopeUri, relationWithinScopeObjectProperty); sentenceOntModel.createIndividual(relationWithinScopeUri, relationWithinScopeObjectProperty);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
...@@ -124,7 +128,7 @@ public class UnlDocumentToRdfConverter { ...@@ -124,7 +128,7 @@ public class UnlDocumentToRdfConverter {
} }
for (var relation : sentencegraph.getRelations()) { for (var relation : sentencegraph.getRelations()) {
var relationUri = RelationsUri.constructRelationUri(sentenceNameSpace, relation, sentencegraph); var relationUri = RelationsUri.constructRelationUri(sentenceNameSpace, relation);
try { try {
var relationLabel = relation.getRelationLabel(); var relationLabel = relation.getRelationLabel();
...@@ -163,31 +167,31 @@ public class UnlDocumentToRdfConverter { ...@@ -163,31 +167,31 @@ public class UnlDocumentToRdfConverter {
} }
private static class RelationsUri { private static class RelationsUri {
public static String constructRelationUri(String scopeName, GraphRelation graphRelation, Graph g) { public static String constructRelationUri(String scopeName, GraphRelation graphRelation) {
return String.format("%s%s_%s_%s", return String.format("%s%s_%s_%s",
scopeName, scopeName,
slugify(constructRelationUri(graphRelation.getNode1(), g)), slugify(constructRelationUri(graphRelation.getNode1())),
graphRelation.getRelationLabel(), graphRelation.getRelationLabel(),
slugify(constructRelationUri(graphRelation.getNode2(), g))); slugify(constructRelationUri(graphRelation.getNode2())));
} }
private static String constructRelationUri(GraphNode node, Graph g) { private static String constructRelationUri(GraphNode node) {
if (node instanceof UniversalWordNode) { if (node instanceof UniversalWordNode) {
return constructRelationUri((UniversalWordNode) node, g); return constructRelationUri((UniversalWordNode) node);
} }
if (node instanceof SubGraphReferenceNode) { if (node instanceof SubGraphReferenceNode) {
return constructRelationUri((SubGraphReferenceNode) node, g); return constructRelationUri((SubGraphReferenceNode) node);
} }
throw new IllegalArgumentException(String.format("Not implemented for type '%s'", node.getClass())); throw new IllegalArgumentException(String.format("Not implemented for type '%s'", node.getClass()));
} }
private static String constructRelationUri(UniversalWordNode node, Graph g) { private static String constructRelationUri(UniversalWordNode node) {
return node.getNodeId(); return node.getNodeId();
} }
private static String constructRelationUri(SubGraphReferenceNode node, Graph g) { private static String constructRelationUri(SubGraphReferenceNode node) {
return "scope_" + SubGraphReferenceNodeExtensions.getScopeNameFromSubGraphReferenceNode(node); return "scope_" + SubGraphReferenceNodeExtensions.getScopeNameFromSubGraphReferenceNode(node);
} }
} }
......
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import fr.tetras_libre.unltools.unl.GraphNode;
import fr.tetras_libre.unltools.unl.SubGraphReferenceNode;
import fr.tetras_libre.unltools.unl.UniversalWordNode;
import fr.tetras_libre.unltools.unl.extensions.SubGraphReferenceNodeExtensions; import fr.tetras_libre.unltools.unl.extensions.SubGraphReferenceNodeExtensions;
import unl.GraphNode;
import unl.SubGraphReferenceNode;
import unl.UniversalWordNode;
import static fr.tetras_libre.unltools.rdf.vocabulary.Slugify.slugify; import static fr.tetras_libre.unltools.rdf.vocabulary.Slugify.slugify;
......
package fr.tetras_libre.unltools.unl.extensions; package fr.tetras_libre.unltools.unl.extensions;
import unl.Graph; import fr.tetras_libre.unltools.unl.Graph;
import unl.GraphRelation; import fr.tetras_libre.unltools.unl.GraphRelation;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.stream.Stream;
public class GraphExtensions { public class GraphExtensions {
public static Collection<GraphRelation> getRelationsWithinScope(String scopeName, Graph g) { public static Collection<GraphRelation> getRelationsWithinScope(String scopeName, Graph g) {
...@@ -16,4 +17,12 @@ public class GraphExtensions { ...@@ -16,4 +17,12 @@ public class GraphExtensions {
} }
return relations; return relations;
} }
public static Stream<GraphRelation> getRelationWithinScopeStream(String scopeName, Graph g) {
return g.getRelations()
.stream()
.filter(relation -> null != relation.getSubGraphReferenceLabel()
//&& !relation.getSubGraphReferenceLabel().isBlank()
&& relation.getSubGraphReferenceLabel().equals(scopeName));
}
} }
package fr.tetras_libre.unltools.unl.extensions; package fr.tetras_libre.unltools.unl.extensions;
import unl.*; import fr.tetras_libre.unltools.unl.*;
public class GraphRelationExtensions { public class GraphRelationExtensions {
public static String getRealNode1Id(GraphRelation graphRelation, Graph g) throws NoEntryNodeException { public static String getRealNode1Id(GraphRelation graphRelation, Graph g) throws NoEntryNodeException {
...@@ -30,5 +30,4 @@ public class GraphRelationExtensions { ...@@ -30,5 +30,4 @@ public class GraphRelationExtensions {
private static String getRealNodeId(SubGraphReferenceNode graphNodeReference, Graph g) throws NoEntryNodeException { private static String getRealNodeId(SubGraphReferenceNode graphNodeReference, Graph g) throws NoEntryNodeException {
return SubGraphReferenceNodeExtensions.getRealNodeId(graphNodeReference, g); return SubGraphReferenceNodeExtensions.getRealNodeId(graphNodeReference, g);
} }
} }
package fr.tetras_libre.unltools.unl.extensions; package fr.tetras_libre.unltools.unl.extensions;
import unl.Graph; import fr.tetras_libre.unltools.unl.Graph;
import unl.NoEntryNodeException; import fr.tetras_libre.unltools.unl.NoEntryNodeException;
import unl.SubGraphReferenceNode; import fr.tetras_libre.unltools.unl.SubGraphReferenceNode;
public class SubGraphReferenceNodeExtensions { public class SubGraphReferenceNodeExtensions {
public static String getScopeNameFromSubGraphReferenceNode(SubGraphReferenceNode g) { public static String getScopeNameFromSubGraphReferenceNode(SubGraphReferenceNode g) {
......
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import unl.*; import fr.tetras_libre.unltools.unl.*;
import java.util.*; import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
public class CatUnlSentence { public class CatUnlSentence {
private static List<UnlDocument> unlDocuments; private static List<UnlDocument> unlDocuments;
......
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import fr.tetras_libre.unltools.unl.*;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsProvider;
import unl.*;
import java.util.*; import java.util.*;
import java.util.stream.Stream; import java.util.stream.Stream;
......
package fr.tetras_libre.unltools.rdf.vocabulary; package fr.tetras_libre.unltools.rdf.vocabulary;
import fr.tetras_libre.unltools.unl.UnlDocument;
import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.ModelFactory;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.provider.ArgumentsSource;
import unl.UnlDocument;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Vector; import java.util.Vector;
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Collection; import java.util.Collection;
import java.util.Vector; import java.util.Vector;
......
package unl; package fr.tetras_libre.unltools.unl;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
public abstract class GraphNode { public abstract class GraphNode {
private static final String entryAttribute = ".@entry"; private static final String entryAttribute = ".@entry";
private final Set attributes; private final Set<String> attributes;
private int nodeNumber; private int nodeNumber;
public GraphNode(Set attributes) { public GraphNode(Set<String> attributes) {
this.attributes = null == attributes ? new TreeSet() : attributes; this.attributes = null == attributes ? new TreeSet<>() : attributes;
} }
public int setNodeNumber(int nodeNumber) { public int setNodeNumber(int nodeNumber) {
...@@ -20,7 +20,7 @@ public abstract class GraphNode { ...@@ -20,7 +20,7 @@ public abstract class GraphNode {
return nodeNumber; return nodeNumber;
} }
public Set getAttributes() { public Set<String> getAttributes() {
return attributes; return attributes;
} }
...@@ -49,8 +49,7 @@ public abstract class GraphNode { ...@@ -49,8 +49,7 @@ public abstract class GraphNode {
return this.InternalCreateHashCodeBuilder(); return this.InternalCreateHashCodeBuilder();
} }
private EqualsBuilder CreateEqualityBuilder() private EqualsBuilder CreateEqualityBuilder() {
{
return this.InternalCreateEqualityBuilder(); return this.InternalCreateEqualityBuilder();
} }
......
package unl; package fr.tetras_libre.unltools.unl;
public class GraphRelation { public class GraphRelation {
private GraphNode node2;
private final String relationLabel; private final String relationLabel;
private final String SubGraphReferenceLabel; private final String SubGraphReferenceLabel;
private GraphNode node2;
private GraphNode node1; private GraphNode node1;
public GraphRelation(GraphNode node1, GraphNode node2, String relationLabel) { public GraphRelation(GraphNode node1, GraphNode node2, String relationLabel) {
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Vector; import java.util.Vector;
......
package unl; package fr.tetras_libre.unltools.unl;
public class NoEntryNodeException extends Exception { public class NoEntryNodeException extends Exception {
public NoEntryNodeException(String message) { public NoEntryNodeException(String message) {
......
package unl; package fr.tetras_libre.unltools.unl;
public class Restriction implements Comparable<Restriction> { public class Restriction implements Comparable<Restriction> {
private String label; private String label;
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
...@@ -7,10 +7,10 @@ public class SubGraphReferenceNode extends GraphNode { ...@@ -7,10 +7,10 @@ public class SubGraphReferenceNode extends GraphNode {
private final String referenceNumber; private final String referenceNumber;
public SubGraphReferenceNode(String referenceNumber) { public SubGraphReferenceNode(String referenceNumber) {
this(referenceNumber, new TreeSet()); this(referenceNumber, new TreeSet<>());
} }
public SubGraphReferenceNode(String referenceNumber, Set attributes) { public SubGraphReferenceNode(String referenceNumber, Set<String> attributes) {
super(attributes); super(attributes);
this.referenceNumber = referenceNumber; this.referenceNumber = referenceNumber;
} }
...@@ -36,7 +36,6 @@ public class SubGraphReferenceNode extends GraphNode { ...@@ -36,7 +36,6 @@ public class SubGraphReferenceNode extends GraphNode {
} }
@Override @Override
public String toString() { public String toString() {
return String.format("%s%s", this.referenceNumber, super.toString()); return String.format("%s%s", this.referenceNumber, super.toString());
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
...@@ -21,6 +21,25 @@ public class UniversalWord { ...@@ -21,6 +21,25 @@ public class UniversalWord {
this.restrictions = restrictions; this.restrictions = restrictions;
} }
/**
* Get the headword of string that represent universalWord
*
* @param universalWord the universalWord string to parse
* @return the headword of universalWord string
* @throws IllegalArgumentException the parameter is null or is whitespaces
*/
public static String getHeadword(String universalWord) {
if (null == universalWord || universalWord.trim().equals(""))
throw new IllegalArgumentException("universalWord should not be whitespaces or null");
int parenthesisPosition = universalWord.indexOf("(");
if (parenthesisPosition > 0) {
return universalWord.substring(0, parenthesisPosition);
}
return universalWord;
}
public String getHeadword() { public String getHeadword() {
return headword; return headword;
} }
...@@ -48,24 +67,6 @@ public class UniversalWord { ...@@ -48,24 +67,6 @@ public class UniversalWord {
return String.format("%s%s", headword, suffix); return String.format("%s%s", headword, suffix);
} }
/**
* Get the headword of string that represent universalWord
* @param universalWord the universalWord string to parse
* @return the headword of universalWord string
* @throws IllegalArgumentException the parameter is null or is whitespaces
*/
public static String getHeadword(String universalWord) {
if (null == universalWord || universalWord.trim().equals(""))
throw new IllegalArgumentException("universalWord should not be whitespaces or null");
int parenthesisPosition = universalWord.indexOf("(");
if (parenthesisPosition > 0) {
return universalWord.substring(0, parenthesisPosition);
}
return universalWord;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (null == obj) { if (null == obj) {
...@@ -91,6 +92,7 @@ public class UniversalWord { ...@@ -91,6 +92,7 @@ public class UniversalWord {
/** /**
* Test if th current universal word represent a number. * Test if th current universal word represent a number.
*
* @return true if the current universal word is a number otherwise false. * @return true if the current universal word is a number otherwise false.
*/ */
public boolean isNumber() { public boolean isNumber() {
......
package unl; package fr.tetras_libre.unltools.unl;
import java.util.Set; import java.util.Set;
...@@ -6,13 +6,13 @@ public class UniversalWordNode extends GraphNode { ...@@ -6,13 +6,13 @@ public class UniversalWordNode extends GraphNode {
private final UniversalWord universalWord; private final UniversalWord universalWord;
private final String instanceNumber; private final String instanceNumber;
public UniversalWordNode(UniversalWord universalWord, Set attributes, String instanceNumber) { public UniversalWordNode(UniversalWord universalWord, Set<String> attributes, String instanceNumber) {
super(attributes); super(attributes);
this.universalWord = universalWord; this.universalWord = universalWord;
this.instanceNumber = instanceNumber; this.instanceNumber = instanceNumber;
} }
public UniversalWordNode(UniversalWord universalWord, Set attributes) { public UniversalWordNode(UniversalWord universalWord, Set<String> attributes) {
this(universalWord, attributes, ""); this(universalWord, attributes, "");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment