satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2010, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 20 | #define LOG_TAG "LatinIME: bigram_dictionary.cpp" |
| 21 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | #include "bigram_dictionary.h" |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 23 | #include "dictionary.h" |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 24 | #include "binary_format.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 25 | |
| 26 | namespace latinime { |
| 27 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 28 | BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength, |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 29 | Dictionary *parentDictionary) |
Jean Chalard | 5b0761e | 2012-04-06 17:52:18 +0900 | [diff] [blame] | 30 | : DICT(dict), MAX_WORD_LENGTH(maxWordLength), mParentDictionary(parentDictionary) { |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 31 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 32 | AKLOGI("BigramDictionary - constructor"); |
| 33 | AKLOGI("Has Bigram : %d", hasBigram); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 34 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 35 | } |
| 36 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 37 | BigramDictionary::~BigramDictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 38 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 39 | |
| 40 | bool BigramDictionary::addWordBigram(unsigned short *word, int length, int frequency) { |
| 41 | word[length] = 0; |
| 42 | if (DEBUG_DICT) { |
Doug Kwan | ce9efbf | 2011-07-07 22:53:50 -0700 | [diff] [blame] | 43 | #ifdef FLAG_DBG |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 44 | char s[length + 1]; |
| 45 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 46 | AKLOGI("Bigram: Found word = %s, freq = %d :", s, frequency); |
satok | 787945b | 2011-07-14 08:32:57 +0900 | [diff] [blame] | 47 | #endif |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Find the right insertion point |
| 51 | int insertAt = 0; |
| 52 | while (insertAt < mMaxBigrams) { |
| 53 | if (frequency > mBigramFreq[insertAt] || (mBigramFreq[insertAt] == frequency |
| 54 | && length < Dictionary::wideStrLen(mBigramChars + insertAt * MAX_WORD_LENGTH))) { |
| 55 | break; |
| 56 | } |
| 57 | insertAt++; |
| 58 | } |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 59 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 60 | AKLOGI("Bigram: InsertAt -> %d maxBigrams: %d", insertAt, mMaxBigrams); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 61 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 62 | if (insertAt < mMaxBigrams) { |
| 63 | memmove((char*) mBigramFreq + (insertAt + 1) * sizeof(mBigramFreq[0]), |
| 64 | (char*) mBigramFreq + insertAt * sizeof(mBigramFreq[0]), |
| 65 | (mMaxBigrams - insertAt - 1) * sizeof(mBigramFreq[0])); |
| 66 | mBigramFreq[insertAt] = frequency; |
| 67 | memmove((char*) mBigramChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short), |
| 68 | (char*) mBigramChars + (insertAt ) * MAX_WORD_LENGTH * sizeof(short), |
| 69 | (mMaxBigrams - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH); |
| 70 | unsigned short *dest = mBigramChars + (insertAt ) * MAX_WORD_LENGTH; |
| 71 | while (length--) { |
| 72 | *dest++ = *word++; |
| 73 | } |
| 74 | *dest = 0; // NULL terminate |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 75 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 76 | AKLOGI("Bigram: Added word at %d", insertAt); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 77 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 83 | /* Parameters : |
| 84 | * prevWord: the word before, the one for which we need to look up bigrams. |
| 85 | * prevWordLength: its length. |
| 86 | * codes: what user typed, in the same format as for UnigramDictionary::getSuggestions. |
| 87 | * codesSize: the size of the codes array. |
| 88 | * bigramChars: an array for output, at the same format as outwords for getSuggestions. |
| 89 | * bigramFreq: an array to output frequencies. |
| 90 | * maxWordLength: the maximum size of a word. |
| 91 | * maxBigrams: the maximum number of bigrams fitting in the bigramChars array. |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 92 | * This method returns the number of bigrams this word has, for backward compatibility. |
| 93 | * Note: this is not the number of bigrams output in the array, which is the number of |
| 94 | * bigrams this word has WHOSE first letter also matches the letter the user typed. |
| 95 | * TODO: this may not be a sensible thing to do. It makes sense when the bigrams are |
| 96 | * used to match the first letter of the second word, but once the user has typed more |
| 97 | * and the bigrams are used to boost unigram result scores, it makes little sense to |
| 98 | * reduce their scope to the ones that match the first letter. |
| 99 | */ |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 100 | int BigramDictionary::getBigrams(unsigned short *prevWord, int prevWordLength, int *codes, |
| 101 | int codesSize, unsigned short *bigramChars, int *bigramFreq, int maxWordLength, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 102 | int maxBigrams) { |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 103 | // TODO: remove unused arguments, and refrain from storing stuff in members of this class |
| 104 | // TODO: have "in" arguments before "out" ones, and make out args explicit in the name |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 105 | mBigramFreq = bigramFreq; |
| 106 | mBigramChars = bigramChars; |
| 107 | mInputCodes = codes; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 108 | mMaxBigrams = maxBigrams; |
| 109 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 110 | const uint8_t* const root = DICT; |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame^] | 111 | int pos = getBigramListForWord(root, prevWord, prevWordLength); |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 112 | int bigramFlags; |
| 113 | int bigramCount = 0; |
| 114 | do { |
| 115 | bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 116 | uint16_t bigramBuffer[MAX_WORD_LENGTH]; |
| 117 | const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags, |
| 118 | &pos); |
| 119 | const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH, |
| 120 | bigramBuffer); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 121 | |
Jean Chalard | ad290d6 | 2012-02-15 19:22:26 -0800 | [diff] [blame] | 122 | // codesSize == 0 means we are trying to find bigram predictions. |
| 123 | if (codesSize < 1 || checkFirstCharacter(bigramBuffer)) { |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 124 | const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags; |
Jean Chalard | 9715cc4 | 2012-03-21 15:26:45 +0900 | [diff] [blame] | 125 | if (addWordBigram(bigramBuffer, length, frequency)) { |
| 126 | ++bigramCount; |
| 127 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 128 | } |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 129 | } while (0 != (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags)); |
| 130 | return bigramCount; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame^] | 133 | int BigramDictionary::getBigramListForWord(const uint8_t* const root, |
| 134 | const unsigned short *prevWord, const int prevWordLength) { |
| 135 | int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength); |
| 136 | |
| 137 | if (NOT_VALID_WORD == pos) return 0; |
| 138 | const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 139 | if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0; |
| 140 | if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) { |
| 141 | BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
| 142 | } else { |
| 143 | pos = BinaryFormat::skipOtherCharacters(root, pos); |
| 144 | } |
| 145 | pos = BinaryFormat::skipChildrenPosition(flags, pos); |
| 146 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 147 | pos = BinaryFormat::skipShortcuts(root, flags, pos); |
| 148 | return pos; |
| 149 | } |
| 150 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 151 | bool BigramDictionary::checkFirstCharacter(unsigned short *word) { |
| 152 | // Checks whether this word starts with same character or neighboring characters of |
| 153 | // what user typed. |
| 154 | |
| 155 | int *inputCodes = mInputCodes; |
| 156 | int maxAlt = MAX_ALTERNATIVES; |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 157 | const unsigned short firstBaseChar = toBaseLowerCase(*word); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 158 | while (maxAlt > 0) { |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 159 | if (toBaseLowerCase(*inputCodes) == firstBaseChar) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | inputCodes++; |
| 163 | maxAlt--; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 168 | // TODO: Move functions related to bigram to here |
| 169 | } // namespace latinime |