blob: eb4bf8d1a82289d8751fe537cb81e66cd4dfebbf [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];
Jean Chalarde3084592012-05-29 16:22:46 +0900120 int unigramFreq;
Jean Chalard588e2f22011-07-25 14:03:19 +0900121 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
122 &pos);
123 const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH,
Jean Chalarde3084592012-05-29 16:22:46 +0900124 bigramBuffer, &unigramFreq);
satok18c28f42010-12-02 18:11:54 +0900125
Jean Chalardad290d62012-02-15 19:22:26 -0800126 // codesSize == 0 means we are trying to find bigram predictions.
127 if (codesSize < 1 || checkFirstCharacter(bigramBuffer)) {
Jean Chalarde3084592012-05-29 16:22:46 +0900128 const int bigramFreq = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
Jean Chalard46fe49f2012-05-29 16:50:25 +0900129 // Due to space constraints, the frequency for bigrams is approximate - the lower the
130 // unigram frequency, the worse the precision. The theoritical maximum error in
131 // resulting frequency is 8 - although in the practice it's never bigger than 3 or 4
132 // in very bad cases. This means that sometimes, we'll see some bigrams interverted
133 // here, but it can't get too bad.
Jean Chalarde3084592012-05-29 16:22:46 +0900134 const int frequency =
135 BinaryFormat::computeFrequencyForBigram(unigramFreq, bigramFreq);
Jean Chalard9715cc42012-03-21 15:26:45 +0900136 if (addWordBigram(bigramBuffer, length, frequency)) {
137 ++bigramCount;
138 }
satok18c28f42010-12-02 18:11:54 +0900139 }
Tom Ouyang4d289d32012-04-26 23:50:21 -0700140 } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
Jean Chalard588e2f22011-07-25 14:03:19 +0900141 return bigramCount;
satok18c28f42010-12-02 18:11:54 +0900142}
143
Jean Chalardee396df2012-04-17 16:02:35 +0900144// Returns a pointer to the start of the bigram list.
145// If the word is not found or has no bigrams, this function returns 0.
Jean Chalard351864b2012-04-24 18:06:51 +0900146int BigramDictionary::getBigramListPositionForWord(const int32_t *prevWord,
147 const int prevWordLength) {
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900148 if (0 >= prevWordLength) return 0;
Jean Chalard351864b2012-04-24 18:06:51 +0900149 const uint8_t* const root = DICT;
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900150 int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
151
152 if (NOT_VALID_WORD == pos) return 0;
153 const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
154 if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
155 if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
156 BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
157 } else {
158 pos = BinaryFormat::skipOtherCharacters(root, pos);
159 }
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900160 pos = BinaryFormat::skipFrequency(flags, pos);
Jean Chalard402b0572012-05-29 15:56:30 +0900161 pos = BinaryFormat::skipChildrenPosition(flags, pos);
Jean Chalard9c2a96a2012-04-17 11:38:44 +0900162 pos = BinaryFormat::skipShortcuts(root, flags, pos);
163 return pos;
164}
165
Jean Chalardf1634c82012-05-02 19:05:27 +0900166void BigramDictionary::fillBigramAddressToFrequencyMapAndFilter(const int32_t *prevWord,
167 const int prevWordLength, std::map<int, int> *map, uint8_t *filter) {
168 memset(filter, 0, BIGRAM_FILTER_BYTE_SIZE);
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900169 const uint8_t* const root = DICT;
170 int pos = getBigramListPositionForWord(prevWord, prevWordLength);
171 if (0 == pos) return;
172
173 int bigramFlags;
174 do {
175 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
176 const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags;
177 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
178 &pos);
179 (*map)[bigramPos] = frequency;
Jean Chalardf1634c82012-05-02 19:05:27 +0900180 setInFilter(filter, bigramPos);
Jean Chalard1ff8dc42012-05-02 16:00:24 +0900181 } while (0 != (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags));
182}
183
satok18c28f42010-12-02 18:11:54 +0900184bool BigramDictionary::checkFirstCharacter(unsigned short *word) {
185 // Checks whether this word starts with same character or neighboring characters of
186 // what user typed.
187
188 int *inputCodes = mInputCodes;
189 int maxAlt = MAX_ALTERNATIVES;
Tom Ouyangaeda8a72012-03-24 15:31:27 +0900190 const unsigned short firstBaseChar = toBaseLowerCase(*word);
satok18c28f42010-12-02 18:11:54 +0900191 while (maxAlt > 0) {
Tom Ouyangaeda8a72012-03-24 15:31:27 +0900192 if (toBaseLowerCase(*inputCodes) == firstBaseChar) {
satok18c28f42010-12-02 18:11:54 +0900193 return true;
194 }
195 inputCodes++;
196 maxAlt--;
197 }
198 return false;
199}
200
Tom Ouyang4d289d32012-04-26 23:50:21 -0700201bool BigramDictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2,
202 int length2) {
203 const uint8_t* const root = DICT;
204 int pos = getBigramListPositionForWord(word1, length1);
205 // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
206 if (0 == pos) return false;
207 int nextWordPos = BinaryFormat::getTerminalPosition(root, word2, length2);
208 if (NOT_VALID_WORD == nextWordPos) return false;
209 int bigramFlags;
210 do {
211 bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
212 const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
213 &pos);
214 if (bigramPos == nextWordPos) {
215 return true;
216 }
217 } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
218 return false;
219}
220
satok30088252010-12-01 21:22:15 +0900221// TODO: Move functions related to bigram to here
222} // namespace latinime