Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 479a8cfd authored by Jérôme Euzenat's avatar Jérôme Euzenat
Browse files

- Similarity can now been considered generically as a distance or similarity

parent 748f274b
No related branches found
No related tags found
No related merge requests found
......@@ -39,36 +39,26 @@ import fr.inrialpes.exmo.ontowrap.OntowrapException;
import fr.inrialpes.exmo.ontosim.util.HungarianAlgorithm;
/**
* The mother class for distance or similarity-based alignments.
* It is abstract because it does not provide an implemented similarity measure
* Otherwise everything is fine.
*
* This class should work with similarity and distances, as soon as, the used
* similarity structure is defined as such.
*
* @author Jrme Euzenat
* @version $Id$
*/
public class DistanceAlignment extends ObjectAlignment implements AlignmentProcess {
public abstract class DistanceAlignment extends ObjectAlignment implements AlignmentProcess {
Similarity sim;
/** Creation **/
public DistanceAlignment() {}
// JE: OntoRewr (LoadedOntology -> one step above)
//public DistanceAlignment( LoadedOntology onto1, LoadedOntology onto2 ){
// Init must now be triggered explicitely
// init( onto1, onto2 );
//};
public DistanceAlignment() {};
public void setSimilarity( Similarity s ) { sim = s; }
public Similarity getSimilarity() { return sim; }
public void addAlignDistanceCell( Object ob1, Object ob2, String relation, double measure) throws AlignmentException {
addAlignCell( ob1, ob2, relation, 1.-measure );
}
public double getAlignedDistance1( Object ob ) throws AlignmentException {
return (1. - getAlignedStrength1(ob));
};
public double getAlignedDistance2( Object ob ) throws AlignmentException {
return (1. - getAlignedStrength2(ob));
};
/**
* Process matching
* - create distance data structures,
......@@ -94,22 +84,26 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
* Prints the distance matrix
*/
public void printDistanceMatrix( Properties params ){
String algName = params.getProperty("algName");
String metric = "distance";
if ( sim.getSimilarity() ) metric = "similarity";
if ( algName == null ) algName = getClass().toString();
System.out.println("\\documentclass{article}\n");
System.out.println("\\usepackage{graphics}\n");
System.out.println("\\begin{document}\n");
System.out.println("\\begin{table}");
sim.printClassSimilarityMatrix("tex");
System.out.println("\\caption{Class distance with measure "+params.getProperty("stringFunction")+"}");
System.out.println("\\caption{Class "+metric+" with measure "+algName+".}" );
System.out.println("\\end{table}");
System.out.println();
System.out.println("\\begin{table}");
sim.printPropertySimilarityMatrix("tex");
System.out.println("\\caption{Property distance with measure "+params.getProperty("stringFunction")+"}");
System.out.println("\\caption{Property "+metric+" with measure "+algName+".}" );
System.out.println("\\end{table}");
System.out.println();
System.out.println("\\begin{table}");
sim.printIndividualSimilarityMatrix("tex");
System.out.println("\\caption{Individual distance with measure "+params.getProperty("stringFunction")+"}");
System.out.println("\\caption{Individual "+metric+" with measure "+algName+".}" );
System.out.println("\\end{table}");
System.out.println("\n\\end{document}");
}
......@@ -160,7 +154,7 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
public Alignment extractqs( double threshold, Properties params) {
double max = 0.;
boolean found = false;
double val = 0;
double val = 0.;
try {
// Extract for properties
......@@ -168,13 +162,14 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
ConcatenatedIterator(ontology1().getObjectProperties().iterator(),
ontology1().getDataProperties().iterator());
for( Object prop1 : pit1 ){
found = false; max = threshold; val = 0;
found = false; max = threshold; val = 0.;
Object prop2 = null;
ConcatenatedIterator pit2 = new
ConcatenatedIterator(ontology2().getObjectProperties().iterator(),
ontology2().getDataProperties().iterator());
for ( Object current : pit2 ){
val = 1 - sim.getPropertySimilarity(prop1,current);
if ( sim.getSimilarity() ) val = sim.getPropertySimilarity(prop1,current);
else val = 1 - sim.getPropertySimilarity(prop1,current);
if ( val > max) {
found = true; max = val; prop2 = current;
}
......@@ -186,7 +181,8 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
found = false; max = threshold; val = 0;
Object class2 = null;
for ( Object current : ontology2().getClasses() ) {
val = 1 - sim.getClassSimilarity(class1,current);
if ( sim.getSimilarity() ) val = sim.getClassSimilarity(class1,current);
else val = 1 - sim.getClassSimilarity(class1,current);
if (val > max) {
found = true; max = val; class2 = current;
}
......@@ -201,7 +197,8 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
Object ind2 = null;
for ( Object current : ontology2().getIndividuals() ) {
if ( ontology2().getEntityURI( current ) != null ) {
val = 1 - sim.getIndividualSimilarity( ind1, current );
if ( sim.getSimilarity() ) val = sim.getIndividualSimilarity( ind1, current );
else val = 1 - sim.getIndividualSimilarity( ind1, current );
if (val > max) {
found = true; max = val; ind2 = current;
}
......@@ -241,17 +238,23 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
for ( Object ob : ontology2().getClasses() ) {
class2[j++] = ob;
}
double ival = sim.getClassSimilarity(class1[0],class2[0]);
for( i = 0; i < nbclasses1; i++ ){
for( j = 0; j < nbclasses2; j++ ){
matrix[i][j] = 1 - sim.getClassSimilarity(class1[i],class2[j]);
if ( ival != -1. && ival != sim.getClassSimilarity(class1[i],class2[j] ) ) ival = -1.;
if ( sim.getSimilarity() ) matrix[i][j] = sim.getClassSimilarity(class1[i],class2[j]);
else matrix[i][j] = 1 - sim.getClassSimilarity(class1[i],class2[j]);
}
}
// Pass it to the algorithm
if ( ival == -1. ) {
int[][] result = HungarianAlgorithm.hgAlgorithm( matrix, "max" );
// Extract the result
for( i=0; i < result.length ; i++ ){
// The matrix has been destroyed
double val = 1 - sim.getClassSimilarity(class1[result[i][0]],class2[result[i][1]]);
double val;
if ( sim.getSimilarity() ) val = sim.getClassSimilarity(class1[result[i][0]],class2[result[i][1]]);
else val = 1 - sim.getClassSimilarity(class1[result[i][0]],class2[result[i][1]]);
// JE: here using strict-> is a very good idea.
// it means that alignments with 0. similarity
// will be excluded from the best match.
......@@ -259,6 +262,7 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
addCell( new ObjectCell( (String)null, class1[result[i][0]], class2[result[i][1]], BasicRelation.createRelation("="), val ) );
}
}
}
} catch (AlignmentException alex) { alex.printStackTrace(); }
catch (OntowrapException owex) { owex.printStackTrace(); }
// For properties
......@@ -278,17 +282,24 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
ConcatenatedIterator(ontology2().getObjectProperties().iterator(),
ontology2().getDataProperties().iterator());
for ( Object ob: pit2 ) prop2[j++] = ob;
double ival = sim.getPropertySimilarity(prop1[0],prop2[0]);
for( i = 0; i < nbprop1; i++ ){
for( j = 0; j < nbprop2; j++ ){
if ( ival != -1. && ival != sim.getPropertySimilarity(prop1[i],prop2[j] ) ) ival = -1.;
if ( sim.getSimilarity() ) matrix[i][j] = sim.getPropertySimilarity(prop1[i],prop2[j]);
else
matrix[i][j] = 1 - sim.getPropertySimilarity(prop1[i],prop2[j]);
}
}
// Pass it to the algorithm
if ( ival == -1. ) {
int[][] result = HungarianAlgorithm.hgAlgorithm( matrix, "max" );
// Extract the result
for( i=0; i < result.length ; i++ ){
// The matrix has been destroyed
double val = 1 - sim.getPropertySimilarity(prop1[result[i][0]],prop2[result[i][1]]);
double val;
if ( sim.getSimilarity() ) val = sim.getPropertySimilarity(prop1[result[i][0]],prop2[result[i][1]]);
else val = 1 - sim.getPropertySimilarity(prop1[result[i][0]],prop2[result[i][1]]);
// JE: here using strict-> is a very good idea.
// it means that alignments with 0. similarity
// will be excluded from the best match.
......@@ -296,6 +307,7 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
addCell( new ObjectCell( (String)null, prop1[result[i][0]], prop2[result[i][1]], BasicRelation.createRelation("="), val ) );
}
}
}
} catch (AlignmentException alex) { alex.printStackTrace(); }
catch (OntowrapException owex) { owex.printStackTrace(); }
// For individuals
......@@ -320,17 +332,24 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
}
double[][] matrix = new double[nbind1][nbind2];
int i, j;
double ival = sim.getIndividualSimilarity(ind1[0],ind2[0]);
for( i=0; i < nbind1; i++ ){
for( j=0; j < nbind2; j++ ){
if ( ival != -1. && ival != sim.getIndividualSimilarity(ind1[i],ind2[j] ) ) ival = -1.;
if ( sim.getSimilarity() ) matrix[i][j] = sim.getIndividualSimilarity(ind1[i],ind2[j]);
else
matrix[i][j] = 1 - sim.getIndividualSimilarity(ind1[i],ind2[j]);
}
}
// Pass it to the algorithm
if ( ival == -1. ) {
int[][] result = HungarianAlgorithm.hgAlgorithm( matrix, "max" );
// Extract the result
for( i=0; i < result.length ; i++ ){
// The matrix has been destroyed
double val = 1 - sim.getIndividualSimilarity(ind1[result[i][0]],ind2[result[i][1]]);
double val;
if ( sim.getSimilarity() ) val = sim.getIndividualSimilarity(ind1[result[i][0]],ind2[result[i][1]]);
else val = 1 - sim.getIndividualSimilarity(ind1[result[i][0]],ind2[result[i][1]]);
// JE: here using strict-> is a very good idea.
// it means that alignments with 0. similarity
// will be excluded from the best match.
......@@ -338,6 +357,7 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
addCell( new ObjectCell( (String)null, ind1[result[i][0]], ind2[result[i][1]], BasicRelation.createRelation("="), val ) );
}
}
}
} catch (AlignmentException alex) { alex.printStackTrace(); //}
} catch (OntowrapException owex) { owex.printStackTrace(); }
}
......@@ -399,7 +419,8 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
// for classes
for ( Object ent1: ontology1().getClasses() ) {
for ( Object ent2: ontology2().getClasses() ) {
val = 1 - sim.getClassSimilarity( ent1, ent2 );
if ( sim.getSimilarity() ) val = sim.getClassSimilarity( ent1, ent2 );
else val = 1 - sim.getClassSimilarity( ent1, ent2 );
if ( val > threshold ){
cellSet.add( new ObjectCell( (String)null, ent1, ent2, BasicRelation.createRelation("="), val ) );
}
......@@ -414,7 +435,8 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
ConcatenatedIterator(ontology2().getObjectProperties().iterator(),
ontology2().getDataProperties().iterator());
for ( Object ent2: pit2 ) {
val = 1 - sim.getPropertySimilarity( ent1, ent2 );
if ( sim.getSimilarity() ) val = sim.getPropertySimilarity( ent1, ent2 );
else val = 1 - sim.getPropertySimilarity( ent1, ent2 );
if ( val > threshold ){
cellSet.add( new ObjectCell( (String)null, ent1, ent2, BasicRelation.createRelation("="), val ) );
}
......@@ -427,7 +449,8 @@ public class DistanceAlignment extends ObjectAlignment implements AlignmentProce
for( Object ent2: ontology2().getIndividuals() ) {
if ( ontology2().getEntityURI( ent2 ) != null ) {
val = 1 - sim.getIndividualSimilarity( ent1, ent2 );
if ( sim.getSimilarity() ) val = sim.getIndividualSimilarity( ent1, ent2 );
else val = 1 - sim.getIndividualSimilarity( ent1, ent2 );
if ( val > threshold ){
cellSet.add( new ObjectCell( (String)null, ent1, ent2, BasicRelation.createRelation("="), val ) );
}
......
......@@ -48,6 +48,8 @@ import fr.inrialpes.exmo.align.impl.ObjectAlignment;
public abstract class MatrixMeasure implements Similarity {
public boolean similarity = true;
//Momentaneously public
public LoadedOntology onto1 = null;
public LoadedOntology onto2 = null;
......@@ -84,26 +86,31 @@ public abstract class MatrixMeasure implements Similarity {
} else {
throw new AlignmentException("");
};
// Beware, I consider that confidence is similarity
for ( Cell c : oalign ){
Object o1 = c.getObject1();
if ( onto1.isClass( o1 ) ) {
Integer i1 = classlist1.get( o1 );
Integer i2 = classlist2.get( c.getObject2() );
if ( i1 != null && i2 != null ) {
clmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
if ( similarity )
clmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
else clmatrix[i1.intValue()][i2.intValue()] = 1.-c.getStrength();
}
} else if ( onto1.isProperty( o1 ) ) {
Integer i1 = proplist1.get( o1 );
Integer i2 = proplist2.get( c.getObject2() );
if ( i1 != null && i2 != null ) {
prmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
if ( similarity )
prmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
else prmatrix[i1.intValue()][i2.intValue()] = 1.-c.getStrength();
}
} else {
Integer i1 = indlist1.get( o1 );
Integer i2 = indlist2.get( c.getObject2() );
if ( i1 != null && i2 != null ) {
indmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
if ( similarity )
indmatrix[i1.intValue()][i2.intValue()] = c.getStrength();
else indmatrix[i1.intValue()][i2.intValue()] = 1.-c.getStrength();
}
}
}
......@@ -238,6 +245,9 @@ public abstract class MatrixMeasure implements Similarity {
System.out.println("\n\\end{tabular}");
}
public boolean getSimilarity() {
return similarity;
}
public void printClassSimilarityMatrix( String type ){
printMatrix( nbclass1, classlist1, classlist2, clmatrix );
}
......
......@@ -37,29 +37,41 @@ public interface Similarity
{
// These parameters contains usually:
// ontology1 and ontology2
// It would be better if they where explicit...
// Apparently the initialize also compute the similarity
/**
* Is it a similarity or a distance?
*/
public boolean getSimilarity();
// JE: OntoRewr: This should not be in init
/**
* Initialize the similarity value with various useful structures
*/
public void initialize( LoadedOntology<Object> onto1, LoadedOntology<Object> onto2 );
public void initialize( LoadedOntology<Object> onto1, LoadedOntology<Object> onto2, Alignment align );
/**
* actually computes the similarity and store it in the adequate structures
*/
public void compute( Properties p );
/**
* Accessors to the stored similarity values
*/
public double getClassSimilarity( Object c1, Object c2 );
public double getPropertySimilarity( Object p1, Object p2);
public double getIndividualSimilarity( Object i1, Object i2 );
/**
* Printers of the obtained similarity values
*/
public void printClassSimilarityMatrix( String type );
public void printPropertySimilarityMatrix( String type );
public void printIndividualSimilarityMatrix( String type );
// New implementation
// JE: this is better as a new implementation.
// however, currently the implementation does not follow it:
// the abstract matrix class provides the get- accessors and the
// concrete classes implement measure as their computation function.
// This is not clean. What should be done is:
// JE: OntoRewr: to be suppressed
public double measure( Object c1, Object c2 ) throws Exception;
// JE2010: These are used by the generic MatrixMeasure implementation
// in which it is sufficient to implement them to solve everything
// However, for more flexibility, it possible to just skip these
// and implement the measure within compute(p) (see InstanceBasedMatrixMeasure).
public double classMeasure( Object c1, Object c2 ) throws Exception;
public double propertyMeasure( Object p1, Object p2) throws Exception;
public double individualMeasure( Object i1, Object i2 ) throws Exception;
......
......@@ -78,6 +78,7 @@ public class ClassStructAlignment extends DistanceAlignment implements Alignment
}
/** Processing **/
// Could better use similarity
public void align( Alignment alignment, Properties params ) throws AlignmentException {
loadInit( alignment );
honto1 = (HeavyLoadedOntology<Object>)getOntologyObject1();
......
......@@ -76,6 +76,7 @@ public class NameAndPropertyAlignment extends DistanceAlignment implements Align
}
/** Processing **/
// Could better use similarity
public void align( Alignment alignment, Properties params ) throws AlignmentException {
loadInit( alignment );
honto1 = (HeavyLoadedOntology<Object>)getOntologyObject1();
......@@ -179,7 +180,7 @@ public class NameAndPropertyAlignment extends DistanceAlignment implements Align
max = propmatrix[i][j];
}
}
if ( found && max < 0.5) { addAlignDistanceCell( proplist1.get(i), proplist2.get(best), "=", max ); }
if ( found && max < 0.5) { addAlignCell( proplist1.get(i), proplist2.get(best), "=", 1.-max ); }
}
if (debug > 0) System.err.print("Computing class distances\n");
// Compute classes distances
......@@ -242,7 +243,7 @@ public class NameAndPropertyAlignment extends DistanceAlignment implements Align
max = classmatrix[i][j];
}
}
if ( found && max < 0.5) { addAlignDistanceCell( classlist1.get(i), classlist2.get(best), "=", max ); }
if ( found && max < 0.5) { addAlignCell( classlist1.get(i), classlist2.get(best), "=", 1.-max ); }
}
}
......
......@@ -49,29 +49,34 @@ public class StringDistAlignment extends DistanceAlignment implements AlignmentP
Method dissimilarity = null;
String methodName = "equalDistance";
protected class StringDistMatrixMeasure extends MatrixMeasure {
public StringDistMatrixMeasure() {
similarity = false; // This is a distance matrix
}
public double measure( Object o1, Object o2 ) throws Exception {
String s1 = ontology1().getEntityName( o1 );
String s2 = ontology2().getEntityName( o2 );
// Unnamed entity = max distance
if ( s1 == null || s2 == null ) return 1.;
Object[] params = { s1.toLowerCase(), s2.toLowerCase() };
if ( debug > 4 )
System.err.println( "OB:"+s1+" ++ "+s2+" ==> "+dissimilarity.invoke( null, params ));
return ((Double)dissimilarity.invoke( null, params )).doubleValue();
}
public double classMeasure( Object cl1, Object cl2 ) throws Exception {
return measure( cl1, cl2 );
}
public double propertyMeasure( Object pr1, Object pr2 ) throws Exception{
return measure( pr1, pr2 );
}
public double individualMeasure( Object id1, Object id2 ) throws Exception{
return measure( id1, id2 );
}
}
/** Creation **/
public StringDistAlignment() {
setSimilarity( new MatrixMeasure() {
public double measure( Object o1, Object o2 ) throws Exception {
String s1 = ontology1().getEntityName( o1 );
String s2 = ontology2().getEntityName( o2 );
// Unnamed entity = max distance
if ( s1 == null || s2 == null ) return 1.;
Object[] params = { s1.toLowerCase(), s2.toLowerCase() };
if ( debug > 4 )
System.err.println( "OB:"+s1+" ++ "+s2+" ==> "+dissimilarity.invoke( null, params ));
return ((Double)dissimilarity.invoke( null, params )).doubleValue();
}
public double classMeasure( Object cl1, Object cl2 ) throws Exception {
return measure( cl1, cl2 );
}
public double propertyMeasure( Object pr1, Object pr2 ) throws Exception{
return measure( pr1, pr2 );
}
public double individualMeasure( Object id1, Object id2 ) throws Exception{
return measure( id1, id2 );
}
} );
setSimilarity( new StringDistMatrixMeasure() );
setType("**");
}
......@@ -90,7 +95,7 @@ public class StringDistAlignment extends DistanceAlignment implements AlignmentP
throw new AlignmentException( "Unknown method for StringDistAlignment : "+params.getProperty("stringFunction"), e );
}
// JE2010: Strange: why does it is not equivalent to call
// JE2010: Strange: why is it not equivalent to call
// super.align( alignment, params )
// Load initial alignment
loadInit( alignment );
......@@ -102,6 +107,7 @@ public class StringDistAlignment extends DistanceAlignment implements AlignmentP
getSimilarity().compute( params );
// Print matrix if asked
params.setProperty( "algName", getClass()+"/"+methodName );
if ( params.getProperty("printMatrix") != null ) printDistanceMatrix( params );
// Extract alignment
......
......@@ -77,6 +77,7 @@ public class StrucSubsDistAlignment extends DistanceAlignment implements Alignme
}
/** Processing **/
// Could better use similarity
public void align( Alignment alignment, Properties params ) throws AlignmentException {
loadInit( alignment );
honto1 = (HeavyLoadedOntology<Object>)getOntologyObject1();
......@@ -182,7 +183,7 @@ public class StrucSubsDistAlignment extends DistanceAlignment implements Alignme
max = propmatrix[i][j];
}
}
if ( found ) { addAlignDistanceCell( proplist1.get(i), proplist2.get(best), "=", max ); }
if ( found ) { addAlignCell( proplist1.get(i), proplist2.get(best), "=", 1.-max ); }
}
if (debug > 0) System.err.print("Computing class distances\n");
......@@ -254,7 +255,7 @@ public class StrucSubsDistAlignment extends DistanceAlignment implements Alignme
max = classmatrix[i][j];
}
}
if ( found ) { addAlignDistanceCell( classlist1.get(i), classlist2.get(best), "=", max ); }
if ( found ) { addAlignCell( classlist1.get(i), classlist2.get(best), "=", 1.-max ); }
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment