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

edu.cmu.sv.utils.NBestDistribution Maven / Gradle / Ivy

Go to download

A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).

There is a newer version: 0.7.0
Show newest version
package edu.cmu.sv.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by David Cohen on 3/27/15.
 */
public class NBestDistribution {
    public Map internalDistribution;

    public NBestDistribution() {
        internalDistribution = new HashMap<>();
    }

    public void remove(T key){
        internalDistribution.remove(key);
    }

    public T getTopHypothesis(){
        T ans = null;
        Double maxProb = -1.0;
        for (T key : internalDistribution.keySet()){
            if (internalDistribution.get(key) > maxProb){
                maxProb = internalDistribution.get(key);
                ans = key;
            }
        }
        return ans;
    }

    public boolean containsKey(T key){
        return internalDistribution.containsKey(key);
    }

    public Double get(T key){
        if (internalDistribution.containsKey(key))
            return internalDistribution.get(key);
        return 0.0;
    }

    public void normalize(){
        Double total = internalDistribution.values().stream().reduce(0.0, (x, y) -> x + y);
        if (total!=1.0) {
            for (T key : internalDistribution.keySet()) {
                internalDistribution.put(key, internalDistribution.get(key) / total);
            }
        }
    }

    public double information(){
        double ans = 0.0;
        for (T key : internalDistribution.keySet()){
            if (internalDistribution.get(key)<=0.00001)
                continue;
            ans -= internalDistribution.get(key)*Math.log(internalDistribution.get(key))/Math.log(2.0);
        }
        return ans;
    }

    public Collection keySet() {return internalDistribution.keySet();}
    public Collection values(){
        return internalDistribution.values();
    }

    /*
    * If the key is already in the distribution, add value to it
    * Otherwise, create the key and set it to value
    * */
    public void put(T key, Double value){
        internalDistribution.put(key,value);
    }

    public Map getInternalDistribution() {
        return internalDistribution;
    }

    @Override
    public String toString() {
        String ans = "NBestDistribution{" +
                "internalDistribution:\n";
        for (T key : internalDistribution.keySet()){
            ans += internalDistribution.get(key)+ ", "+key+"\n";
        }
        return ans+'}';
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy