]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/sbcs/Checker.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / tools / misc / src / com / ibm / icu / dev / tool / charsetdet / sbcs / Checker.java
1 /*
2  ***********************************************************************
3  * Copyright (C) 2005-2006, International Business Machines            *
4  * Corporation and others. All Rights Reserved.                        *
5  ***********************************************************************
6  *
7  */
8
9 package com.ibm.icu.dev.tool.charsetdet.sbcs;
10
11 /**
12  * @author emader
13  *
14  * TODO To change the template for this generated type comment go to
15  * Window - Preferences - Java - Code Style - Code Templates
16  */
17 public class Checker implements NGramParser.NGramParserClient
18 {
19     private NGramList ngrams;
20     private int totalNGrams;
21     private int totalHits;
22     
23     private String language;
24     private String encoding;
25     
26     private int[] histogram;
27
28     private static final int BUFFER_SIZE = 1024;
29     
30     private char[] buffer;
31     private int bufIndex;
32     private int bufMax;
33
34     private NGramParser parser;
35
36     /**
37      * TODO This should take cumulative percent and the name...
38      */
39     public Checker(NGramList list, InputFile dataFile)
40     {
41         ngrams = list;
42         ngrams.setMapper(dataFile);
43         
44         language = languageName(dataFile.getFilename());
45         encoding = dataFile.getEncoding();
46         
47         buffer = new char[BUFFER_SIZE];
48         parser = new NGramParser(this);
49         resetCounts();
50         
51         histogram = new int[100];
52         resetHistogram();
53    }
54     
55     public void handleNGram(String key)
56     {
57         NGramList.NGram ngram = ngrams.get(key);
58         
59         totalNGrams += 1;
60         
61         if (ngram != null) {
62             totalHits += 1;
63             //ngram.incrementRefCount();
64         }
65     }
66     
67     private void resetCounts()
68     {
69         bufIndex = 0;
70         totalNGrams = totalHits = 0;
71     }
72     
73     private void resetHistogram()
74     {
75         for(int i = 0; i < 100; i += 1) {
76             histogram[i] = 0;
77         }
78         
79     }
80     
81     private static void exceptionError(Exception e)
82     {
83         System.err.println("ioError: " + e.toString());
84     }
85
86     private static String languageName(String filename)
87     {
88         return filename.substring(0, filename.indexOf('.'));
89     }
90     
91     private boolean nextBuffer(InputFile inputFile)
92     {
93         try {
94             bufMax = inputFile.read(buffer);
95         } catch (Exception e) {
96             bufMax = -1;
97             exceptionError(e);
98             
99             return false;
100         }
101         
102         bufIndex = 0;
103         
104         return bufMax >= 0;
105     }
106     
107     private void parseBuffer()
108     {
109         resetCounts();
110         parser.reset();
111         parser.parse();
112     }
113     
114     public char nextChar()
115     {
116         if (bufIndex >= bufMax) {
117             return 0;
118         }
119         
120         return buffer[bufIndex++];
121     }
122     
123     public String getLanguage()
124     {
125         return language;
126     }
127     
128     public void setMapper(InputFile file)
129     {
130         ngrams.setMapper(file);
131     }
132     
133     public int checkBuffer(char[] theBuffer, int charCount)
134     {
135         buffer = theBuffer;
136         bufMax = charCount;
137         
138         parseBuffer();
139         
140         return totalHits;
141     }
142     
143     public void check(InputFile dataFile)
144     {
145         int minHist = 101, maxHist = -1;
146         
147         dataFile.open();
148         
149         String dataFilename = dataFile.getFilename();
150         String fileEncoding = dataFile.getEncoding();
151         
152         System.out.println(language + "(" + encoding + ") stats, " + languageName(dataFilename) + "(" + fileEncoding + ") data:");
153         
154         setMapper(dataFile);
155         resetHistogram();
156
157         while (nextBuffer(dataFile)) {
158             parseBuffer();
159             
160             double percentHits = (double) totalHits / totalNGrams * 100.0;
161             int ph = (int) percentHits;
162             
163             if (ph < minHist) {
164                 minHist = ph;
165             }
166             
167             if (ph > maxHist) {
168                 maxHist = ph;
169             }
170             
171             histogram[ph] += 1;
172         }
173         
174         for(int ph = minHist; ph <= maxHist; ph += 1) {
175             System.out.println(ph + "\t" + histogram[ph]);
176         }
177         
178         System.out.println();
179         
180         dataFile.close();
181         
182         return;
183     }
184 }