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

📄 accountor.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.app.jacson.util;

import gnu.trove.TObjectIntHashMap;
import gnu.trove.TIntIntHashMap;
import gnu.trove.TIntIntIterator;

import de.spieleck.config.ConfigNode;
import de.spieleck.config.ConfigVerify.Acceptor;

import de.spieleck.app.jacson.JacsonRegistry;
import de.spieleck.app.jacson.JacsonReport;
import de.spieleck.app.jacson.JacsonConfigException;

import de.spieleck.util.Strings;

/**
 * Class to collect and report various statistical informations
 * about incoming chunks.
 * @author fsn
 */
public class Accountor
    implements Acceptor
{
    /** Report name for absolute limit */
    public final static String LIMIT_NODE = "limit";

    /** Report name for percentile limit */
    public final static String PERC_NODE = "percentlimit";

    /** Report name for numbered limit */
    public final static String NUMBER_NODE = "numberlimit";

    /** Report name for remain values value-name */
    public final static String NAMENAME_REMAIN = "remain";

    /** Report name for remaining sum */
    public final static String NAME_REMAIN = "remain";

    /** Report name for remaining count */
    public final static String NAME_RCOUNT = "remaincount";

    protected TObjectIntHashMap seen;

    /** lower limit to the output (saves significantly resources) */
    protected int limit;
    protected double percentLimit;

    /** Not yet used: can be achieved in XSL postprocessing */
    protected int numberLimit;

    /** keep track of the maximum and total counts */
    protected int max, total;

    public Accountor(ConfigNode config)
        throws JacsonConfigException
    {
        seen = new TObjectIntHashMap(100);
        limit = config.getInt(LIMIT_NODE, 1);
        percentLimit = config.getDouble(PERC_NODE, 1.0);
        numberLimit = config.getInt(NUMBER_NODE, 1000);
        max = 0;
        total = 0;
        ConfigUtil.verify(config, this);
    }

    public boolean accept(ConfigNode cn)
    {
        String name = cn.getName();
        return name.equals(LIMIT_NODE) 
            || name.equals(PERC_NODE) 
            || name.equals(NUMBER_NODE) 
            // XXX this is checked here and used where??
            || JacsonRegistry.acceptId(cn);
    }

    public void account(Object h)
    {
        total++;
        if ( !seen.increment(h) )
        {
            seen.put(h, 1);
        }
        else
        {
            int h2 = seen.get(h);
            if ( h2 > max ) 
                max = h2;
        }
    }

    public void summary(JacsonReport jr)
    {
        jr.begin("accounting");
        jr.report("limit", Integer.toString(limit));
        jr.report("percentlimit", Double.toString(percentLimit));
        jr.begin("overview");
        int seensize = seen.size();
        jr.report("size", Integer.toString(seensize));
        jr.report("total", Integer.toString(total));
        jr.report("max", max == 0 && total > 0 ? "1" : (Integer.toString(max)));
        jr.end();
        Object[] key = seen.keys();
        jr.begin("list");
        int realLimit = (int) (max * percentLimit / 100.0);
        if ( limit > realLimit )
            realLimit = limit;
        TIntIntHashMap distribution = new TIntIntHashMap();
        int remain = 0;
        int remainCount = 0;
        for(int i = 0; i < key.length; i++ )
        {
            Object h = key[i];
            int value = seen.get(h);
            if ( !distribution.increment(value) )
                distribution.put(value, 1);
            if ( value > realLimit )
            {
                jr.begin("count");
                if ( h instanceof Strings )
                {
                    Strings h2 = (Strings) h;
                    for(int j = 0; j < h2.getLength(); j++)
                        jr.report("name", h2.getString(j));
                }
                else
                {
                    jr.report("name", h.toString());
                }
                jr.report("value", Integer.toString(seen.get(key[i])));
                jr.end();
            }
            else
            {
                remain += value;
                remainCount++;
            }
        }
        reportCount(jr, NAMENAME_REMAIN, NAME_REMAIN, Integer.toString(remain));
        reportCount(jr, NAMENAME_REMAIN, NAME_RCOUNT, Integer.toString(remainCount));
        jr.end();
        jr.begin("distribution");
        jr.report("size", Integer.toString(distribution.size()));
        for(TIntIntIterator it = distribution.iterator();
            it.hasNext(); )
        {
            it.advance();
            int value = it.value();
            int keyval   = it.key();
            reportCount(jr, Integer.toString(keyval),Integer.toString(value));
        }
        jr.end();
        jr.end();
    }

    public static void reportCount(JacsonReport jr, String name, String value)
    {
        reportCount(jr, "name", name, value);
    }

    public static void reportCount(JacsonReport jr, String nameName, 
                                            String name, String value)
    {
        jr.begin("count");
        jr.report(nameName, name);
        jr.report("value", value);
        jr.end();
    }
        
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

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