com.aliasi.test.unit.cluster.CompleteLinkClustererTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aliasi-lingpipe Show documentation
Show all versions of aliasi-lingpipe Show documentation
This is the original Lingpipe:
http://alias-i.com/lingpipe/web/download.html
There were not made any changes to the source code.
package com.aliasi.test.unit.cluster;
import com.aliasi.cluster.Dendrogram;
import com.aliasi.cluster.LeafDendrogram;
import com.aliasi.cluster.LinkDendrogram;
import com.aliasi.cluster.CompleteLinkClusterer;
import com.aliasi.util.Distance;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
public class CompleteLinkClustererTest {
@Test
public void testBoundaries() {
// cut and paste from single link
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(SingleLinkClustererTest.TEST_DISTANCE);
Set elts0 = new HashSet();
Set> clusters = clusterer.cluster(elts0);
assertEquals(0,clusters.size());
Set elts1 = new HashSet();
elts1.add("A");
Set> clustering = new HashSet>();
clustering.add(elts1);
assertEquals(clustering,clusterer.cluster(elts1));
Dendrogram dendro1 = clusterer.hierarchicalCluster(elts1);
assertTrue(dendro1 instanceof LeafDendrogram);
assertEquals(elts1,dendro1.memberSet());
assertEquals(0.0,dendro1.score(),0.001);
}
@Test(expected=IllegalArgumentException.class)
public void testCompleteLinkExc() {
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(SingleLinkClustererTest.TEST_DISTANCE);
Set elts0 = new HashSet();
clusterer.hierarchicalCluster(elts0);
}
@Test
public void testOne() {
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(SingleLinkClustererTest.TEST_DISTANCE);
Set elts = new HashSet();
elts.add("A");
elts.add("B");
elts.add("C");
elts.add("D");
elts.add("E");
Dendrogram dendro = clusterer.hierarchicalCluster(elts);
Set a = new HashSet();
a.add("A");
Set b = new HashSet();
b.add("B");
Set c = new HashSet();
c.add("C");
Set d = new HashSet();
d.add("D");
Set e = new HashSet();
e.add("E");
Set ab = new HashSet();
ab.addAll(a);
ab.addAll(b);
Set abc = new HashSet();
abc.addAll(ab);
abc.addAll(c);
Set de = new HashSet();
de.addAll(d);
de.addAll(e);
Set abcde = new HashSet();
abcde.addAll(abc);
abcde.addAll(de);
assertEquals(abcde,dendro.memberSet());
Set> p1 = new HashSet>();
p1.add(abcde);
assertEquals(p1,dendro.partitionK(1));
Set> p2 = new HashSet>();
p2.add(abc);
p2.add(de);
assertEquals(p2,dendro.partitionK(2));
Set> p3 = new HashSet>();
p3.add(abc);
p3.add(d);
p3.add(e);
assertEquals(p3,dendro.partitionK(3));
Set> p4 = new HashSet>();
p4.add(ab);
p4.add(c);
p4.add(d);
p4.add(e);
assertEquals(p4,dendro.partitionK(4));
Set> p5 = new HashSet>();
p5.add(a);
p5.add(b);
p5.add(c);
p5.add(d);
p5.add(e);
assertEquals(p5,dendro.partitionK(5));
assertEquals("ouch",9.0,dendro.score(),0.001);
}
@Test(expected=IllegalArgumentException.class)
public void testCompleteExc1() {
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(SingleLinkClustererTest.TEST_DISTANCE);
Set elts = new HashSet();
elts.add("A");
elts.add("B");
elts.add("C");
elts.add("D");
elts.add("E");
Dendrogram dendro = clusterer.hierarchicalCluster(elts);
dendro.partitionK(0);
}
@Test(expected=IllegalArgumentException.class)
public void testCompleteExc2() {
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(SingleLinkClustererTest.TEST_DISTANCE);
Set elts = new HashSet();
elts.add("A");
elts.add("B");
elts.add("C");
elts.add("D");
elts.add("E");
Dendrogram dendro = clusterer.hierarchicalCluster(elts);
dendro.partitionK(6);
}
@Test
public void testSix() {
Integer[] ints = new Integer[10];
for (int i = 0; i < ints.length; ++i)
ints[i] = Integer.valueOf(i);
SingleLinkClustererTest.FixedDistance dist
= new SingleLinkClustererTest.FixedDistance();
double[][] vals = new double[][]
{ { 13 },
{ 21, 9 },
{ 18, 19, 22 },
{ 4, 15, 20, 3 },
{ 8, 14, 12, 23, 5},
{ 7, 10, 11, 27, 24, 6 },
{ 28, 16, 17, 1, 2, 25, 26 } };
for (int i = 0; i < vals.length; ++i)
for (int j = 0; j < vals[i].length; ++j)
dist.setVal(ints[i+1],ints[j],vals[i][j]);
Set elts = new HashSet();
for (int i = 0; i < 8; ++i)
elts.add(ints[i]);
CompleteLinkClusterer clusterer = new CompleteLinkClusterer(dist);
Dendrogram dendrogram = clusterer.hierarchicalCluster(elts);
Set dtrs1 = new HashSet();
dtrs1.add(ints[0]);
dtrs1.add(ints[5]);
dtrs1.add(ints[6]);
dtrs1.add(ints[1]);
dtrs1.add(ints[2]);
Set dtrs2 = new HashSet();
dtrs2.add(ints[3]);
dtrs2.add(ints[7]);
dtrs2.add(ints[4]);
assertTrue(dendrogram instanceof LinkDendrogram);
LinkDendrogram linkDendro = (LinkDendrogram) dendrogram;
Dendrogram dendro1 = linkDendro.dendrogram1();
Dendrogram dendro2 = linkDendro.dendrogram2();
assertTrue(dendro1.memberSet().equals(dtrs1)
&& dendro2.memberSet().equals(dtrs2)
||
dendro2.memberSet().equals(dtrs1)
&& dendro1.memberSet().equals(dtrs2));
// just check top level
// expect: {{{0+{5+6}}+{1+2}}+{{3+7}+4}}
}
@Test
public void testPartition() {
for (double maxDistance = 0.05; maxDistance < 7.0; maxDistance += 0.5)
assertTestPartitionAt(maxDistance);
}
/**
* We lightly refactored this test contributed along
* with a bug report by Ben McCann. Used with permission.
*
* @author Ben McCann (benmccann.com)
*/
void assertTestPartitionAt(double maxDistance) {
Set input = new HashSet();
input.add(1.0);
input.add(2.0);
input.add(3.0);
input.add(4.0);
input.add(5.0);
input.add(5.1);
input.add(5.2);
input.add(5.3);
input.add(5.4);
Distance distance =
new Distance() {
// no override spec for interfaces
public double distance(Double a1, Double a2) {
return Math.abs(a1 - a2);
}
};
CompleteLinkClusterer clusterer
= new CompleteLinkClusterer(maxDistance, distance);
Set> clusters = clusterer.cluster(input);
int size = 0;
for (Set cluster : clusters)
size += cluster.size();
assertEquals(input.size(),size);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy