blob: e62ae6fd9e23b30a15bf76feb5ab4fb4b60744d5 [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
Ken Wakasa5460ea32012-07-30 16:27:44 +09002 * Copyright (C) 2010, The Android Open Source Project
Ken Wakasa0bbb9172012-07-25 17:51:43 +09003 *
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 */
satok30088252010-12-01 21:22:15 +090016
Ken Wakasaf1008c52012-07-31 17:56:40 +090017#include <cstring>
satok18c28f42010-12-02 18:11:54 +090018
satoke808e432010-12-02 14:53:24 +090019#define LOG_TAG "LatinIME: bigram_dictionary.cpp"
20
satok30088252010-12-01 21:22:15 +090021#include "bigram_dictionary.h"
Jean Chalard588e2f22011-07-25 14:03:19 +090022#include "binary_format.h"
Jean Chalard49ba1352012-05-07 20:14:00 +090023#include "bloom_filter.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090024#include "defines.h"
Jean Chalard49ba1352012-05-07 20:14:00 +090025#include "dictionary.h"
satok30088252010-12-01 21:22:15 +090026
27namespace latinime {
28
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090029BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength, int maxPredictions)
30 : DICT(dict), MAX_WORD_LENGTH(maxWordLength), MAX_PREDICTIONS(maxPredictions) {
Ken Wakasade3070a2011-03-19 09:16:42 +090031 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090032 AKLOGI("BigramDictionary - constructor");
Ken Wakasade3070a2011-03-19 09:16:42 +090033 }
satok30088252010-12-01 21:22:15 +090034}
35
satok18c28f42010-12-02 18:11:54 +090036BigramDictionary::~BigramDictionary() {
satok30088252010-12-01 21:22:15 +090037}
satok18c28f42010-12-02 18:11:54 +090038
Ken Wakasa1e614932012-10-29 18:06:22 +090039bool BigramDictionary::addWordBigram(int *word, int length, int frequency, int *bigramFreq,
40 int *bigramCodePoints, int *outputTypes) const {
satok18c28f42010-12-02 18:11:54 +090041 word[length] = 0;
42 if (DEBUG_DICT) {
Doug Kwance9efbf2011-07-07 22:53:50 -070043#ifdef FLAG_DBG
satok18c28f42010-12-02 18:11:54 +090044 char s[length + 1];
Ken Wakasa1e614932012-10-29 18:06:22 +090045 for (int i = 0; i <= length; i++) s[i] = static_cast<char>(word[i]);
satok9fb6f472012-01-13 18:01:22 +090046 AKLOGI("Bigram: Found word = %s, freq = %d :", s, frequency);
satok787945b2011-07-14 08:32:57 +090047#endif
satok18c28f42010-12-02 18:11:54 +090048 }
49
50 // Find the right insertion point
51 int insertAt = 0;
Jean Chalard1a69ad52012-07-11 11:37:36 +090052 while (insertAt < MAX_PREDICTIONS) {
satokb1ed1d42012-06-14 16:35:23 -070053 if (frequency > bigramFreq[insertAt] || (bigramFreq[insertAt] == frequency
Ken Wakasa1e614932012-10-29 18:06:22 +090054 && length < Dictionary::wideStrLen(
55 bigramCodePoints + insertAt * MAX_WORD_LENGTH))) {
satok18c28f42010-12-02 18:11:54 +090056 break;
57 }
58 insertAt++;
59 }
Ken Wakasade3070a2011-03-19 09:16:42 +090060 if (DEBUG_DICT) {
Jean Chalard1a69ad52012-07-11 11:37:36 +090061 AKLOGI("Bigram: InsertAt -> %d MAX_PREDICTIONS: %d", insertAt, MAX_PREDICTIONS);
Ken Wakasade3070a2011-03-19 09:16:42 +090062 }
Jean Chalard1a69ad52012-07-11 11:37:36 +090063 if (insertAt < MAX_PREDICTIONS) {
Ken Wakasa267030d2012-08-14 18:54:13 +090064 memmove(bigramFreq + (insertAt + 1),
65 bigramFreq + insertAt,
Ken Wakasabcec82d2012-08-12 11:10:48 +090066 (MAX_PREDICTIONS - insertAt - 1) * sizeof(bigramFreq[0]));
satokb1ed1d42012-06-14 16:35:23 -070067 bigramFreq[insertAt] = frequency;
Jean Chalardc7387a42012-07-12 15:21:11 +090068 outputTypes[insertAt] = Dictionary::KIND_PREDICTION;
Ken Wakasa1e614932012-10-29 18:06:22 +090069 memmove(bigramCodePoints + (insertAt + 1) * MAX_WORD_LENGTH,
70 bigramCodePoints + insertAt * MAX_WORD_LENGTH,
71 (MAX_PREDICTIONS - insertAt - 1) * sizeof(bigramCodePoints[0]) * MAX_WORD_LENGTH);
72 int *dest = bigramCodePoints + insertAt * MAX_WORD_LENGTH;
satok18c28f42010-12-02 18:11:54 +090073 while (length--) {
74 *dest++ = *word++;
75 }
76 *dest = 0; // NULL terminate
Ken Wakasade3070a2011-03-19 09:16:42 +090077 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090078 AKLOGI("Bigram: Added word at %d", insertAt);
Ken Wakasade3070a2011-03-19 09:16:42 +090079 }
satok18c28f42010-12-02 18:11:54 +090080 return true;
81 }
82 return false;
83}
84
Jean Chalard588e2f22011-07-25 14:03:19 +090085/* Parameters :
86 * prevWord: the word before, the one for which we need to look up bigrams.
87 * prevWordLength: its length.
satokb1ed1d42012-06-14 16:35:23 -070088 * inputCodes: what user typed, in the same format as for UnigramDictionary::getSuggestions.
Jean Chalard588e2f22011-07-25 14:03:19 +090089 * codesSize: the size of the codes array.
Ken Wakasa1e614932012-10-29 18:06:22 +090090 * bigramCodePoints: an array for output, at the same format as outwords for getSuggestions.
Jean Chalard588e2f22011-07-25 14:03:19 +090091 * bigramFreq: an array to output frequencies.
Jean Chalard6931df92012-07-12 12:55:48 +090092 * outputTypes: an array to output types.
Jean Chalard588e2f22011-07-25 14:03:19 +090093 * This method returns the number of bigrams this word has, for backward compatibility.
94 * Note: this is not the number of bigrams output in the array, which is the number of
95 * bigrams this word has WHOSE first letter also matches the letter the user typed.
96 * TODO: this may not be a sensible thing to do. It makes sense when the bigrams are
97 * used to match the first letter of the second word, but once the user has typed more
98 * and the bigrams are used to boost unigram result scores, it makes little sense to
99 * reduce their scope to the ones that match the first letter.
100 */
Ken Wakasa1e614932012-10-29 18:06:22 +0900101int BigramDictionary::getBigrams(const int *prevWord, int prevWordLength, int *inputCodes,
102 int codesSize, int *bigramCodePoints, int *bigramFreq, int *outputTypes) const {
Jean Chalard588e2f22011-07-25 14:03:19 +0900103 // 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
satok18c28f42010-12-02 18:11:54 +0900105
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900106 const uint8_t *const root = DICT;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900107 int pos = getBigramListPositionForWord(prevWord, prevWordLength,
108 false /* forceLowerCaseSearch */);
Jean Chalard351864b2012-04-24 18:06:51 +0900109 // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
Jean Chalarde9a86e22012-06-28 21:01:29 +0900110 if (0 == pos) {
111 // If no bigrams for this exact word, search again in lower case.
112 pos = getBigramListPositionForWord(prevWord, prevWordLength,
113 true /* forceLowerCaseSearch */);
114 }
115 // If still no bigrams, we really don't have them!
Jean Chalardee396df2012-04-17 16:02:35 +0900116 if (0 == pos) return 0;
Ken Wakasad86d3132012-09-04 20:29:38 +0900117 uint8_t bigramFlags;
Jean Chalard588e2f22011-07-25 14:03:19 +0900118 int bigramCount = 0;
119 do {
120 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
Ken Wakasa1e614932012-10-29 18:06:22 +0900121 int bigramBuffer[MAX_WORD_LENGTH];
Jean Chalard62cd9192012-05-30 14:46:43 +0900122 int unigramFreq = 0;
Jean Chalard588e2f22011-07-25 14:03:19 +0900123 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
124 &pos);
125 const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH,
Jean Chalarde3084592012-05-29 16:22:46 +0900126 bigramBuffer, &unigramFreq);
satok18c28f42010-12-02 18:11:54 +0900127
Jean Chalardad290d62012-02-15 19:22:26 -0800128 // codesSize == 0 means we are trying to find bigram predictions.
satokb1ed1d42012-06-14 16:35:23 -0700129 if (codesSize < 1 || checkFirstCharacter(bigramBuffer, inputCodes)) {
Jean Chalard19560502012-07-31 23:45:32 +0900130 const int bigramFreqTemp = BinaryFormat::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
Jean Chalard46fe49f2012-05-29 16:50:25 +0900131 // Due to space constraints, the frequency for bigrams is approximate - the lower the
132 // unigram frequency, the worse the precision. The theoritical maximum error in
133 // resulting frequency is 8 - although in the practice it's never bigger than 3 or 4
134 // in very bad cases. This means that sometimes, we'll see some bigrams interverted
135 // here, but it can't get too bad.
Jean Chalarde3084592012-05-29 16:22:46 +0900136 const int frequency =
satokb1ed1d42012-06-14 16:35:23 -0700137 BinaryFormat::computeFrequencyForBigram(unigramFreq, bigramFreqTemp);
Ken Wakasa1e614932012-10-29 18:06:22 +0900138 if (addWordBigram(bigramBuffer, length, frequency, bigramFreq, bigramCodePoints,
Jean Chalardc7387a42012-07-12 15:21:11 +0900139 outputTypes)) {
Jean Chalard9715cc42012-03-21 15:26:45 +0900140 ++bigramCount;
141 }
satok18c28f42010-12-02 18:11:54 +0900142 }
Jean Chalard19560502012-07-31 23:45:32 +0900143 } while (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
Jean Chalard588e2f22011-07-25 14:03:19 +0900144 return bigramCount;
satok18c28f42010-12-02 18:11:54 +0900145}
146
Jean Chalardee396df2012-04-17 16:02:35 +0900147// Returns a pointer to the start of the bigram list.
148// If the word is not found or has no bigrams, this function returns 0.
Ken Wakasaaa5a3e82012-12-03 19:54:30 +0900149int BigramDictionary::getBigramListPositionForWord(const int *prevWord, const int prevWordLength,
150 const bool forceLowerCaseSearch) const {
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900151 if (0 >= prevWordLength) return 0;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900152 const uint8_t *const root = DICT;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900153 int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength,
154 forceLowerCaseSearch);
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900155
156 if (NOT_VALID_WORD == pos) return 0;
Ken Wakasad86d3132012-09-04 20:29:38 +0900157 const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
Jean Chalard19560502012-07-31 23:45:32 +0900158 if (0 == (flags & BinaryFormat::FLAG_HAS_BIGRAMS)) return 0;
159 if (0 == (flags & BinaryFormat::FLAG_HAS_MULTIPLE_CHARS)) {
Ken Wakasaf2789812012-09-04 12:49:46 +0900160 BinaryFormat::getCodePointAndForwardPointer(root, &pos);
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900161 } else {
162 pos = BinaryFormat::skipOtherCharacters(root, pos);
163 }
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900164 pos = BinaryFormat::skipFrequency(flags, pos);
Jean Chalard402b0572012-05-29 15:56:30 +0900165 pos = BinaryFormat::skipChildrenPosition(flags, pos);
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900166 pos = BinaryFormat::skipShortcuts(root, flags, pos);
167 return pos;
168}
169
Ken Wakasaaa5a3e82012-12-03 19:54:30 +0900170void BigramDictionary::fillBigramAddressToFrequencyMapAndFilter(const int *prevWord,
satokb1ed1d42012-06-14 16:35:23 -0700171 const int prevWordLength, std::map<int, int> *map, uint8_t *filter) const {
Jean Chalardf1634c82012-05-02 19:05:27 +0900172 memset(filter, 0, BIGRAM_FILTER_BYTE_SIZE);
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900173 const uint8_t *const root = DICT;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900174 int pos = getBigramListPositionForWord(prevWord, prevWordLength,
175 false /* forceLowerCaseSearch */);
176 if (0 == pos) {
177 // If no bigrams for this exact string, search again in lower case.
178 pos = getBigramListPositionForWord(prevWord, prevWordLength,
179 true /* forceLowerCaseSearch */);
180 }
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900181 if (0 == pos) return;
182
Ken Wakasad86d3132012-09-04 20:29:38 +0900183 uint8_t bigramFlags;
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900184 do {
185 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
Jean Chalard19560502012-07-31 23:45:32 +0900186 const int frequency = BinaryFormat::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900187 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
188 &pos);
189 (*map)[bigramPos] = frequency;
Jean Chalardf1634c82012-05-02 19:05:27 +0900190 setInFilter(filter, bigramPos);
Jean Chalard19560502012-07-31 23:45:32 +0900191 } while (0 != (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags));
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900192}
193
Ken Wakasa1e614932012-10-29 18:06:22 +0900194bool BigramDictionary::checkFirstCharacter(int *word, int *inputCodes) const {
satok18c28f42010-12-02 18:11:54 +0900195 // Checks whether this word starts with same character or neighboring characters of
196 // what user typed.
197
satok18c28f42010-12-02 18:11:54 +0900198 int maxAlt = MAX_ALTERNATIVES;
Ken Wakasa1e614932012-10-29 18:06:22 +0900199 const int firstBaseChar = toBaseLowerCase(*word);
satok18c28f42010-12-02 18:11:54 +0900200 while (maxAlt > 0) {
Tom Ouyangaeda8a72012-03-24 15:31:27 +0900201 if (toBaseLowerCase(*inputCodes) == firstBaseChar) {
satok18c28f42010-12-02 18:11:54 +0900202 return true;
203 }
204 inputCodes++;
205 maxAlt--;
206 }
207 return false;
208}
209
Ken Wakasaaa5a3e82012-12-03 19:54:30 +0900210bool BigramDictionary::isValidBigram(const int *word1, int length1, const int *word2,
satokb1ed1d42012-06-14 16:35:23 -0700211 int length2) const {
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900212 const uint8_t *const root = DICT;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900213 int pos = getBigramListPositionForWord(word1, length1, false /* forceLowerCaseSearch */);
Tom Ouyang4d289d32012-04-26 23:50:21 -0700214 // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
215 if (0 == pos) return false;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900216 int nextWordPos = BinaryFormat::getTerminalPosition(root, word2, length2,
217 false /* forceLowerCaseSearch */);
Tom Ouyang4d289d32012-04-26 23:50:21 -0700218 if (NOT_VALID_WORD == nextWordPos) return false;
Ken Wakasad86d3132012-09-04 20:29:38 +0900219 uint8_t bigramFlags;
Tom Ouyang4d289d32012-04-26 23:50:21 -0700220 do {
221 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
222 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
223 &pos);
224 if (bigramPos == nextWordPos) {
225 return true;
226 }
Jean Chalard19560502012-07-31 23:45:32 +0900227 } while (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
Tom Ouyang4d289d32012-04-26 23:50:21 -0700228 return false;
229}
230
satok30088252010-12-01 21:22:15 +0900231// TODO: Move functions related to bigram to here
232} // namespace latinime