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"); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 33 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 34 | } |
| 35 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 36 | BigramDictionary::~BigramDictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 37 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 38 | |
| 39 | bool BigramDictionary::addWordBigram(unsigned short *word, int length, int frequency) { |
| 40 | word[length] = 0; |
| 41 | if (DEBUG_DICT) { |
Doug Kwan | ce9efbf | 2011-07-07 22:53:50 -0700 | [diff] [blame] | 42 | #ifdef FLAG_DBG |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 43 | char s[length + 1]; |
| 44 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 45 | AKLOGI("Bigram: Found word = %s, freq = %d :", s, frequency); |
satok | 787945b | 2011-07-14 08:32:57 +0900 | [diff] [blame] | 46 | #endif |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Find the right insertion point |
| 50 | int insertAt = 0; |
| 51 | while (insertAt < mMaxBigrams) { |
| 52 | if (frequency > mBigramFreq[insertAt] || (mBigramFreq[insertAt] == frequency |
| 53 | && length < Dictionary::wideStrLen(mBigramChars + insertAt * MAX_WORD_LENGTH))) { |
| 54 | break; |
| 55 | } |
| 56 | insertAt++; |
| 57 | } |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 58 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 59 | AKLOGI("Bigram: InsertAt -> %d maxBigrams: %d", insertAt, mMaxBigrams); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 60 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 61 | if (insertAt < mMaxBigrams) { |
| 62 | memmove((char*) mBigramFreq + (insertAt + 1) * sizeof(mBigramFreq[0]), |
| 63 | (char*) mBigramFreq + insertAt * sizeof(mBigramFreq[0]), |
| 64 | (mMaxBigrams - insertAt - 1) * sizeof(mBigramFreq[0])); |
| 65 | mBigramFreq[insertAt] = frequency; |
| 66 | memmove((char*) mBigramChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short), |
| 67 | (char*) mBigramChars + (insertAt ) * MAX_WORD_LENGTH * sizeof(short), |
| 68 | (mMaxBigrams - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH); |
| 69 | unsigned short *dest = mBigramChars + (insertAt ) * MAX_WORD_LENGTH; |
| 70 | while (length--) { |
| 71 | *dest++ = *word++; |
| 72 | } |
| 73 | *dest = 0; // NULL terminate |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 74 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 75 | AKLOGI("Bigram: Added word at %d", insertAt); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 76 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 82 | /* Parameters : |
| 83 | * prevWord: the word before, the one for which we need to look up bigrams. |
| 84 | * prevWordLength: its length. |
| 85 | * codes: what user typed, in the same format as for UnigramDictionary::getSuggestions. |
| 86 | * codesSize: the size of the codes array. |
| 87 | * bigramChars: an array for output, at the same format as outwords for getSuggestions. |
| 88 | * bigramFreq: an array to output frequencies. |
| 89 | * maxWordLength: the maximum size of a word. |
| 90 | * maxBigrams: the maximum number of bigrams fitting in the bigramChars array. |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 91 | * This method returns the number of bigrams this word has, for backward compatibility. |
| 92 | * Note: this is not the number of bigrams output in the array, which is the number of |
| 93 | * bigrams this word has WHOSE first letter also matches the letter the user typed. |
| 94 | * TODO: this may not be a sensible thing to do. It makes sense when the bigrams are |
| 95 | * used to match the first letter of the second word, but once the user has typed more |
| 96 | * and the bigrams are used to boost unigram result scores, it makes little sense to |
| 97 | * reduce their scope to the ones that match the first letter. |
| 98 | */ |
Jean Chalard | 522a04e | 2012-04-23 15:37:07 +0900 | [diff] [blame] | 99 | int BigramDictionary::getBigrams(const int32_t *prevWord, int prevWordLength, int *codes, |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 100 | int codesSize, unsigned short *bigramChars, int *bigramFreq, int maxWordLength, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 101 | int maxBigrams) { |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 102 | // TODO: remove unused arguments, and refrain from storing stuff in members of this class |
| 103 | // 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] | 104 | mBigramFreq = bigramFreq; |
| 105 | mBigramChars = bigramChars; |
| 106 | mInputCodes = codes; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 107 | mMaxBigrams = maxBigrams; |
| 108 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 109 | const uint8_t* const root = DICT; |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame^] | 110 | int pos = getBigramListPositionForWord(prevWord, prevWordLength); |
| 111 | // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams |
Jean Chalard | ee396df | 2012-04-17 16:02:35 +0900 | [diff] [blame] | 112 | if (0 == pos) return 0; |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 113 | int bigramFlags; |
| 114 | int bigramCount = 0; |
| 115 | do { |
| 116 | bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 117 | uint16_t bigramBuffer[MAX_WORD_LENGTH]; |
| 118 | const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags, |
| 119 | &pos); |
| 120 | const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH, |
| 121 | bigramBuffer); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 122 | |
Jean Chalard | ad290d6 | 2012-02-15 19:22:26 -0800 | [diff] [blame] | 123 | // codesSize == 0 means we are trying to find bigram predictions. |
| 124 | if (codesSize < 1 || checkFirstCharacter(bigramBuffer)) { |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 125 | const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags; |
Jean Chalard | 9715cc4 | 2012-03-21 15:26:45 +0900 | [diff] [blame] | 126 | if (addWordBigram(bigramBuffer, length, frequency)) { |
| 127 | ++bigramCount; |
| 128 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 129 | } |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 130 | } while (0 != (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags)); |
| 131 | return bigramCount; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Jean Chalard | ee396df | 2012-04-17 16:02:35 +0900 | [diff] [blame] | 134 | // Returns a pointer to the start of the bigram list. |
| 135 | // If the word is not found or has no bigrams, this function returns 0. |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame^] | 136 | int BigramDictionary::getBigramListPositionForWord(const int32_t *prevWord, |
| 137 | const int prevWordLength) { |
| 138 | const uint8_t* const root = DICT; |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame] | 139 | int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength); |
| 140 | |
| 141 | if (NOT_VALID_WORD == pos) return 0; |
| 142 | const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 143 | if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0; |
| 144 | if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) { |
| 145 | BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
| 146 | } else { |
| 147 | pos = BinaryFormat::skipOtherCharacters(root, pos); |
| 148 | } |
| 149 | pos = BinaryFormat::skipChildrenPosition(flags, pos); |
| 150 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 151 | pos = BinaryFormat::skipShortcuts(root, flags, pos); |
| 152 | return pos; |
| 153 | } |
| 154 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 155 | bool BigramDictionary::checkFirstCharacter(unsigned short *word) { |
| 156 | // Checks whether this word starts with same character or neighboring characters of |
| 157 | // what user typed. |
| 158 | |
| 159 | int *inputCodes = mInputCodes; |
| 160 | int maxAlt = MAX_ALTERNATIVES; |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 161 | const unsigned short firstBaseChar = toBaseLowerCase(*word); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 162 | while (maxAlt > 0) { |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 163 | if (toBaseLowerCase(*inputCodes) == firstBaseChar) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | inputCodes++; |
| 167 | maxAlt--; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 172 | // TODO: Move functions related to bigram to here |
| 173 | } // namespace latinime |