Commit 4dc624e3 authored by Kerstin Bach's avatar Kerstin Bach

Merge branch 'deagledxd/mycbr-sdk-mycbr-3.0.1' into mycbr-3.0.1

Resolved Conflicts:
	src/test/junittest/similarity/StringFunctionTest.java
parents 87a41054 a73fbeec
......@@ -185,12 +185,16 @@ public class DateDesc extends SimpleAttDesc {
* the description for which a new function should be created
* @return the Similarity.getFunction for description desc
*/
public DateFct addDateFct(String name, boolean active) {
DateFct f = new DateFct(owner.getProject(), this, name);
public DateFct addDateFct(String name, boolean active, DateFct.DateFunctionPrecision adjust) {
DateFct f = new DateFct(owner.getProject(), this, name, adjust);
addFunction(f, active);
return f;
}
public DateFct addDateFct(String name, boolean active) {
return addDateFct(name, active, DateFct.DateFunctionPrecision.Year);
}
/*
* (non-Javadoc)
......@@ -265,7 +269,7 @@ public class DateDesc extends SimpleAttDesc {
void addDefaultFct() {
if (owner != null && owner != owner.getProject()) {
ISimFct activeSim = addDateFct(Project.DEFAULT_FCT_NAME, false);
ISimFct activeSim = addDateFct(Project.DEFAULT_FCT_NAME, false, DateFct.DateFunctionPrecision.Day);
updateAmalgamationFcts(owner, activeSim);
}
}
......
......@@ -28,12 +28,13 @@
package de.dfki.mycbr.core.similarity;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Observable;
import de.dfki.mycbr.core.Project;
import de.dfki.mycbr.core.casebase.Attribute;
import de.dfki.mycbr.core.casebase.MultipleAttribute;
import de.dfki.mycbr.core.casebase.SpecialAttribute;
import de.dfki.mycbr.core.casebase.*;
import de.dfki.mycbr.core.model.AttributeDesc;
import de.dfki.mycbr.core.model.DateDesc;
import de.dfki.mycbr.core.similarity.config.MultipleConfig;
......@@ -46,15 +47,19 @@ import de.dfki.mycbr.core.similarity.config.MultipleConfig;
*/
public class DateFct extends Observable implements ISimFct {
public enum DateFunctionPrecision { Second, Minute, Hour, Day, Month, Year }
private Project prj;
private String name;
private DateDesc desc;
protected MultipleConfig mc = MultipleConfig.DEFAULT_CONFIG;
public DateFct(Project prj,DateDesc desc, String name) {
private DateFunctionPrecision precision;
public DateFct(Project prj,DateDesc desc, String name, DateFunctionPrecision precision) {
this.prj = prj;
this.desc = desc;
this.name = name;
this.precision = precision;
}
/* (non-Javadoc)
......@@ -67,7 +72,74 @@ public class DateFct extends Observable implements ISimFct {
return prj.calculateSpecialSimilarity(value1, value2);
} else if (value1 instanceof MultipleAttribute<?> && value2 instanceof MultipleAttribute<?>) {
return prj.calculateMultipleAttributeSimilarity(this, (MultipleAttribute<?>)value1, (MultipleAttribute<?>)value2);
} else {
} else if (value1 instanceof DateAttribute && value2 instanceof DateAttribute) {
DateAttribute dateAttr1 = (DateAttribute) value1;
DateAttribute dateAttr2 = (DateAttribute) value2;
Date date1 = dateAttr1.getDate();
Date date2 = dateAttr2.getDate();
double result = 0.0;
Calendar calendar1 = GregorianCalendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = GregorianCalendar.getInstance();
calendar2.setTime(date2);
switch (this.precision) {
case Second:
if (calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)
&& calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
&& calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int secondsDate1 = calendar1.get(Calendar.SECOND);
int secondsDate2 = calendar2.get(Calendar.SECOND);
result = 1 - (Math.abs(secondsDate2 - secondsDate1) / 60.0);
}
break;
case Minute:
if (calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
&& calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int minutesDate1 = calendar1.get(Calendar.MINUTE);
int minutesDate2 = calendar2.get(Calendar.MINUTE);
result = 1 - (Math.abs(minutesDate2 - minutesDate1) / 60.0);
}
break;
case Hour:
if (calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int hoursDate1 = calendar1.get(Calendar.HOUR_OF_DAY);
int hoursDate2 = calendar2.get(Calendar.HOUR_OF_DAY);
result = 1 - (Math.abs(hoursDate2 - hoursDate1) / 24.0);
}
break;
case Day:
if (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int daysDate1 = calendar1.get(Calendar.DAY_OF_MONTH);
int daysDate2 = calendar2.get(Calendar.DAY_OF_MONTH);
result = 1 - (Math.abs(daysDate2 - daysDate1) / 31.0); // Muss bzgl. Schaltjahr angepasst werden
}
break;
case Month:
if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int monthsDate1 = calendar1.get(Calendar.MONTH);
int monthsDate2 = calendar2.get(Calendar.MONTH);
result = 1 - (Math.abs(monthsDate2 - monthsDate1) / 12.0);
}
break;
case Year:
int yearsDate1 = calendar1.get(Calendar.YEAR);
int yearsDate2 = calendar2.get(Calendar.YEAR);
result = 1 - (Math.abs(yearsDate2 - yearsDate1) / 100.0); // Konfigurierbar machen
break;
}
return Similarity.get(result);
} else {
return Similarity.INVALID_SIM;
}
......@@ -81,6 +153,12 @@ public class DateFct extends Observable implements ISimFct {
return desc;
}
/* (non-Javadoc)
*/
public DateFunctionPrecision getPrecision() {
return precision;
}
/* (non-Javadoc)
* @see de.dfki.mycbr.core.similarity.ISimFct#getMultipleConfig()
*/
......@@ -159,9 +237,12 @@ public class DateFct extends Observable implements ISimFct {
@Override
public void clone(AttributeDesc descNEW, boolean active) {
if (descNEW instanceof DateDesc && !name.equals(Project.DEFAULT_FCT_NAME)) {
DateFct f = ((DateDesc)descNEW).addDateFct(name, active);
DateFct f = ((DateDesc)descNEW).addDateFct(name, active, this.precision);
f.mc = this.mc;
}
}
public void setPrecision(DateFunctionPrecision adj) {
this.precision = adj;
}
}
......@@ -38,5 +38,5 @@ public enum StringConfig {
/**
*
*/
EQUALITY, NGRAM;
EQUALITY, NGRAM, LEVENSHTEIN;
}
......@@ -81,6 +81,7 @@ public class AllTests {
suite.addTestSuite(SymbolFunctionTest.class);
suite.addTestSuite(TaxonomyTest.class);
suite.addTestSuite(TrigramTest.class);
suite.addTestSuite(DateFunctionTest.class);
// load package
//suite.addTestSuite(Test1000.class);
......
This diff is collapsed.
......@@ -32,6 +32,7 @@ import junit.framework.TestCase;
import org.junit.Test;
import de.dfki.mycbr.core.similarity.DateFct;
import de.dfki.mycbr.core.similarity.StringFct;
import de.dfki.mycbr.core.similarity.config.StringConfig;
import test.junittest.TestFramework;
......@@ -41,7 +42,27 @@ import test.junittest.TestFramework;
*
*/
public class StringFunctionTest extends TestCase {
@Test
public void testBase() {
try {
TestFramework frame = new TestFramework();
StringFct f = frame.dealerDesc.addStringFct(StringConfig.LEVENSHTEIN, "f1", true);
// Base Tests
assertEquals(frame.prj, f.getProject());
assertEquals(true, f.isSymmetric());
assertEquals("f1", f.getName());
f.setName("f2");
assertEquals("f2", f.getName());
} catch (Exception exp) {
assertTrue("Excpetion in StringFctTest: testBase",false);
}
}
/**
* Test method for {@link de.dfki.mycbr.core.similarity.StringFct calculateSimilarity(Attribute, Attribute)}.
*/
......@@ -71,7 +92,7 @@ public class StringFunctionTest extends TestCase {
try {
TestFramework frame = new TestFramework();
StringFct f = frame.dealerDesc.addStringFct(StringConfig.NGRAM, "f1", true);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car23")).getRoundedValue() == 0.5);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.00);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24 Car24"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 0.5);
......@@ -80,7 +101,86 @@ public class StringFunctionTest extends TestCase {
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("tritrtr"), frame.dealerDesc.getStringAttribute("tritr")).getRoundedValue() == 0.6);
} catch (Exception exp) {
assertTrue("Excpetion in StringFctTest: testCalculateSimilarityEQUALITY",false);
assertTrue("Excpetion in StringFctTest: testCalculateSimilarityNGRAM",false);
}
}
/**
* Test method for {@link de.dfki.mycbr.core.similarity.StringFct calculateSimilarity(Attribute, Attribute)}.
*/
@Test
public void testCalculateSimilarityLEVENSHTEIN() {
try {
TestFramework frame = new TestFramework();
for (int i = 0; i < 2; i++) {
StringFct f = frame.dealerDesc.addStringFct(StringConfig.LEVENSHTEIN, "f1", true);
// Test all variations in casesens and caseinsens
f.setCaseSensitive(i == 0);
// Base functionality
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car23")).getRoundedValue() == 0.8);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car13")).getRoundedValue() == 0.6);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Bike")).getRoundedValue() == 0.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Bike24")).getRoundedValue() == 0.33);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute(""), frame.dealerDesc.getStringAttribute("Bike24")).getRoundedValue() == 0.0);
// UNICODE Test
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("ÆÇĐŸƃ®"), frame.dealerDesc.getStringAttribute("ÆÇĐŸƃ®")).getRoundedValue() == 1.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("ʤʐɰ"), frame.dealerDesc.getStringAttribute("ʤɰ")).getRoundedValue() == 0.67);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute(""), frame.dealerDesc.getStringAttribute("ʤɰ")).getRoundedValue() == 0.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("ʤʐɰ"), frame.dealerDesc.getStringAttribute("")).getRoundedValue() == 0.0);
// Test configuration feature
f.setLevenshteinAddCost(2);
f.setLevenshteinChangeCost(3);
f.setLevenshteinDelCost(2);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car23")).getRoundedValue() == 0.4);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute(""), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 0.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("")).getRoundedValue() == 0.0);
f.setLevenshteinChangeCost(6);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("Car23")).getRoundedValue() == 0.2);
f.setLevenshteinAddCost(0);
f.setLevenshteinChangeCost(0);
f.setLevenshteinDelCost(0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car25"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Bike123"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute(""), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 0.0);
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car24"), frame.dealerDesc.getStringAttribute("")).getRoundedValue() == 0.0);
// Only positive values allowed
try {
f.setLevenshteinChangeCost(-1);
assertTrue(false);
} catch (UnsupportedOperationException ex) {
assertTrue(true);
}
try {
f.setLevenshteinAddCost(-1);
assertTrue(false);
} catch (UnsupportedOperationException ex) {
assertTrue(true);
}
try {
f.setLevenshteinDelCost(-1);
assertTrue(false);
} catch (UnsupportedOperationException ex) {
assertTrue(true);
}
assertTrue(f.calculateSimilarity(frame.dealerDesc.getStringAttribute("Car25"), frame.dealerDesc.getStringAttribute("Car24")).getRoundedValue() == 1.0);
}
} catch (Exception exp) {
assertTrue("Excpetion in StringFctTest: testCalculateSimilarityLEVENSHTEIN",false);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment