Search in sources :

Example 61 with Instance

use of org.dkpro.tc.api.features.Instance in project dkpro-tc by dkpro.

the class LiblinearDataWriterTest method dataWriterTest.

@Test
public void dataWriterTest() throws Exception {
    List<Instance> fs = new ArrayList<Instance>();
    List<Feature> features1 = new ArrayList<>();
    features1.add(new Feature("feature1", 1.0, FeatureType.NUMERIC));
    features1.add(new Feature("feature2", 0.0, FeatureType.NUMERIC));
    List<Feature> features2 = new ArrayList<>();
    features2.add(new Feature("feature2", 0.5, FeatureType.NUMERIC));
    features2.add(new Feature("feature1", 0.5, FeatureType.NUMERIC));
    Instance instance1 = new Instance(features1, "0");
    Instance instance2 = new Instance(features2, "1");
    fs.add(instance1);
    fs.add(instance2);
    File outputDirectory = folder.newFolder();
    StringBuilder sb = new StringBuilder();
    sb.append("feature1\n");
    sb.append("feature2\n");
    FileUtils.writeStringToFile(new File(outputDirectory, Constants.FILENAME_FEATURES), sb.toString(), "utf-8");
    File outputFile = new File(outputDirectory, Constants.FILENAME_DATA_IN_CLASSIFIER_FORMAT);
    LibsvmDataFormatWriter writer = new LibsvmDataFormatWriter();
    writer.init(outputDirectory, false, Constants.LM_SINGLE_LABEL, false, new String[] { "0", "1" });
    writer.writeClassifierFormat(fs);
    Problem problem = Problem.readFromFile(outputFile, 1.0);
    assertEquals(2, problem.l);
    assertEquals(4, problem.n);
    assertEquals(0.0, problem.y[0], 0.00001);
    assertEquals(1.0, problem.y[1], 0.00001);
}
Also used : LibsvmDataFormatWriter(org.dkpro.tc.io.libsvm.LibsvmDataFormatWriter) Instance(org.dkpro.tc.api.features.Instance) ArrayList(java.util.ArrayList) Problem(de.bwaldvogel.liblinear.Problem) Feature(org.dkpro.tc.api.features.Feature) File(java.io.File) Test(org.junit.Test)

Aggregations

Instance (org.dkpro.tc.api.features.Instance)61 ArrayList (java.util.ArrayList)38 Feature (org.dkpro.tc.api.features.Feature)30 Test (org.junit.Test)27 File (java.io.File)17 AnalysisEngineProcessException (org.apache.uima.analysis_engine.AnalysisEngineProcessException)14 IOException (java.io.IOException)12 Gson (com.google.gson.Gson)8 TextClassificationException (org.dkpro.tc.api.exception.TextClassificationException)8 Attribute (weka.core.Attribute)8 DenseInstance (weka.core.DenseInstance)8 Instances (weka.core.Instances)8 SparseInstance (weka.core.SparseInstance)8 FeatureExtractorResource_ImplBase (org.dkpro.tc.api.features.FeatureExtractorResource_ImplBase)6 BufferedReader (java.io.BufferedReader)5 FileInputStream (java.io.FileInputStream)5 InputStreamReader (java.io.InputStreamReader)5 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)5 CollectionReaderDescription (org.apache.uima.collection.CollectionReaderDescription)5 ExternalResourceDescription (org.apache.uima.resource.ExternalResourceDescription)5