blob: ab033ad905b63bb399270bf0957ecbd6bd111e1a [file] [log] [blame]
Jean Chalard1059f272011-06-28 20:45:05 +09001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LATINIME_BINARY_FORMAT_H
18#define LATINIME_BINARY_FORMAT_H
19
Jean Chalard46a1eec2012-02-27 19:48:47 +090020#include <limits>
Jean Chalardf0a98092011-07-20 18:42:32 +090021#include "unigram_dictionary.h"
22
Jean Chalard1059f272011-06-28 20:45:05 +090023namespace latinime {
24
25class BinaryFormat {
Ken Wakasae12e9b52012-01-06 12:24:38 +090026 private:
Jean Chalard1059f272011-06-28 20:45:05 +090027 const static int32_t MINIMAL_ONE_BYTE_CHARACTER_VALUE = 0x20;
28 const static int32_t CHARACTER_ARRAY_TERMINATOR = 0x1F;
29 const static int MULTIPLE_BYTE_CHARACTER_ADDITIONAL_SIZE = 2;
30
Ken Wakasae12e9b52012-01-06 12:24:38 +090031 public:
Jean Chalardf0a98092011-07-20 18:42:32 +090032 const static int UNKNOWN_FORMAT = -1;
Jean Chalard46a1eec2012-02-27 19:48:47 +090033 // Originally, format version 1 had a 16-bit magic number, then the version number `01'
34 // then options that must be 0. Hence the first 32-bits of the format are always as follow
35 // and it's okay to consider them a magic number as a whole.
36 const static uint32_t FORMAT_VERSION_1_MAGIC_NUMBER = 0x78B10100;
37 const static unsigned int FORMAT_VERSION_1_HEADER_SIZE = 5;
38 // The versions of Latin IME that only handle format version 1 only test for the magic
39 // number, so we had to change it so that version 2 files would be rejected by older
40 // implementations. On this occasion, we made the magic number 32 bits long.
41 const static uint32_t FORMAT_VERSION_2_MAGIC_NUMBER = 0x9BC13AFE;
Jean Chalardf0a98092011-07-20 18:42:32 +090042
43 static int detectFormat(const uint8_t* const dict);
Jean Chalard46a1eec2012-02-27 19:48:47 +090044 static unsigned int getHeaderSize(const uint8_t* const dict);
Jean Chalard1059f272011-06-28 20:45:05 +090045 static int getGroupCountAndForwardPointer(const uint8_t* const dict, int* pos);
46 static uint8_t getFlagsAndForwardPointer(const uint8_t* const dict, int* pos);
47 static int32_t getCharCodeAndForwardPointer(const uint8_t* const dict, int* pos);
48 static int readFrequencyWithoutMovingPointer(const uint8_t* const dict, const int pos);
49 static int skipOtherCharacters(const uint8_t* const dict, const int pos);
50 static int skipAttributes(const uint8_t* const dict, const int pos);
51 static int skipChildrenPosition(const uint8_t flags, const int pos);
52 static int skipFrequency(const uint8_t flags, const int pos);
53 static int skipAllAttributes(const uint8_t* const dict, const uint8_t flags, const int pos);
54 static int skipChildrenPosAndAttributes(const uint8_t* const dict, const uint8_t flags,
55 const int pos);
56 static int readChildrenPosition(const uint8_t* const dict, const uint8_t flags, const int pos);
57 static bool hasChildrenInFlags(const uint8_t flags);
58 static int getAttributeAddressAndForwardPointer(const uint8_t* const dict, const uint8_t flags,
59 int *pos);
Jean Chalard6a0e9642011-07-25 18:17:11 +090060 static int getTerminalPosition(const uint8_t* const root, const uint16_t* const inWord,
61 const int length);
Jean Chalard588e2f22011-07-25 14:03:19 +090062 static int getWordAtAddress(const uint8_t* const root, const int address, const int maxDepth,
63 uint16_t* outWord);
Jean Chalard1059f272011-06-28 20:45:05 +090064};
65
Jean Chalardf0a98092011-07-20 18:42:32 +090066inline int BinaryFormat::detectFormat(const uint8_t* const dict) {
Jean Chalard46a1eec2012-02-27 19:48:47 +090067 // The magic number is stored big-endian.
68 const uint32_t magicNumber = (dict[0] << 24) + (dict[1] << 16) + (dict[2] << 8) + dict[3];
69 switch (magicNumber) {
70 case FORMAT_VERSION_1_MAGIC_NUMBER:
71 // Format 1 header is exactly 5 bytes long and looks like:
72 // Magic number (2 bytes) 0x78 0xB1
73 // Version number (1 byte) 0x01
74 // Options (2 bytes) must be 0x00 0x00
75 return 1;
76 case FORMAT_VERSION_2_MAGIC_NUMBER:
77 // Format 2 header is as follows:
78 // Magic number (4 bytes) 0x9B 0xC1 0x3A 0xFE
79 // Version number (2 bytes) 0x00 0x02
80 // Options (2 bytes) must be 0x00 0x00
81 // Header size (4 bytes) : integer, big endian
82 return (dict[4] << 8) + dict[5];
83 default:
84 return UNKNOWN_FORMAT;
85 }
86}
87
88inline unsigned int BinaryFormat::getHeaderSize(const uint8_t* const dict) {
89 switch (detectFormat(dict)) {
90 case 1:
91 return FORMAT_VERSION_1_HEADER_SIZE;
92 case 2:
93 // See the format of the header in the comment in detectFormat() above
94 return (dict[8] << 24) + (dict[9] << 16) + (dict[10] << 8) + dict[11];
95 default:
96 return std::numeric_limits<unsigned int>::max();
97 }
Jean Chalardf0a98092011-07-20 18:42:32 +090098}
99
Jean Chalard1059f272011-06-28 20:45:05 +0900100inline int BinaryFormat::getGroupCountAndForwardPointer(const uint8_t* const dict, int* pos) {
Jean Chalard4c0eca62012-01-16 15:15:53 +0900101 const int msb = dict[(*pos)++];
102 if (msb < 0x80) return msb;
103 return ((msb & 0x7F) << 8) | dict[(*pos)++];
Jean Chalard1059f272011-06-28 20:45:05 +0900104}
105
106inline uint8_t BinaryFormat::getFlagsAndForwardPointer(const uint8_t* const dict, int* pos) {
107 return dict[(*pos)++];
108}
109
110inline int32_t BinaryFormat::getCharCodeAndForwardPointer(const uint8_t* const dict, int* pos) {
111 const int origin = *pos;
112 const int32_t character = dict[origin];
113 if (character < MINIMAL_ONE_BYTE_CHARACTER_VALUE) {
114 if (character == CHARACTER_ARRAY_TERMINATOR) {
115 *pos = origin + 1;
116 return NOT_A_CHARACTER;
117 } else {
118 *pos = origin + 3;
119 const int32_t char_1 = character << 16;
120 const int32_t char_2 = char_1 + (dict[origin + 1] << 8);
121 return char_2 + dict[origin + 2];
122 }
123 } else {
124 *pos = origin + 1;
125 return character;
126 }
127}
128
129inline int BinaryFormat::readFrequencyWithoutMovingPointer(const uint8_t* const dict,
130 const int pos) {
131 return dict[pos];
132}
133
134inline int BinaryFormat::skipOtherCharacters(const uint8_t* const dict, const int pos) {
135 int currentPos = pos;
136 int32_t character = dict[currentPos++];
137 while (CHARACTER_ARRAY_TERMINATOR != character) {
138 if (character < MINIMAL_ONE_BYTE_CHARACTER_VALUE) {
139 currentPos += MULTIPLE_BYTE_CHARACTER_ADDITIONAL_SIZE;
140 }
141 character = dict[currentPos++];
142 }
143 return currentPos;
144}
145
146static inline int attributeAddressSize(const uint8_t flags) {
147 static const int ATTRIBUTE_ADDRESS_SHIFT = 4;
148 return (flags & UnigramDictionary::MASK_ATTRIBUTE_ADDRESS_TYPE) >> ATTRIBUTE_ADDRESS_SHIFT;
149 /* Note: this is a value-dependant optimization of what may probably be
150 more readably written this way:
151 switch (flags * UnigramDictionary::MASK_ATTRIBUTE_ADDRESS_TYPE) {
152 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE: return 1;
153 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES: return 2;
154 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTE: return 3;
155 default: return 0;
156 }
157 */
158}
159
160inline int BinaryFormat::skipAttributes(const uint8_t* const dict, const int pos) {
161 int currentPos = pos;
162 uint8_t flags = getFlagsAndForwardPointer(dict, &currentPos);
163 while (flags & UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT) {
164 currentPos += attributeAddressSize(flags);
165 flags = getFlagsAndForwardPointer(dict, &currentPos);
166 }
167 currentPos += attributeAddressSize(flags);
168 return currentPos;
169}
170
171static inline int childrenAddressSize(const uint8_t flags) {
172 static const int CHILDREN_ADDRESS_SHIFT = 6;
173 return (UnigramDictionary::MASK_GROUP_ADDRESS_TYPE & flags) >> CHILDREN_ADDRESS_SHIFT;
174 /* See the note in attributeAddressSize. The same applies here */
175}
176
177inline int BinaryFormat::skipChildrenPosition(const uint8_t flags, const int pos) {
178 return pos + childrenAddressSize(flags);
179}
180
181inline int BinaryFormat::skipFrequency(const uint8_t flags, const int pos) {
182 return UnigramDictionary::FLAG_IS_TERMINAL & flags ? pos + 1 : pos;
183}
184
185inline int BinaryFormat::skipAllAttributes(const uint8_t* const dict, const uint8_t flags,
186 const int pos) {
Jean Chalarde0e33962011-12-26 20:23:32 +0900187 // This function skips all attributes: shortcuts and bigrams.
188 int newPos = pos;
189 if (UnigramDictionary::FLAG_HAS_SHORTCUT_TARGETS & flags) {
190 newPos = skipAttributes(dict, newPos);
Jean Chalard1059f272011-06-28 20:45:05 +0900191 }
Jean Chalarde0e33962011-12-26 20:23:32 +0900192 if (UnigramDictionary::FLAG_HAS_BIGRAMS & flags) {
193 newPos = skipAttributes(dict, newPos);
194 }
195 return newPos;
Jean Chalard1059f272011-06-28 20:45:05 +0900196}
197
198inline int BinaryFormat::skipChildrenPosAndAttributes(const uint8_t* const dict,
199 const uint8_t flags, const int pos) {
200 int currentPos = pos;
201 currentPos = skipChildrenPosition(flags, currentPos);
202 currentPos = skipAllAttributes(dict, flags, currentPos);
203 return currentPos;
204}
205
206inline int BinaryFormat::readChildrenPosition(const uint8_t* const dict, const uint8_t flags,
207 const int pos) {
208 int offset = 0;
209 switch (UnigramDictionary::MASK_GROUP_ADDRESS_TYPE & flags) {
210 case UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_ONEBYTE:
211 offset = dict[pos];
212 break;
213 case UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_TWOBYTES:
214 offset = dict[pos] << 8;
215 offset += dict[pos + 1];
216 break;
217 case UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_THREEBYTES:
218 offset = dict[pos] << 16;
219 offset += dict[pos + 1] << 8;
220 offset += dict[pos + 2];
221 break;
222 default:
223 // If we come here, it means we asked for the children of a word with
224 // no children.
225 return -1;
226 }
227 return pos + offset;
228}
229
230inline bool BinaryFormat::hasChildrenInFlags(const uint8_t flags) {
231 return (UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_NOADDRESS
232 != (UnigramDictionary::MASK_GROUP_ADDRESS_TYPE & flags));
233}
234
235inline int BinaryFormat::getAttributeAddressAndForwardPointer(const uint8_t* const dict,
236 const uint8_t flags, int *pos) {
237 int offset = 0;
238 const int origin = *pos;
239 switch (UnigramDictionary::MASK_ATTRIBUTE_ADDRESS_TYPE & flags) {
240 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE:
241 offset = dict[origin];
242 *pos = origin + 1;
243 break;
244 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES:
245 offset = dict[origin] << 8;
246 offset += dict[origin + 1];
247 *pos = origin + 2;
248 break;
249 case UnigramDictionary::FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES:
250 offset = dict[origin] << 16;
251 offset += dict[origin + 1] << 8;
252 offset += dict[origin + 2];
253 *pos = origin + 3;
254 break;
255 }
256 if (UnigramDictionary::FLAG_ATTRIBUTE_OFFSET_NEGATIVE & flags) {
257 return origin - offset;
258 } else {
259 return origin + offset;
260 }
261}
262
Jean Chalard6a0e9642011-07-25 18:17:11 +0900263// This function gets the byte position of the last chargroup of the exact matching word in the
264// dictionary. If no match is found, it returns NOT_VALID_WORD.
265inline int BinaryFormat::getTerminalPosition(const uint8_t* const root,
266 const uint16_t* const inWord, const int length) {
267 int pos = 0;
268 int wordPos = 0;
269
270 while (true) {
271 // If we already traversed the tree further than the word is long, there means
272 // there was no match (or we would have found it).
273 if (wordPos > length) return NOT_VALID_WORD;
274 int charGroupCount = BinaryFormat::getGroupCountAndForwardPointer(root, &pos);
275 const uint16_t wChar = inWord[wordPos];
276 while (true) {
277 // If there are no more character groups in this node, it means we could not
278 // find a matching character for this depth, therefore there is no match.
279 if (0 >= charGroupCount) return NOT_VALID_WORD;
280 const int charGroupPos = pos;
281 const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
282 int32_t character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
283 if (character == wChar) {
284 // This is the correct node. Only one character group may start with the same
285 // char within a node, so either we found our match in this node, or there is
286 // no match and we can return NOT_VALID_WORD. So we will check all the characters
287 // in this character group indeed does match.
288 if (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & flags) {
289 character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
290 while (NOT_A_CHARACTER != character) {
291 ++wordPos;
292 // If we shoot the length of the word we search for, or if we find a single
293 // character that does not match, as explained above, it means the word is
294 // not in the dictionary (by virtue of this chargroup being the only one to
295 // match the word on the first character, but not matching the whole word).
296 if (wordPos > length) return NOT_VALID_WORD;
297 if (inWord[wordPos] != character) return NOT_VALID_WORD;
298 character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
299 }
300 }
301 // If we come here we know that so far, we do match. Either we are on a terminal
302 // and we match the length, in which case we found it, or we traverse children.
303 // If we don't match the length AND don't have children, then a word in the
304 // dictionary fully matches a prefix of the searched word but not the full word.
305 ++wordPos;
306 if (UnigramDictionary::FLAG_IS_TERMINAL & flags) {
307 if (wordPos == length) {
308 return charGroupPos;
309 }
310 pos = BinaryFormat::skipFrequency(UnigramDictionary::FLAG_IS_TERMINAL, pos);
311 }
312 if (UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_NOADDRESS
313 == (UnigramDictionary::MASK_GROUP_ADDRESS_TYPE & flags)) {
314 return NOT_VALID_WORD;
315 }
316 // We have children and we are still shorter than the word we are searching for, so
317 // we need to traverse children. Put the pointer on the children position, and
318 // break
319 pos = BinaryFormat::readChildrenPosition(root, flags, pos);
320 break;
321 } else {
322 // This chargroup does not match, so skip the remaining part and go to the next.
323 if (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & flags) {
324 pos = BinaryFormat::skipOtherCharacters(root, pos);
325 }
326 pos = BinaryFormat::skipFrequency(flags, pos);
327 pos = BinaryFormat::skipChildrenPosAndAttributes(root, flags, pos);
328 }
329 --charGroupCount;
330 }
331 }
332}
333
Jean Chalard588e2f22011-07-25 14:03:19 +0900334// This function searches for a terminal in the dictionary by its address.
335// Due to the fact that words are ordered in the dictionary in a strict breadth-first order,
336// it is possible to check for this with advantageous complexity. For each node, we search
337// for groups with children and compare the children address with the address we look for.
338// When we shoot the address we look for, it means the word we look for is in the children
339// of the previous group. The only tricky part is the fact that if we arrive at the end of a
340// node with the last group's children address still less than what we are searching for, we
341// must descend the last group's children (for example, if the word we are searching for starts
342// with a z, it's the last group of the root node, so all children addresses will be smaller
343// than the address we look for, and we have to descend the z node).
344/* Parameters :
345 * root: the dictionary buffer
346 * address: the byte position of the last chargroup of the word we are searching for (this is
347 * what is stored as the "bigram address" in each bigram)
348 * outword: an array to write the found word, with MAX_WORD_LENGTH size.
349 * Return value : the length of the word, of 0 if the word was not found.
350 */
351inline int BinaryFormat::getWordAtAddress(const uint8_t* const root, const int address,
352 const int maxDepth, uint16_t* outWord) {
353 int pos = 0;
354 int wordPos = 0;
355
356 // One iteration of the outer loop iterates through nodes. As stated above, we will only
357 // traverse nodes that are actually a part of the terminal we are searching, so each time
358 // we enter this loop we are one depth level further than last time.
359 // The only reason we count nodes is because we want to reduce the probability of infinite
360 // looping in case there is a bug. Since we know there is an upper bound to the depth we are
361 // supposed to traverse, it does not hurt to count iterations.
362 for (int loopCount = maxDepth; loopCount > 0; --loopCount) {
363 int lastCandidateGroupPos = 0;
364 // Let's loop through char groups in this node searching for either the terminal
365 // or one of its ascendants.
366 for (int charGroupCount = getGroupCountAndForwardPointer(root, &pos); charGroupCount > 0;
367 --charGroupCount) {
368 const int startPos = pos;
369 const uint8_t flags = getFlagsAndForwardPointer(root, &pos);
370 const int32_t character = getCharCodeAndForwardPointer(root, &pos);
371 if (address == startPos) {
372 // We found the address. Copy the rest of the word in the buffer and return
373 // the length.
374 outWord[wordPos] = character;
375 if (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & flags) {
376 int32_t nextChar = getCharCodeAndForwardPointer(root, &pos);
377 // We count chars in order to avoid infinite loops if the file is broken or
378 // if there is some other bug
379 int charCount = maxDepth;
380 while (-1 != nextChar && --charCount > 0) {
381 outWord[++wordPos] = nextChar;
382 nextChar = getCharCodeAndForwardPointer(root, &pos);
383 }
384 }
385 return ++wordPos;
386 }
387 // We need to skip past this char group, so skip any remaining chars after the
388 // first and possibly the frequency.
389 if (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & flags) {
390 pos = skipOtherCharacters(root, pos);
391 }
392 pos = skipFrequency(flags, pos);
393
394 // The fact that this group has children is very important. Since we already know
395 // that this group does not match, if it has no children we know it is irrelevant
396 // to what we are searching for.
397 const bool hasChildren = (UnigramDictionary::FLAG_GROUP_ADDRESS_TYPE_NOADDRESS !=
398 (UnigramDictionary::MASK_GROUP_ADDRESS_TYPE & flags));
399 // We will write in `found' whether we have passed the children address we are
400 // searching for. For example if we search for "beer", the children of b are less
401 // than the address we are searching for and the children of c are greater. When we
402 // come here for c, we realize this is too big, and that we should descend b.
403 bool found;
404 if (hasChildren) {
405 // Here comes the tricky part. First, read the children position.
406 const int childrenPos = readChildrenPosition(root, flags, pos);
407 if (childrenPos > address) {
408 // If the children pos is greater than address, it means the previous chargroup,
409 // which address is stored in lastCandidateGroupPos, was the right one.
410 found = true;
411 } else if (1 >= charGroupCount) {
412 // However if we are on the LAST group of this node, and we have NOT shot the
413 // address we should descend THIS node. So we trick the lastCandidateGroupPos
414 // so that we will descend this node, not the previous one.
415 lastCandidateGroupPos = startPos;
416 found = true;
417 } else {
418 // Else, we should continue looking.
419 found = false;
420 }
421 } else {
422 // Even if we don't have children here, we could still be on the last group of this
423 // node. If this is the case, we should descend the last group that had children,
424 // and their address is already in lastCandidateGroup.
425 found = (1 >= charGroupCount);
426 }
427
428 if (found) {
429 // Okay, we found the group we should descend. Its address is in
430 // the lastCandidateGroupPos variable, so we just re-read it.
431 if (0 != lastCandidateGroupPos) {
432 const uint8_t lastFlags =
433 getFlagsAndForwardPointer(root, &lastCandidateGroupPos);
434 const int32_t lastChar =
435 getCharCodeAndForwardPointer(root, &lastCandidateGroupPos);
436 // We copy all the characters in this group to the buffer
437 outWord[wordPos] = lastChar;
438 if (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & lastFlags) {
439 int32_t nextChar =
440 getCharCodeAndForwardPointer(root, &lastCandidateGroupPos);
441 int charCount = maxDepth;
442 while (-1 != nextChar && --charCount > 0) {
443 outWord[++wordPos] = nextChar;
444 nextChar = getCharCodeAndForwardPointer(root, &lastCandidateGroupPos);
445 }
446 }
447 ++wordPos;
448 // Now we only need to branch to the children address. Skip the frequency if
449 // it's there, read pos, and break to resume the search at pos.
450 lastCandidateGroupPos = skipFrequency(lastFlags, lastCandidateGroupPos);
451 pos = readChildrenPosition(root, lastFlags, lastCandidateGroupPos);
452 break;
453 } else {
454 // Here is a little tricky part: we come here if we found out that all children
455 // addresses in this group are bigger than the address we are searching for.
456 // Should we conclude the word is not in the dictionary? No! It could still be
457 // one of the remaining chargroups in this node, so we have to keep looking in
458 // this node until we find it (or we realize it's not there either, in which
459 // case it's actually not in the dictionary). Pass the end of this group, ready
460 // to start the next one.
461 pos = skipChildrenPosAndAttributes(root, flags, pos);
462 }
463 } else {
464 // If we did not find it, we should record the last children address for the next
465 // iteration.
466 if (hasChildren) lastCandidateGroupPos = startPos;
467 // Now skip the end of this group (children pos and the attributes if any) so that
468 // our pos is after the end of this char group, at the start of the next one.
469 pos = skipChildrenPosAndAttributes(root, flags, pos);
470 }
471
472 }
473 }
474 // If we have looked through all the chargroups and found no match, the address is
475 // not the address of a terminal in this dictionary.
476 return 0;
477}
478
Jean Chalard1059f272011-06-28 20:45:05 +0900479} // namespace latinime
480
481#endif // LATINIME_BINARY_FORMAT_H