]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/CharsetRecog_2022.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / CharsetRecog_2022.java
1 /*\r
2 *******************************************************************************\r
3 * Copyright (C) 2005 - 2008, International Business Machines Corporation and  *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 package com.ibm.icu.text;\r
8 \r
9 /**\r
10  *  class CharsetRecog_2022  part of the ICU charset detection imlementation.\r
11  *                           This is a superclass for the individual detectors for\r
12  *                           each of the detectable members of the ISO 2022 family\r
13  *                           of encodings.\r
14  * \r
15  *                           The separate classes are nested within this class.\r
16  * \r
17  * @internal\r
18  */\r
19 abstract class CharsetRecog_2022 extends CharsetRecognizer {\r
20 \r
21     \r
22     /**\r
23      * Matching function shared among the 2022 detectors JP, CN and KR\r
24      * Counts up the number of legal an unrecognized escape sequences in\r
25      * the sample of text, and computes a score based on the total number &\r
26      * the proportion that fit the encoding.\r
27      * \r
28      * \r
29      * @param text the byte buffer containing text to analyse\r
30      * @param textLen  the size of the text in the byte.\r
31      * @param escapeSequences the byte escape sequences to test for.\r
32      * @return match quality, in the range of 0-100.\r
33      */\r
34     int   match(byte [] text, int textLen, byte [][] escapeSequences) {\r
35         int     i, j;\r
36         int     escN;\r
37         int     hits   = 0;\r
38         int     misses = 0;\r
39         int     shifts = 0;\r
40         int     quality;\r
41         scanInput:\r
42             for (i=0; i<textLen; i++) {\r
43                 if (text[i] == 0x1b) {\r
44                     checkEscapes:\r
45                         for (escN=0; escN<escapeSequences.length; escN++) {\r
46                             byte [] seq = escapeSequences[escN];\r
47                             \r
48                             if ((textLen - i) < seq.length) {\r
49                                 continue checkEscapes;\r
50                             }\r
51                             \r
52                             for (j=1; j<seq.length; j++) {\r
53                                 if (seq[j] != text[i+j])  {\r
54                                     continue checkEscapes;\r
55                                 }                                   \r
56                             }\r
57                             \r
58                             hits++; \r
59                             i += seq.length-1;\r
60                             continue scanInput;\r
61                         }\r
62                 \r
63                         misses++;                  \r
64                 }\r
65                 \r
66                 if (text[i] == 0x0e || text[i] == 0x0f) {\r
67                     // Shift in/out\r
68                     shifts++;\r
69                 }\r
70             }\r
71         \r
72         if (hits == 0) {\r
73             return 0;\r
74         }\r
75         \r
76         //\r
77         // Initial quality is based on relative proportion of recongized vs.\r
78         //   unrecognized escape sequences. \r
79         //   All good:  quality = 100;\r
80         //   half or less good: quality = 0;\r
81         //   linear inbetween.\r
82         quality = (100*hits - 100*misses) / (hits + misses);\r
83         \r
84         // Back off quality if there were too few escape sequences seen.\r
85         //   Include shifts in this computation, so that KR does not get penalized\r
86         //   for having only a single Escape sequence, but many shifts.\r
87         if (hits+shifts < 5) {\r
88             quality -= (5-(hits+shifts))*10;\r
89         }\r
90         \r
91         if (quality < 0) {\r
92             quality = 0;\r
93         }        \r
94         return quality;\r
95     }\r
96 \r
97     \r
98  \r
99     \r
100     static class CharsetRecog_2022JP extends CharsetRecog_2022 {\r
101         private byte [] [] escapeSequences = {\r
102                 {0x1b, 0x24, 0x28, 0x43},   // KS X 1001:1992\r
103                 {0x1b, 0x24, 0x28, 0x44},   // JIS X 212-1990\r
104                 {0x1b, 0x24, 0x40},         // JIS C 6226-1978\r
105                 {0x1b, 0x24, 0x41},         // GB 2312-80\r
106                 {0x1b, 0x24, 0x42},         // JIS X 208-1983\r
107                 {0x1b, 0x26, 0x40},         // JIS X 208 1990, 1997\r
108                 {0x1b, 0x28, 0x42},         // ASCII\r
109                 {0x1b, 0x28, 0x48},         // JIS-Roman\r
110                 {0x1b, 0x28, 0x49},         // Half-width katakana\r
111                 {0x1b, 0x28, 0x4a},         // JIS-Roman\r
112                 {0x1b, 0x2e, 0x41},         // ISO 8859-1\r
113                 {0x1b, 0x2e, 0x46}          // ISO 8859-7\r
114                 };\r
115         \r
116         String getName() {\r
117             return "ISO-2022-JP";\r
118         }\r
119         \r
120         int   match(CharsetDetector det) {\r
121             return match(det.fInputBytes, det.fInputLen, escapeSequences);\r
122         }\r
123     }\r
124 \r
125     static class CharsetRecog_2022KR extends CharsetRecog_2022 {\r
126         private byte [] [] escapeSequences = {\r
127                 {0x1b, 0x24, 0x29, 0x43}   \r
128                  };\r
129         \r
130         String getName() {\r
131             return "ISO-2022-KR";\r
132         }\r
133         \r
134         int   match(CharsetDetector det) {\r
135             return match(det.fInputBytes, det.fInputLen, escapeSequences);\r
136         }\r
137         \r
138     }\r
139 \r
140     static class CharsetRecog_2022CN extends CharsetRecog_2022 {\r
141         private byte [] [] escapeSequences = {\r
142                 {0x1b, 0x24, 0x29, 0x41},   // GB 2312-80\r
143                 {0x1b, 0x24, 0x29, 0x47},   // CNS 11643-1992 Plane 1\r
144                 {0x1b, 0x24, 0x2A, 0x48},   // CNS 11643-1992 Plane 2\r
145                 {0x1b, 0x24, 0x29, 0x45},   // ISO-IR-165\r
146                 {0x1b, 0x24, 0x2B, 0x49},   // CNS 11643-1992 Plane 3\r
147                 {0x1b, 0x24, 0x2B, 0x4A},   // CNS 11643-1992 Plane 4\r
148                 {0x1b, 0x24, 0x2B, 0x4B},   // CNS 11643-1992 Plane 5\r
149                 {0x1b, 0x24, 0x2B, 0x4C},   // CNS 11643-1992 Plane 6\r
150                 {0x1b, 0x24, 0x2B, 0x4D},   // CNS 11643-1992 Plane 7\r
151                 {0x1b, 0x4e},               // SS2\r
152                 {0x1b, 0x4f},               // SS3\r
153         };\r
154         \r
155         String getName() {\r
156             return "ISO-2022-CN";\r
157         }\r
158         \r
159         \r
160         int   match(CharsetDetector det) {\r
161             return match(det.fInputBytes, det.fInputLen, escapeSequences);\r
162         }\r
163     }\r
164     \r
165     }\r
166 \r