blob: 8c73f4400c0e7a0927cb75049b64a86a40b57c7e [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
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
satok18c28f42010-12-02 18:11:54 +090018#include <string.h>
19
satoke808e432010-12-02 14:53:24 +090020#define LOG_TAG "LatinIME: bigram_dictionary.cpp"
21
satok30088252010-12-01 21:22:15 +090022#include "bigram_dictionary.h"
Jean Chalard588e2f22011-07-25 14:03:19 +090023#include "binary_format.h"
Jean Chalard49ba1352012-05-07 20:14:00 +090024#include "bloom_filter.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090025#include "defines.h"
Jean Chalard49ba1352012-05-07 20:14:00 +090026#include "dictionary.h"
satok30088252010-12-01 21:22:15 +090027
28namespace latinime {
29
satok18c28f42010-12-02 18:11:54 +090030BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength,
satok18c28f42010-12-02 18:11:54 +090031 Dictionary *parentDictionary)
Jean Chalard5b0761e2012-04-06 17:52:18 +090032 : DICT(dict), MAX_WORD_LENGTH(maxWordLength), mParentDictionary(parentDictionary) {
Ken Wakasade3070a2011-03-19 09:16:42 +090033 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090034 AKLOGI("BigramDictionary - constructor");
Ken Wakasade3070a2011-03-19 09:16:42 +090035 }
satok30088252010-12-01 21:22:15 +090036}
37
satok18c28f42010-12-02 18:11:54 +090038BigramDictionary::~BigramDictionary() {
satok30088252010-12-01 21:22:15 +090039}
satok18c28f42010-12-02 18:11:54 +090040
41bool BigramDictionary::addWordBigram(unsigned short *word, int length, int frequency) {
42 word[length] = 0;
43 if (DEBUG_DICT) {
Doug Kwance9efbf2011-07-07 22:53:50 -070044#ifdef FLAG_DBG
satok18c28f42010-12-02 18:11:54 +090045 char s[length + 1];
46 for (int i = 0; i <= length; i++) s[i] = word[i];
satok9fb6f472012-01-13 18:01:22 +090047 AKLOGI("Bigram: Found word = %s, freq = %d :", s, frequency);
satok787945b2011-07-14 08:32:57 +090048#endif
satok18c28f42010-12-02 18:11:54 +090049 }
50
51 // Find the right insertion point
52 int insertAt = 0;
53 while (insertAt < mMaxBigrams) {
54 if (frequency > mBigramFreq[insertAt] || (mBigramFreq[insertAt] == frequency
55 && length < Dictionary::wideStrLen(mBigramChars + insertAt * MAX_WORD_LENGTH))) {
56 break;
57 }
58 insertAt++;
59 }
Ken Wakasade3070a2011-03-19 09:16:42 +090060 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090061 AKLOGI("Bigram: InsertAt -> %d maxBigrams: %d", insertAt, mMaxBigrams);
Ken Wakasade3070a2011-03-19 09:16:42 +090062 }
satok18c28f42010-12-02 18:11:54 +090063 if (insertAt < mMaxBigrams) {
64 memmove((char*) mBigramFreq + (insertAt + 1) * sizeof(mBigramFreq[0]),
65 (char*) mBigramFreq + insertAt * sizeof(mBigramFreq[0]),
66 (mMaxBigrams - insertAt - 1) * sizeof(mBigramFreq[0]));
67 mBigramFreq[insertAt] = frequency;
68 memmove((char*) mBigramChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
69 (char*) mBigramChars + (insertAt ) * MAX_WORD_LENGTH * sizeof(short),
70 (mMaxBigrams - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
71 unsigned short *dest = mBigramChars + (insertAt ) * MAX_WORD_LENGTH;
72 while (length--) {
73 *dest++ = *word++;
74 }
75 *dest = 0; // NULL terminate
Ken Wakasade3070a2011-03-19 09:16:42 +090076 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090077 AKLOGI("Bigram: Added word at %d", insertAt);
Ken Wakasade3070a2011-03-19 09:16:42 +090078 }
satok18c28f42010-12-02 18:11:54 +090079 return true;
80 }
81 return false;
82}
83
Jean Chalard588e2f22011-07-25 14:03:19 +090084/* Parameters :
85 * prevWord: the word before, the one for which we need to look up bigrams.
86 * prevWordLength: its length.
87 * codes: what user typed, in the same format as for UnigramDictionary::getSuggestions.
88 * codesSize: the size of the codes array.
89 * bigramChars: an array for output, at the same format as outwords for getSuggestions.
90 * bigramFreq: an array to output frequencies.
91 * maxWordLength: the maximum size of a word.
92 * maxBigrams: the maximum number of bigrams fitting in the bigramChars array.
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 */
Jean Chalard522a04e2012-04-23 15:37:07 +0900101int BigramDictionary::getBigrams(const int32_t *prevWord, int prevWordLength, int *codes,
satok18c28f42010-12-02 18:11:54 +0900102 int codesSize, unsigned short *bigramChars, int *bigramFreq, int maxWordLength,
satok6ba8de22012-03-28 18:21:04 +0900103 int maxBigrams) {
Jean Chalard588e2f22011-07-25 14:03:19 +0900104 // TODO: remove unused arguments, and refrain from storing stuff in members of this class
105 // TODO: have "in" arguments before "out" ones, and make out args explicit in the name
satok18c28f42010-12-02 18:11:54 +0900106 mBigramFreq = bigramFreq;
107 mBigramChars = bigramChars;
108 mInputCodes = codes;
satok18c28f42010-12-02 18:11:54 +0900109 mMaxBigrams = maxBigrams;
110
Jean Chalard588e2f22011-07-25 14:03:19 +0900111 const uint8_t* const root = DICT;
Jean Chalard351864b2012-04-24 18:06:51 +0900112 int pos = getBigramListPositionForWord(prevWord, prevWordLength);
113 // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
Jean Chalardee396df2012-04-17 16:02:35 +0900114 if (0 == pos) return 0;
Jean Chalard588e2f22011-07-25 14:03:19 +0900115 int bigramFlags;
116 int bigramCount = 0;
117 do {
118 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
119 uint16_t bigramBuffer[MAX_WORD_LENGTH];
120 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
121 &pos);
122 const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH,
123 bigramBuffer);
satok18c28f42010-12-02 18:11:54 +0900124
Jean Chalardad290d62012-02-15 19:22:26 -0800125 // codesSize == 0 means we are trying to find bigram predictions.
126 if (codesSize < 1 || checkFirstCharacter(bigramBuffer)) {
Jean Chalard588e2f22011-07-25 14:03:19 +0900127 const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
Jean Chalard9715cc42012-03-21 15:26:45 +0900128 if (addWordBigram(bigramBuffer, length, frequency)) {
129 ++bigramCount;
130 }
satok18c28f42010-12-02 18:11:54 +0900131 }
Tom Ouyang4d289d32012-04-26 23:50:21 -0700132 } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
Jean Chalard588e2f22011-07-25 14:03:19 +0900133 return bigramCount;
satok18c28f42010-12-02 18:11:54 +0900134}
135
Jean Chalardee396df2012-04-17 16:02:35 +0900136// Returns a pointer to the start of the bigram list.
137// If the word is not found or has no bigrams, this function returns 0.
Jean Chalard351864b2012-04-24 18:06:51 +0900138int BigramDictionary::getBigramListPositionForWord(const int32_t *prevWord,
139 const int prevWordLength) {
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900140 if (0 >= prevWordLength) return 0;
Jean Chalard351864b2012-04-24 18:06:51 +0900141 const uint8_t* const root = DICT;
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900142 int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
143
144 if (NOT_VALID_WORD == pos) return 0;
145 const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
146 if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
147 if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
148 BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
149 } else {
150 pos = BinaryFormat::skipOtherCharacters(root, pos);
151 }
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900152 pos = BinaryFormat::skipFrequency(flags, pos);
Jean Chalard402b0572012-05-29 15:56:30 +0900153 pos = BinaryFormat::skipChildrenPosition(flags, pos);
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900154 pos = BinaryFormat::skipShortcuts(root, flags, pos);
155 return pos;
156}
157
Jean Chalardf1634c82012-05-02 19:05:27 +0900158void BigramDictionary::fillBigramAddressToFrequencyMapAndFilter(const int32_t *prevWord,
159 const int prevWordLength, std::map<int, int> *map, uint8_t *filter) {
160 memset(filter, 0, BIGRAM_FILTER_BYTE_SIZE);
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900161 const uint8_t* const root = DICT;
162 int pos = getBigramListPositionForWord(prevWord, prevWordLength);
163 if (0 == pos) return;
164
165 int bigramFlags;
166 do {
167 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
168 const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
169 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
170 &pos);
171 (*map)[bigramPos] = frequency;
Jean Chalardf1634c82012-05-02 19:05:27 +0900172 setInFilter(filter, bigramPos);
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900173 } while (0 != (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags));
174}
175
satok18c28f42010-12-02 18:11:54 +0900176bool BigramDictionary::checkFirstCharacter(unsigned short *word) {
177 // Checks whether this word starts with same character or neighboring characters of
178 // what user typed.
179
180 int *inputCodes = mInputCodes;
181 int maxAlt = MAX_ALTERNATIVES;
Tom Ouyangaeda8a72012-03-24 15:31:27 +0900182 const unsigned short firstBaseChar = toBaseLowerCase(*word);
satok18c28f42010-12-02 18:11:54 +0900183 while (maxAlt > 0) {
Tom Ouyangaeda8a72012-03-24 15:31:27 +0900184 if (toBaseLowerCase(*inputCodes) == firstBaseChar) {
satok18c28f42010-12-02 18:11:54 +0900185 return true;
186 }
187 inputCodes++;
188 maxAlt--;
189 }
190 return false;
191}
192
Tom Ouyang4d289d32012-04-26 23:50:21 -0700193bool BigramDictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2,
194 int length2) {
195 const uint8_t* const root = DICT;
196 int pos = getBigramListPositionForWord(word1, length1);
197 // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
198 if (0 == pos) return false;
199 int nextWordPos = BinaryFormat::getTerminalPosition(root, word2, length2);
200 if (NOT_VALID_WORD == nextWordPos) return false;
201 int bigramFlags;
202 do {
203 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
204 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
205 &pos);
206 if (bigramPos == nextWordPos) {
207 return true;
208 }
209 } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
210 return false;
211}
212
satok30088252010-12-01 21:22:15 +0900213// TODO: Move functions related to bigram to here
214} // namespace latinime