Commit 3c0e7ccc authored by Marcel's avatar Marcel

Levenshtein StringFct

parent 0b758eed
......@@ -38,5 +38,5 @@ public enum StringConfig {
/**
*
*/
EQUALITY, NGRAM;
EQUALITY, NGRAM, LEVENSHTEIN;
}
......@@ -69,12 +69,91 @@ 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);
} 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