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

com.aliasi.test.unit.features.KnownFeatureExtractorTest 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.features;

import org.junit.Test;

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

import com.aliasi.features.KnownFeatureExtractor;

import com.aliasi.util.AbstractExternalizable;
import com.aliasi.util.FeatureExtractor;
import com.aliasi.util.ObjectToDoubleMap;

import java.io.IOException;
import java.io.NotSerializableException;

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

public class KnownFeatureExtractorTest {

    @Test
    public void testBasic() {
        ObjectToDoubleMap feats1 = new ObjectToDoubleMap();
        feats1.put("A",10.0);
        feats1.put("B",2.0);
        feats1.put("C",-50.0);

        MockFeatureExtractor mfe = new MockFeatureExtractor();
        mfe.put(1,feats1);

        Set knownFeatureSet = new HashSet(Arrays.asList(new String[] { "A", "C" }));
        KnownFeatureExtractor knownFe = new KnownFeatureExtractor(mfe,knownFeatureSet);
        assertEquals(knownFeatureSet,knownFe.knownFeatureSet());
        Map feats = knownFe.features(1);
        assertEquals(10.0,feats.get("A"));
        assertNull(feats.get("B"));
        assertEquals(-50.0,feats.get("C"));
    }

    @Test
    public void testSerialize() throws IOException, ClassNotFoundException {
        ObjectToDoubleMap feats1 = new ObjectToDoubleMap();
        feats1.put("A",10.0);
        feats1.put("B",2.0);
        feats1.put("C",-50.0);

        SerializableMockFeatureExtractor mfe = new SerializableMockFeatureExtractor();
        mfe.put(1,feats1);

        Set knownFeatureSet = new HashSet(Arrays.asList(new String[] { "A", "C" }));
        KnownFeatureExtractor knownFe 
            = new KnownFeatureExtractor(mfe,knownFeatureSet);

        FeatureExtractor knownFe2
            = (FeatureExtractor) AbstractExternalizable.serializeDeserialize(knownFe);

        Map feats = knownFe2.features(1);
        assertEquals(10.0,feats.get("A"));
        assertNull(feats.get("B"));
        assertEquals(-50.0,feats.get("C"));
    }

    @Test(expected = NotSerializableException.class)
    public void testUnSerialize() throws IOException, ClassNotFoundException {
        MockFeatureExtractor mfe = new MockFeatureExtractor();
        Set knownSet = new HashSet();
        KnownFeatureExtractor knownFe = new KnownFeatureExtractor(mfe,knownSet);
        AbstractExternalizable.serializeDeserialize(knownFe);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy