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

ciir.umass.edu.learning.RankList Maven / Gradle / Ivy

There is a newer version: 2.10.1
Show newest version
/*===============================================================================
 * Copyright (c) 2010-2012 University of Massachusetts.  All Rights Reserved.
 *
 * Use of the RankLib package is subject to the terms of the software license set
 * forth in the LICENSE file included with this software, and also available at
 * http://people.cs.umass.edu/~vdang/ranklib_license.html
 *===============================================================================
 */

package ciir.umass.edu.learning;

import java.util.List;

import ciir.umass.edu.utilities.Sorter;

/**
 * @author vdang
 *
 * This class implement the list of objects (each of which is a DataPoint) to be ranked.
 */
public class RankList {

    protected DataPoint[] rl = null;

    protected int featureCount = 0;

    public RankList(final List rl) {
        this.rl = new DataPoint[rl.size()];
        for (int i = 0; i < rl.size(); i++) {
            this.rl[i] = rl.get(i);
        }
        init();
    }

    public RankList(final RankList rl) {
        this.rl = new DataPoint[rl.size()];
        for (int i = 0; i < rl.size(); i++) {
            this.rl[i] = rl.get(i);
        }
        init();
    }

    public RankList(final RankList rl, final int[] idx) {
        this.rl = new DataPoint[rl.size()];
        for (int i = 0; i < idx.length; i++) {
            this.rl[i] = rl.get(idx[i]);
        }
        init();
    }

    public RankList(final RankList rl, final int[] idx, final int offset) {
        this.rl = new DataPoint[rl.size()];
        for (int i = 0; i < idx.length; i++) {
            this.rl[i] = rl.get(idx[i] - offset);
        }
        init();
    }

    protected void init() {
        for (final DataPoint dp : this.rl) {
            final int c = dp.getFeatureCount();
            if (c > featureCount) {
                featureCount = c;
            }
        }
    }

    public String getID() {
        return get(0).getID();
    }

    public int size() {
        return rl.length;
    }

    public DataPoint get(final int k) {
        return rl[k];
    }

    public void set(final int k, final DataPoint p) {
        rl[k] = p;
    }

    public RankList getCorrectRanking() {
        final double[] score = new double[rl.length];
        for (int i = 0; i < rl.length; i++) {
            score[i] = rl[i].getLabel();
        }
        final int[] idx = Sorter.sort(score, false);
        return new RankList(this, idx);
    }

    public RankList getRanking(final short fid) {
        final double[] score = new double[rl.length];
        for (int i = 0; i < rl.length; i++) {
            score[i] = rl[i].getFeatureValue(fid);
        }
        final int[] idx = Sorter.sort(score, false);
        return new RankList(this, idx);
    }

    public int getFeatureCount() {
        return featureCount;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy