All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliasi.test.unit.cluster.SingleLinkClustererTest Maven / Gradle / Ivy

Go to download

This is the original Lingpipe: http://alias-i.com/lingpipe/web/download.html There were not made any changes to the source code.

There is a newer version: 4.1.2-JL1.0
Show newest version
package com.aliasi.test.unit.cluster;

import com.aliasi.cluster.Dendrogram;
import com.aliasi.cluster.LeafDendrogram;
import com.aliasi.cluster.SingleLinkClusterer;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import com.aliasi.util.Distance;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SingleLinkClustererTest  {

    static class FixedDistance implements Distance {
        Map> mVals = new HashMap>();
        public double distance(E e1, E e2) {
            if (e1.equals(e2)) return 0.0;
            Map m1 = mVals.get(e1);
            if (m1 == null) return Double.POSITIVE_INFINITY;
            Double v = m1.get(e2);
            if (v == null) return Double.POSITIVE_INFINITY;
            return v.doubleValue();
        }
        public void setVal(E e1, E e2, double val) {
            set(e1,e2,val);
            set(e2,e1,val);
        }
        void set(E e1, E e2, double val) {
            Map m1 = mVals.get(e1);
            if (m1 == null) {
                m1 = new HashMap();
                mVals.put(e1,m1);
            }
            m1.put(e2,Double.valueOf(val));
        }
    }


    static class TestDistance extends FixedDistance {
        TestDistance() {
            setVal("A","B",1);
            setVal("A","C",2);
            setVal("A","D",7);
            setVal("A","E",5);
            setVal("B","C",3);
            setVal("B","D",8);
            setVal("B","E",6);
            setVal("C","D",5);
            setVal("C","E",9);
            setVal("D","E",4);
        }
    }

    static final Distance TEST_DISTANCE 
        = new TestDistance();
    
    @Test
    public void testBoundaries() {
        // cut and paste from complete link
        SingleLinkClusterer clusterer 
            = new SingleLinkClusterer(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 testBoundariesExc() {
        SingleLinkClusterer clusterer 
            = new SingleLinkClusterer(TEST_DISTANCE);
        Set elts0 = new HashSet();
        clusterer.hierarchicalCluster(elts0);
    }


    @Test
    public void testOne() {

        SingleLinkClusterer clusterer 
            = new SingleLinkClusterer(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(5.0,dendro.score(),0.001);
    }

    @Test(expected=IllegalArgumentException.class)
    public void testSlExc1() {
        SingleLinkClusterer clusterer 
            = new SingleLinkClusterer(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 testSlExc2() {
        SingleLinkClusterer clusterer 
            = new SingleLinkClusterer(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);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy