|
1 /* |
|
2 ********************************************************************** |
|
3 * Copyright (C) 2005-2012, International Business Machines |
|
4 * Corporation and others. All Rights Reserved. |
|
5 ********************************************************************** |
|
6 */ |
|
7 |
|
8 #include "unicode/utypes.h" |
|
9 |
|
10 #if !UCONFIG_NO_CONVERSION |
|
11 |
|
12 #include "cstring.h" |
|
13 |
|
14 #include "csr2022.h" |
|
15 #include "csmatch.h" |
|
16 |
|
17 U_NAMESPACE_BEGIN |
|
18 |
|
19 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) |
|
20 |
|
21 /** |
|
22 * Matching function shared among the 2022 detectors JP, CN and KR |
|
23 * Counts up the number of legal and unrecognized escape sequences in |
|
24 * the sample of text, and computes a score based on the total number & |
|
25 * the proportion that fit the encoding. |
|
26 * |
|
27 * |
|
28 * @param text the byte buffer containing text to analyse |
|
29 * @param textLen the size of the text in the byte. |
|
30 * @param escapeSequences the byte escape sequences to test for. |
|
31 * @return match quality, in the range of 0-100. |
|
32 */ |
|
33 int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, const uint8_t escapeSequences[][5], int32_t escapeSequences_length) const |
|
34 { |
|
35 int32_t i, j; |
|
36 int32_t escN; |
|
37 int32_t hits = 0; |
|
38 int32_t misses = 0; |
|
39 int32_t shifts = 0; |
|
40 int32_t quality; |
|
41 |
|
42 i = 0; |
|
43 while(i < textLen) { |
|
44 if(text[i] == 0x1B) { |
|
45 escN = 0; |
|
46 while(escN < escapeSequences_length) { |
|
47 const uint8_t *seq = escapeSequences[escN]; |
|
48 int32_t seq_length = (int32_t)uprv_strlen((const char *) seq); |
|
49 |
|
50 if (textLen-i >= seq_length) { |
|
51 j = 1; |
|
52 while(j < seq_length) { |
|
53 if(seq[j] != text[i+j]) { |
|
54 goto checkEscapes; |
|
55 } |
|
56 |
|
57 j += 1; |
|
58 } |
|
59 |
|
60 hits += 1; |
|
61 i += seq_length-1; |
|
62 goto scanInput; |
|
63 } |
|
64 // else we ran out of string to compare this time. |
|
65 checkEscapes: |
|
66 escN += 1; |
|
67 } |
|
68 |
|
69 misses += 1; |
|
70 } |
|
71 |
|
72 if( text[i]== 0x0e || text[i] == 0x0f){ |
|
73 shifts += 1; |
|
74 } |
|
75 |
|
76 scanInput: |
|
77 i += 1; |
|
78 } |
|
79 |
|
80 if (hits == 0) { |
|
81 return 0; |
|
82 } |
|
83 |
|
84 // |
|
85 // Initial quality is based on relative proportion of recongized vs. |
|
86 // unrecognized escape sequences. |
|
87 // All good: quality = 100; |
|
88 // half or less good: quality = 0; |
|
89 // linear inbetween. |
|
90 quality = (100*hits - 100*misses) / (hits + misses); |
|
91 |
|
92 // Back off quality if there were too few escape sequences seen. |
|
93 // Include shifts in this computation, so that KR does not get penalized |
|
94 // for having only a single Escape sequence, but many shifts. |
|
95 if (hits+shifts < 5) { |
|
96 quality -= (5-(hits+shifts))*10; |
|
97 } |
|
98 |
|
99 if (quality < 0) { |
|
100 quality = 0; |
|
101 } |
|
102 |
|
103 return quality; |
|
104 } |
|
105 |
|
106 |
|
107 static const uint8_t escapeSequences_2022JP[][5] = { |
|
108 {0x1b, 0x24, 0x28, 0x43, 0x00}, // KS X 1001:1992 |
|
109 {0x1b, 0x24, 0x28, 0x44, 0x00}, // JIS X 212-1990 |
|
110 {0x1b, 0x24, 0x40, 0x00, 0x00}, // JIS C 6226-1978 |
|
111 {0x1b, 0x24, 0x41, 0x00, 0x00}, // GB 2312-80 |
|
112 {0x1b, 0x24, 0x42, 0x00, 0x00}, // JIS X 208-1983 |
|
113 {0x1b, 0x26, 0x40, 0x00, 0x00}, // JIS X 208 1990, 1997 |
|
114 {0x1b, 0x28, 0x42, 0x00, 0x00}, // ASCII |
|
115 {0x1b, 0x28, 0x48, 0x00, 0x00}, // JIS-Roman |
|
116 {0x1b, 0x28, 0x49, 0x00, 0x00}, // Half-width katakana |
|
117 {0x1b, 0x28, 0x4a, 0x00, 0x00}, // JIS-Roman |
|
118 {0x1b, 0x2e, 0x41, 0x00, 0x00}, // ISO 8859-1 |
|
119 {0x1b, 0x2e, 0x46, 0x00, 0x00} // ISO 8859-7 |
|
120 }; |
|
121 |
|
122 static const uint8_t escapeSequences_2022KR[][5] = { |
|
123 {0x1b, 0x24, 0x29, 0x43, 0x00} |
|
124 }; |
|
125 |
|
126 static const uint8_t escapeSequences_2022CN[][5] = { |
|
127 {0x1b, 0x24, 0x29, 0x41, 0x00}, // GB 2312-80 |
|
128 {0x1b, 0x24, 0x29, 0x47, 0x00}, // CNS 11643-1992 Plane 1 |
|
129 {0x1b, 0x24, 0x2A, 0x48, 0x00}, // CNS 11643-1992 Plane 2 |
|
130 {0x1b, 0x24, 0x29, 0x45, 0x00}, // ISO-IR-165 |
|
131 {0x1b, 0x24, 0x2B, 0x49, 0x00}, // CNS 11643-1992 Plane 3 |
|
132 {0x1b, 0x24, 0x2B, 0x4A, 0x00}, // CNS 11643-1992 Plane 4 |
|
133 {0x1b, 0x24, 0x2B, 0x4B, 0x00}, // CNS 11643-1992 Plane 5 |
|
134 {0x1b, 0x24, 0x2B, 0x4C, 0x00}, // CNS 11643-1992 Plane 6 |
|
135 {0x1b, 0x24, 0x2B, 0x4D, 0x00}, // CNS 11643-1992 Plane 7 |
|
136 {0x1b, 0x4e, 0x00, 0x00, 0x00}, // SS2 |
|
137 {0x1b, 0x4f, 0x00, 0x00, 0x00}, // SS3 |
|
138 }; |
|
139 |
|
140 CharsetRecog_2022JP::~CharsetRecog_2022JP() {} |
|
141 |
|
142 const char *CharsetRecog_2022JP::getName() const { |
|
143 return "ISO-2022-JP"; |
|
144 } |
|
145 |
|
146 UBool CharsetRecog_2022JP::match(InputText *textIn, CharsetMatch *results) const { |
|
147 int32_t confidence = match_2022(textIn->fInputBytes, |
|
148 textIn->fInputLen, |
|
149 escapeSequences_2022JP, |
|
150 ARRAY_SIZE(escapeSequences_2022JP)); |
|
151 results->set(textIn, this, confidence); |
|
152 return (confidence > 0); |
|
153 } |
|
154 |
|
155 CharsetRecog_2022KR::~CharsetRecog_2022KR() {} |
|
156 |
|
157 const char *CharsetRecog_2022KR::getName() const { |
|
158 return "ISO-2022-KR"; |
|
159 } |
|
160 |
|
161 UBool CharsetRecog_2022KR::match(InputText *textIn, CharsetMatch *results) const { |
|
162 int32_t confidence = match_2022(textIn->fInputBytes, |
|
163 textIn->fInputLen, |
|
164 escapeSequences_2022KR, |
|
165 ARRAY_SIZE(escapeSequences_2022KR)); |
|
166 results->set(textIn, this, confidence); |
|
167 return (confidence > 0); |
|
168 } |
|
169 |
|
170 CharsetRecog_2022CN::~CharsetRecog_2022CN() {} |
|
171 |
|
172 const char *CharsetRecog_2022CN::getName() const { |
|
173 return "ISO-2022-CN"; |
|
174 } |
|
175 |
|
176 UBool CharsetRecog_2022CN::match(InputText *textIn, CharsetMatch *results) const { |
|
177 int32_t confidence = match_2022(textIn->fInputBytes, |
|
178 textIn->fInputLen, |
|
179 escapeSequences_2022CN, |
|
180 ARRAY_SIZE(escapeSequences_2022CN)); |
|
181 results->set(textIn, this, confidence); |
|
182 return (confidence > 0); |
|
183 } |
|
184 |
|
185 CharsetRecog_2022::~CharsetRecog_2022() { |
|
186 // nothing to do |
|
187 } |
|
188 |
|
189 U_NAMESPACE_END |
|
190 #endif |