⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entryvaluecomparator.java

📁 Standord Classifier实现了一个基于Java的最大熵分类器。用于模式识别
💻 JAVA
字号:
package edu.stanford.nlp.util;import java.util.*;/** * Comparator for values of Map entries. Allows sorting in normal or reversed * order. Values must implement Comparable to be used. * <p> * Example use (sorts map of counts with highest first):<pre> * Map counts = ... // Object key -> Integer/Double count * List entries=new ArrayList(counts.entrySet()); * Collections.sort(entries,new EntryValueComparator(false)); * </pre> * * @author Joseph Smarr (jsmarr@stanford.edu) */public class EntryValueComparator implements Comparator{    private boolean ascending; // whether to sort in normal order or reversed    private Map m; // map from which to get the values for the keys to sort        /**      * Constructs a new EntryValueComparator using ascending (normal) order that     * works on Map.Entry objects.     */    public EntryValueComparator() { this(null,true); }        /**      * Constructs a new EntryValueComparator that will sort in the given order      * and works on Map.Entry objects.     */    public EntryValueComparator(boolean ascending) { this.ascending=ascending; }            /**     * Constructs a new EntryValueComparator that will sort keys for the given     * Map in ascending (normal) order. It will also sort Map.Entry objects.     */    public EntryValueComparator(Map m) { this(m,true); }        /**     * Constructs a new EmptyValueComparator to sort keys or entries of the given     * map in the given order.  If <tt>m</tt> is non-null, this Comparator can     * be used to sort its <tt>keySet()</tt> as well as its <tt>entrySet()</tt>.     * Otherwise it can only be used on the entries, since there's no way to get     * the value for a given key.     * @param m Map whose keys are to be sorted, or <tt>null</tt> if      * <tt>Map.Entry</tt> objects will be sorted.      * @param ascending whether to sort in ascending (normal) order or      * descending (reverse) order. Ascending order is alphabetical, descending      * order puts higher numbers first.     */    public EntryValueComparator(Map m,boolean ascending)    {        this.m=m;        this.ascending=ascending;    }        /**      * Compares the values of the two given Map.Entry objects in the given order.     * Returns 0 if either object is not a Map.Entry or if their values cannot     * be compared.     */    public int compare(Object o1,Object o2)    {        Object v1;        Object v2;                if((o1 instanceof Map.Entry) && (o2 instanceof Map.Entry))        {            // compare the values of these entries            v1=((Map.Entry)o1).getValue();            v2=((Map.Entry)o2).getValue();        }        else if(m!=null)        {            // treat objects as keys if we have the map and they're not entries            v1=o1;            v2=o2;        }        else return(0); // can't do anything in this case                return(ascending?compareOr0(v1,v2):compareOr0(v2,v1));    }        /**      * Returns <tt>source.compareTo(target)</tt> or 0 if source is null or not     * comparable.     */    private int compareOr0(Object source,Object target)    {        if(source==null || !(source instanceof Comparable)) return(0);        return(((Comparable)source).compareTo(target));    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -