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

📄 filters.java

📁 Standord Classifier实现了一个基于Java的最大熵分类器。用于模式识别
💻 JAVA
字号:
package edu.stanford.nlp.util;import java.lang.reflect.Array; // for runtime type preservationimport java.util.*;import edu.stanford.nlp.process.Appliable;/** * Filters contains some simple implementations of the Filter interface. *  * @author Christopher Manning * @version 1.0 */public class Filters {    /** Nothing to instantiate */    private Filters() {}    /** The acceptFilter accepts everything. */    public static Filter acceptFilter() {        return new CategoricalFilter(true);    }    /** The rejectFilter accepts nothing. */    public static Filter rejectFilter() {        return new CategoricalFilter(false);    }    private static final class CategoricalFilter implements Filter {	        private final boolean judgment;        private CategoricalFilter(boolean judgment) {            this.judgment = judgment;        }        /** Checks if the given object passes the filter.         *  @param obj an object to test         */        public boolean accept(Object obj) {            return judgment;        }    }    /** The collectionAcceptFilter accepts a certain collection. */    public static Filter collectionAcceptFilter(Object[] objs) {        return new CollectionAcceptFilter(Arrays.asList(objs), true);    }    /** The collectionAcceptFilter accepts a certain collection. */    public static Filter collectionAcceptFilter(Collection objs) {        return new CollectionAcceptFilter(objs, true);    }    /** The collectionRejectFilter rejects a certain collection. */    public static Filter collectionRejectFilter(Object[] objs) {        return new CollectionAcceptFilter(Arrays.asList(objs), false);    }    /** The collectionRejectFilter rejects a certain collection. */    public static Filter collectionRejectFilter(Collection objs) {        return new CollectionAcceptFilter(objs, false);    }    private static final class CollectionAcceptFilter implements Filter {	        private final Collection args;        private final boolean judgment;        private CollectionAcceptFilter(Collection c, boolean judgment) {            this.args = new HashSet(c);            this.judgment = judgment;        }        /** Checks if the given object passes the filter.         *  @param obj an object to test         */        public boolean accept(Object obj) {            if (args.contains(obj)) {            	return judgment;            } else {               	return ! judgment;            }        }    }        /** Filter that accepts only when both filters accept (AND). */    public static Filter andFilter(Filter f1,Filter f2)    {        return(new CombinedFilter(f1,f2,true));    }        /** Filter that accepts when either filter accepts (OR). */    public static Filter orFilter(Filter f1,Filter f2)    {        return(new CombinedFilter(f1,f2,false));    }    /** Conjunction or disjunction of two filters. */    private static class CombinedFilter implements Filter    {        private Filter f1,f2;        private boolean conjunction; // and vs. or                public CombinedFilter(Filter f1,Filter f2,boolean conjunction)        {            this.f1=f1;            this.f2=f2;            this.conjunction=conjunction;        }                public boolean accept(Object o)        {            if(conjunction) return(f1.accept(o)&&f2.accept(o));            return(f1.accept(o)||f2.accept(o));        }    }        /** Filter that does the opposite of given filter (NOT). */    public static Filter notFilter(Filter filter)    {        return(new NegatedFilter(filter));    }    /** Filter that's either negated or normal as specified. */    public static Filter switchedFilter(Filter filter,boolean negated)    {        return(new NegatedFilter(filter,negated));    }        /** Negation of a filter. */    private static class NegatedFilter implements Filter    {        private Filter filter;        private boolean negated;                public NegatedFilter(Filter filter,boolean negated)        {            this.filter=filter;            this.negated=negated;        }        public NegatedFilter(Filter filter) { this(filter,true); }                public boolean accept(Object o)        {            return(negated^filter.accept(o)); // xor        }    }        /**     * Filter that first transforms input before filtering it. An object is      * accepted iff the given filter accepts its output from the given Appliable.     * This is particularly useful when filtering a list of objects, since for     * example the Appliable might return a single field or method result from     * the objects being filtered, and thus the Object could be filtered based     * on some part of it without having to write a special filter.     */    public static Filter transformedFilter(Filter filter,Appliable appliable)    {        return(new TransformedFilter(filter,appliable));    }        /** Filters on output of Appliable, returns matching input. */    private static class TransformedFilter implements Filter    {        private Filter filter;        private Appliable appliable;                public TransformedFilter(Filter filter,Appliable appliable)        {            this.filter=filter;            this.appliable=appliable;        }                /** Returns o iff filter accepts appliable's transformation of o. */        public boolean accept(Object o)        {            return(filter.accept(appliable.apply(o)));        }    }        /**     * Applies the given filter to each of the given elems, and returns the list     * of elems that were accepted. The runtime type of the returned array is     * the same as the passed in array.     */    public static Object[] filter(Object[] elems,Filter filter)    {        List filtered=new ArrayList();        for(int i=0;i<elems.length;i++)            if(filter.accept(elems[i]))                filtered.add(elems[i]);                return(filtered.toArray((Object[])Array.newInstance(elems.getClass().getComponentType(),filtered.size())));    }        /** Removes all elems in the given Collection that aren't accepted by the given Filter. */    public static void retainAll(Collection elems,Filter filter)    {        for(Iterator iter=elems.iterator();iter.hasNext();)        {            Object elem=iter.next();            if(!filter.accept(elem))                iter.remove();        }    }}

⌨️ 快捷键说明

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