Skip to content

Commit

Permalink
Aligned the length implementation to the definition in the related work.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRoeder committed Sep 29, 2024
1 parent 3e6ef41 commit 2faae5b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@

public class LengthBasedRefinementScorer extends AbstractScoreCalculatorDecorator {

protected double propertyPenalty = 0.01;
protected double junctionPenalty = 0.005;
protected double lengthPenalty = 0.01;

public LengthBasedRefinementScorer(ScoreCalculator decorated) {
super(decorated);
}

public LengthBasedRefinementScorer(ScoreCalculator decorated, double propertyPenalty, double junctionPenalty) {
public LengthBasedRefinementScorer(ScoreCalculator decorated, double lengthPenalty) {
super(decorated);
this.propertyPenalty = propertyPenalty;
this.junctionPenalty = junctionPenalty;
this.lengthPenalty = lengthPenalty;
}

@Override
public double calculateRefinementScore(int posCount, int negCount, double classificationScore, ClassExpression ce) {
LengthDetectingVisitor visitor = new LengthDetectingVisitor();
ce.accept(visitor);
return classificationScore
- ((visitor.propertyCount * propertyPenalty) + (visitor.junctionMemberCount * junctionPenalty));
return classificationScore - (visitor.length * lengthPenalty);
}

public static class Factory implements ScoreCalculatorFactory {
Expand All @@ -43,77 +40,44 @@ public ScoreCalculator create(int numOfPositives, int numOfNegatives) {
}
}

/**
* Length as defined in "Learning Concept Lengths Accelerates Concept Learning
* in ALC"
*
* @author Michael Röder ([email protected])
*
*/
public static class LengthDetectingVisitor implements ClassExpressionVisitor {

protected int junctionCount = 0;
protected int junctionMemberCount = 0;
protected int propertyCount = 0;
protected int length = 0;

public LengthDetectingVisitor() {
super();
}

@Override
public void visitNamedClass(NamedClass node) {
if (node.isNegated()) {
length += 2;
} else {
length += 1;
}
}

@Override
public void visitJunction(Junction node) {
this.junctionCount++;
this.junctionMemberCount += node.getChildren().size();
length += 1;
for (ClassExpression child : node.getChildren()) {
child.accept(this);
}
}

@Override
public void visitSimpleQuantificationRole(SimpleQuantifiedRole node) {
this.propertyCount++;
length += 2;
node.getTailExpression().accept(this);
}

/**
* @return the junctionCount
*/
public int getJunctionCount() {
return junctionCount;
}

/**
* @param junctionCount the junctionCount to set
*/
public void setJunctionCount(int junctionCount) {
this.junctionCount = junctionCount;
}

/**
* @return the junctionMemberCount
*/
public int getJunctionMemberCount() {
return junctionMemberCount;
}

/**
* @param junctionMemberCount the junctionMemberCount to set
*/
public void setJunctionMemberCount(int junctionMemberCount) {
this.junctionMemberCount = junctionMemberCount;
}

/**
* @return the propertyCount
*/
public int getPropertyCount() {
return propertyCount;
}

/**
* @param propertyCount the propertyCount to set
*/
public void setPropertyCount(int propertyCount) {
this.propertyCount = propertyCount;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
@RunWith(Parameterized.class)
public class LengthBasedRefinementScorerTest {

public static final double PROPERTY_PENALTY = 0.01;
public static final double JUNCTION_PENALTY = 0.005;
public static final double LENGTH_PENALTY = 0.01;

@Parameters
public static List<Object[]> parameters() {
Expand All @@ -26,32 +25,32 @@ public static List<Object[]> parameters() {
final NamedClass A = new NamedClass("A");
final NamedClass B = new NamedClass("B");
// A
testCases.add(new Object[] { 10, 20, 5, 10, A, 0.5 });
testCases.add(new Object[] { 10, 20, 5, 10, A, 0.5 - LENGTH_PENALTY });
// A ⊓ B
testCases.add(new Object[] { 10, 20, 5, 10, new Junction(true, A, B), 0.5 - 2 * JUNCTION_PENALTY });
testCases.add(new Object[] { 10, 20, 5, 10, new Junction(false, A, B), 0.5 - 2 * JUNCTION_PENALTY });
testCases.add(new Object[] { 10, 20, 5, 10, new Junction(true, A, B), 0.5 - 3 * LENGTH_PENALTY });
testCases.add(new Object[] { 10, 20, 5, 10, new Junction(false, A, B), 0.5 - 3 * LENGTH_PENALTY });
// ∃r.A
testCases.add(
new Object[] { 10, 20, 5, 10, new SimpleQuantifiedRole(true, "r", false, A), 0.5 - PROPERTY_PENALTY });
testCases.add(new Object[] { 10, 20, 5, 10, new SimpleQuantifiedRole(true, "r", false, A),
0.5 - 3 * LENGTH_PENALTY });
// ∃r-.A
testCases.add(
new Object[] { 10, 20, 5, 10, new SimpleQuantifiedRole(true, "r", true, A), 0.5 - PROPERTY_PENALTY });
new Object[] { 10, 20, 5, 10, new SimpleQuantifiedRole(true, "r", true, A), 0.5 - 3 * LENGTH_PENALTY });
// ∃r.(A ⊓ B)
testCases.add(new Object[] { 10, 20, 5, 10, new SimpleQuantifiedRole(true, "r", true, new Junction(true, A, B)),
0.5 - PROPERTY_PENALTY - 2 * JUNCTION_PENALTY });
0.5 - 5 * LENGTH_PENALTY });
// ∃r.(A ⊓ B ⊓ ∃r.B)
testCases.add(new Object[] { 10, 20, 5, 10,
new SimpleQuantifiedRole(true, "r", true,
new Junction(true, A, B, new SimpleQuantifiedRole(true, "r", true, B))),
0.5 - 2 * PROPERTY_PENALTY - 3 * JUNCTION_PENALTY });
0.5 - 8 * LENGTH_PENALTY });
// (A ⊓ ∃r.B)
testCases.add(new Object[] { 10, 20, 5, 10, new Junction(true, A, new SimpleQuantifiedRole(true, "r", true, B)),
0.5 - PROPERTY_PENALTY - 2 * JUNCTION_PENALTY });
0.5 - 5 * LENGTH_PENALTY });
// ∃r1.∃r2.∃r3.A
testCases.add(new Object[] { 10, 20, 5, 10,
new SimpleQuantifiedRole(true, "r1", false,
new SimpleQuantifiedRole(true, "r2", false, new SimpleQuantifiedRole(true, "r3", false, A))),
0.5 - 3 * PROPERTY_PENALTY });
0.5 - 7 * LENGTH_PENALTY });

return testCases;
}
Expand Down Expand Up @@ -81,7 +80,7 @@ public void test() {
Assert.assertTrue("Faulty test case. There are more negative selected than negative examples exist.",
numNegSelected <= numNegExamples);
LengthBasedRefinementScorer calculator = new LengthBasedRefinementScorer(
new AccuracyCalculator(numPosExamples, numNegExamples), PROPERTY_PENALTY, JUNCTION_PENALTY);
new AccuracyCalculator(numPosExamples, numNegExamples), LENGTH_PENALTY);
Assert.assertEquals(expected,
calculator.calculateRefinementScore(numPosSelected, numNegSelected,
calculator.calculateClassificationScore(numPosSelected, numNegSelected), classExpression),
Expand Down

0 comments on commit 2faae5b

Please sign in to comment.