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

org.itc.irst.tcc.sre.util.PercentileBootstrapProcedure Maven / Gradle / Ivy

/*
 * PercentileBootstrapProcedure.java 1.0 01/06/2004
 *
 * Copyright 2002-2004 by  ITC-irst
 * via Sommarive 18 - Povo, 38050 Trento (I)
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of ITC-irst. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ITC-irst.
 */
package org.itc.irst.tcc.sre.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TO DO
 * 
 * @author Claudio Giuliano
 * @version %I%, %G%
 * @since 1.0
 */
public class PercentileBootstrapProcedure {
    /**
     * Define a static logger variable so that it references the Logger instance
     * named PercentileBootstrapProcedure.
     */
    static Logger logger = LoggerFactory
            .getLogger(PercentileBootstrapProcedure.class.getName());

    //
    private int iterations;

    //
    private double confidence;

    //
    private DecimalFormat formatter = new DecimalFormat("00.0");

    //
    private DecimalFormat formatter1 = new DecimalFormat("#0.0");

    /**
     * Creates a PercentileBootstrapProcedure object.
     */
    public PercentileBootstrapProcedure(File goldFile, File preferredFile,
            int n, double c) throws IOException, IndexOutOfBoundsException {
        logger.debug("PercentileBootstrapProcedure.PercentileBootstrapProcedure: ");
        logger.debug("gold:" + goldFile);
        logger.debug("preferred:" + preferredFile);
        confidence = c;
        iterations = n;

        List gold = new Output1(goldFile).getList();
        List preferred = new Output1(preferredFile, goldFile).getList();

        logger.debug("gold.size:" + gold.size());
        logger.debug("preferred.size:" + preferred.size());

        Evaluator[] preferredEval = evalAll(gold, preferred);

        double ps = calculateScore(preferredEval);
        logger.info("original score, size : " + formatter.format(ps * 100)
                + ", " + preferredEval.length);

        // logger.debug("preferred.length:" + preferredEval.length);

        double[] s = test(preferredEval);

        // for (int i=0;iOutput1.
     */
    static Logger logger = LoggerFactory.getLogger(Output1.class.getName());

    //
    private SortedMap map;

    //
    Output1(File ans) throws IOException {
        map = new TreeMap();
        read(ans);
    } // end constructor

    // pref gold
    Output1(File ans, File ref) throws IOException {
        map = new TreeMap();
        read(ans, ref);
    } // end constructor

    //
    private void read(File ans) throws IOException {
        LineNumberReader lr = new LineNumberReader(new FileReader(ans));
        String line = null;
        while ((line = lr.readLine()) != null) {
            String[] s = line.split("\t");
            String id = s[1].substring(0, s[1].indexOf('-'));
            put(new Integer(id), new Double(s[0]));
        } // end while

        lr.close();
    } // end read

    //
    private void read(File ans, File ref) throws IOException {
        LineNumberReader ar = new LineNumberReader(new FileReader(ans));
        LineNumberReader rr = new LineNumberReader(new FileReader(ref));

        String a = null, r = null;
        while (((a = ar.readLine()) != null) && ((r = rr.readLine()) != null)) {
            // logger.debug("a: " + a);
            // logger.debug("r: " + r);
            String[] s = r.split("\t");
            // logger.debug("s[1]: " + s[1]);
            String id = s[1].substring(0, s[1].indexOf('-'));
            // logger.debug("id: " + id);
            put(new Integer(id), new Double(a.trim()));
        } // end while

        ar.close();
        rr.close();
    } // end read

    //
    private void put(Integer sentID, Double pred) {
        List list = (List) map.get(sentID);
        if (list == null) {
            list = new ArrayList();
        }

        list.add(pred);
        map.put(sentID, list);
    } // end put

    //
    public List getList() {
        List result = new ArrayList();
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            Integer id = (Integer) it.next();
            List list = (List) map.get(id);
            // logger.debug(id + " : " + list.size());
            result.add(list);
        }

        return result;
    } // end getList

} // end class Output1

class Quicksort {
    public static final Random RND = new Random();

    private void swap(double[] array, int i, int j) {
        double tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    private int partition(double[] array, int begin, int end) {
        int index = begin + RND.nextInt(end - begin + 1);
        double pivot = array[index];
        swap(array, index, end);
        for (int i = index = begin; i < end; ++i) {
            // if (cmp.compare(array[i], pivot) <= 0) {
            if (array[i] <= pivot) {
                swap(array, index++, i);
            }
        }
        swap(array, index, end);
        return (index);
    }

    private void qsort(double[] array, int begin, int end) {
        if (end > begin) {
            int index = partition(array, begin, end);
            qsort(array, begin, index - 1);
            qsort(array, index + 1, end);
        }
    }

    public void sort(double[] array) {
        qsort(array, 0, array.length - 1);
    }
} // end class Quicksort




© 2015 - 2025 Weber Informatics LLC | Privacy Policy