2010-12-01 12:22:15 +00:00
|
|
|
/*
|
2012-07-30 07:27:44 +00:00
|
|
|
* Copyright (C) 2010, The Android Open Source Project
|
2012-07-25 08:51:43 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2010-12-01 12:22:15 +00:00
|
|
|
|
2012-07-31 08:56:40 +00:00
|
|
|
#include <cstring>
|
2010-12-02 09:11:54 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
#define LOG_TAG "LatinIME: bigram_dictionary.cpp"
|
|
|
|
|
2010-12-01 12:22:15 +00:00
|
|
|
#include "bigram_dictionary.h"
|
2011-07-25 05:03:19 +00:00
|
|
|
#include "binary_format.h"
|
2012-05-07 11:14:00 +00:00
|
|
|
#include "bloom_filter.h"
|
2013-01-29 05:49:45 +00:00
|
|
|
#include "char_utils.h"
|
2012-05-16 14:05:32 +00:00
|
|
|
#include "defines.h"
|
2012-05-07 11:14:00 +00:00
|
|
|
#include "dictionary.h"
|
2010-12-01 12:22:15 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
BigramDictionary::BigramDictionary(const uint8_t *const streamStart) : DICT_ROOT(streamStart) {
|
2011-03-19 00:16:42 +00:00
|
|
|
if (DEBUG_DICT) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("BigramDictionary - constructor");
|
2011-03-19 00:16:42 +00:00
|
|
|
}
|
2010-12-02 09:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BigramDictionary::~BigramDictionary() {
|
|
|
|
}
|
|
|
|
|
2013-03-18 04:08:31 +00:00
|
|
|
void BigramDictionary::addWordBigram(int *word, int length, int probability, int *bigramProbability,
|
2012-10-29 09:06:22 +00:00
|
|
|
int *bigramCodePoints, int *outputTypes) const {
|
2010-12-02 09:11:54 +00:00
|
|
|
word[length] = 0;
|
2013-04-08 08:33:32 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2011-07-08 05:53:50 +00:00
|
|
|
#ifdef FLAG_DBG
|
2010-12-02 09:11:54 +00:00
|
|
|
char s[length + 1];
|
2012-10-29 09:06:22 +00:00
|
|
|
for (int i = 0; i <= length; i++) s[i] = static_cast<char>(word[i]);
|
2013-03-18 04:08:31 +00:00
|
|
|
AKLOGI("Bigram: Found word = %s, freq = %d :", s, probability);
|
2011-07-13 23:32:57 +00:00
|
|
|
#endif
|
2010-12-02 09:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the right insertion point
|
|
|
|
int insertAt = 0;
|
2013-01-11 09:59:01 +00:00
|
|
|
while (insertAt < MAX_RESULTS) {
|
2013-03-18 04:08:31 +00:00
|
|
|
if (probability > bigramProbability[insertAt] || (bigramProbability[insertAt] == probability
|
2013-01-29 05:49:45 +00:00
|
|
|
&& length < getCodePointCount(MAX_WORD_LENGTH,
|
2012-10-29 09:06:22 +00:00
|
|
|
bigramCodePoints + insertAt * MAX_WORD_LENGTH))) {
|
2010-12-02 09:11:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
insertAt++;
|
|
|
|
}
|
2013-04-08 08:33:32 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2013-01-11 09:59:01 +00:00
|
|
|
AKLOGI("Bigram: InsertAt -> %d MAX_RESULTS: %d", insertAt, MAX_RESULTS);
|
2011-03-19 00:16:42 +00:00
|
|
|
}
|
2013-01-11 09:59:01 +00:00
|
|
|
if (insertAt >= MAX_RESULTS) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-18 04:08:31 +00:00
|
|
|
memmove(bigramProbability + (insertAt + 1),
|
|
|
|
bigramProbability + insertAt,
|
|
|
|
(MAX_RESULTS - insertAt - 1) * sizeof(bigramProbability[0]));
|
|
|
|
bigramProbability[insertAt] = probability;
|
2013-01-11 09:59:01 +00:00
|
|
|
outputTypes[insertAt] = Dictionary::KIND_PREDICTION;
|
|
|
|
memmove(bigramCodePoints + (insertAt + 1) * MAX_WORD_LENGTH,
|
|
|
|
bigramCodePoints + insertAt * MAX_WORD_LENGTH,
|
|
|
|
(MAX_RESULTS - insertAt - 1) * sizeof(bigramCodePoints[0]) * MAX_WORD_LENGTH);
|
|
|
|
int *dest = bigramCodePoints + insertAt * MAX_WORD_LENGTH;
|
|
|
|
while (length--) {
|
|
|
|
*dest++ = *word++;
|
|
|
|
}
|
|
|
|
*dest = 0; // NULL terminate
|
2013-04-08 08:33:32 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2013-01-11 09:59:01 +00:00
|
|
|
AKLOGI("Bigram: Added word at %d", insertAt);
|
2010-12-02 09:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 05:03:19 +00:00
|
|
|
/* Parameters :
|
|
|
|
* prevWord: the word before, the one for which we need to look up bigrams.
|
|
|
|
* prevWordLength: its length.
|
2013-01-11 16:18:00 +00:00
|
|
|
* inputCodePoints: what user typed, in the same format as for UnigramDictionary::getSuggestions.
|
|
|
|
* inputSize: the size of the codes array.
|
2012-10-29 09:06:22 +00:00
|
|
|
* bigramCodePoints: an array for output, at the same format as outwords for getSuggestions.
|
2013-03-18 04:08:31 +00:00
|
|
|
* bigramProbability: an array to output frequencies.
|
2012-07-12 03:55:48 +00:00
|
|
|
* outputTypes: an array to output types.
|
2011-07-25 05:03:19 +00:00
|
|
|
* This method returns the number of bigrams this word has, for backward compatibility.
|
|
|
|
* Note: this is not the number of bigrams output in the array, which is the number of
|
|
|
|
* bigrams this word has WHOSE first letter also matches the letter the user typed.
|
|
|
|
* TODO: this may not be a sensible thing to do. It makes sense when the bigrams are
|
|
|
|
* used to match the first letter of the second word, but once the user has typed more
|
|
|
|
* and the bigrams are used to boost unigram result scores, it makes little sense to
|
|
|
|
* reduce their scope to the ones that match the first letter.
|
|
|
|
*/
|
2013-01-11 16:18:00 +00:00
|
|
|
int BigramDictionary::getBigrams(const int *prevWord, int prevWordLength, int *inputCodePoints,
|
2013-03-18 04:08:31 +00:00
|
|
|
int inputSize, int *bigramCodePoints, int *bigramProbability, int *outputTypes) const {
|
2011-07-25 05:03:19 +00:00
|
|
|
// TODO: remove unused arguments, and refrain from storing stuff in members of this class
|
|
|
|
// TODO: have "in" arguments before "out" ones, and make out args explicit in the name
|
2010-12-02 09:11:54 +00:00
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
const uint8_t *const root = DICT_ROOT;
|
2012-06-28 12:01:29 +00:00
|
|
|
int pos = getBigramListPositionForWord(prevWord, prevWordLength,
|
|
|
|
false /* forceLowerCaseSearch */);
|
2012-04-24 09:06:51 +00:00
|
|
|
// getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
|
2012-06-28 12:01:29 +00:00
|
|
|
if (0 == pos) {
|
|
|
|
// If no bigrams for this exact word, search again in lower case.
|
|
|
|
pos = getBigramListPositionForWord(prevWord, prevWordLength,
|
|
|
|
true /* forceLowerCaseSearch */);
|
|
|
|
}
|
|
|
|
// If still no bigrams, we really don't have them!
|
2012-04-17 07:02:35 +00:00
|
|
|
if (0 == pos) return 0;
|
2012-09-04 11:29:38 +00:00
|
|
|
uint8_t bigramFlags;
|
2011-07-25 05:03:19 +00:00
|
|
|
int bigramCount = 0;
|
|
|
|
do {
|
|
|
|
bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
|
2012-10-29 09:06:22 +00:00
|
|
|
int bigramBuffer[MAX_WORD_LENGTH];
|
2013-03-18 04:08:31 +00:00
|
|
|
int unigramProbability = 0;
|
2011-07-25 05:03:19 +00:00
|
|
|
const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
|
|
|
|
&pos);
|
|
|
|
const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH,
|
2013-03-18 04:08:31 +00:00
|
|
|
bigramBuffer, &unigramProbability);
|
2011-07-25 05:03:19 +00:00
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
// inputSize == 0 means we are trying to find bigram predictions.
|
|
|
|
if (inputSize < 1 || checkFirstCharacter(bigramBuffer, inputCodePoints)) {
|
2013-03-18 04:08:31 +00:00
|
|
|
const int bigramProbabilityTemp =
|
|
|
|
BinaryFormat::MASK_ATTRIBUTE_PROBABILITY & bigramFlags;
|
|
|
|
// Due to space constraints, the probability for bigrams is approximate - the lower the
|
|
|
|
// unigram probability, the worse the precision. The theoritical maximum error in
|
|
|
|
// resulting probability is 8 - although in the practice it's never bigger than 3 or 4
|
2012-05-29 07:50:25 +00:00
|
|
|
// in very bad cases. This means that sometimes, we'll see some bigrams interverted
|
|
|
|
// here, but it can't get too bad.
|
2013-03-18 04:08:31 +00:00
|
|
|
const int probability = BinaryFormat::computeProbabilityForBigram(
|
|
|
|
unigramProbability, bigramProbabilityTemp);
|
|
|
|
addWordBigram(bigramBuffer, length, probability, bigramProbability, bigramCodePoints,
|
2013-01-11 09:59:01 +00:00
|
|
|
outputTypes);
|
|
|
|
++bigramCount;
|
2010-12-02 09:11:54 +00:00
|
|
|
}
|
2012-07-31 14:45:32 +00:00
|
|
|
} while (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
|
2013-01-11 09:59:01 +00:00
|
|
|
return min(bigramCount, MAX_RESULTS);
|
2010-12-01 12:22:15 +00:00
|
|
|
}
|
2010-12-02 09:11:54 +00:00
|
|
|
|
2012-04-17 07:02:35 +00:00
|
|
|
// Returns a pointer to the start of the bigram list.
|
|
|
|
// If the word is not found or has no bigrams, this function returns 0.
|
2012-12-03 10:54:30 +00:00
|
|
|
int BigramDictionary::getBigramListPositionForWord(const int *prevWord, const int prevWordLength,
|
|
|
|
const bool forceLowerCaseSearch) const {
|
2012-05-02 07:00:24 +00:00
|
|
|
if (0 >= prevWordLength) return 0;
|
2013-01-11 16:18:00 +00:00
|
|
|
const uint8_t *const root = DICT_ROOT;
|
2012-06-28 12:01:29 +00:00
|
|
|
int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength,
|
|
|
|
forceLowerCaseSearch);
|
2012-04-17 02:38:44 +00:00
|
|
|
|
|
|
|
if (NOT_VALID_WORD == pos) return 0;
|
2012-09-04 11:29:38 +00:00
|
|
|
const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
|
2012-07-31 14:45:32 +00:00
|
|
|
if (0 == (flags & BinaryFormat::FLAG_HAS_BIGRAMS)) return 0;
|
|
|
|
if (0 == (flags & BinaryFormat::FLAG_HAS_MULTIPLE_CHARS)) {
|
2012-09-04 03:49:46 +00:00
|
|
|
BinaryFormat::getCodePointAndForwardPointer(root, &pos);
|
2012-04-17 02:38:44 +00:00
|
|
|
} else {
|
|
|
|
pos = BinaryFormat::skipOtherCharacters(root, pos);
|
|
|
|
}
|
2013-03-18 04:08:31 +00:00
|
|
|
pos = BinaryFormat::skipProbability(flags, pos);
|
2012-05-29 06:56:30 +00:00
|
|
|
pos = BinaryFormat::skipChildrenPosition(flags, pos);
|
2012-04-17 02:38:44 +00:00
|
|
|
pos = BinaryFormat::skipShortcuts(root, flags, pos);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2013-03-18 04:08:31 +00:00
|
|
|
void BigramDictionary::fillBigramAddressToProbabilityMapAndFilter(const int *prevWord,
|
2012-06-14 23:35:23 +00:00
|
|
|
const int prevWordLength, std::map<int, int> *map, uint8_t *filter) const {
|
2012-05-02 10:05:27 +00:00
|
|
|
memset(filter, 0, BIGRAM_FILTER_BYTE_SIZE);
|
2013-01-11 16:18:00 +00:00
|
|
|
const uint8_t *const root = DICT_ROOT;
|
2012-06-28 12:01:29 +00:00
|
|
|
int pos = getBigramListPositionForWord(prevWord, prevWordLength,
|
|
|
|
false /* forceLowerCaseSearch */);
|
|
|
|
if (0 == pos) {
|
|
|
|
// If no bigrams for this exact string, search again in lower case.
|
|
|
|
pos = getBigramListPositionForWord(prevWord, prevWordLength,
|
|
|
|
true /* forceLowerCaseSearch */);
|
|
|
|
}
|
2012-05-02 07:00:24 +00:00
|
|
|
if (0 == pos) return;
|
|
|
|
|
2012-09-04 11:29:38 +00:00
|
|
|
uint8_t bigramFlags;
|
2012-05-02 07:00:24 +00:00
|
|
|
do {
|
|
|
|
bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
|
2013-03-18 04:08:31 +00:00
|
|
|
const int probability = BinaryFormat::MASK_ATTRIBUTE_PROBABILITY & bigramFlags;
|
2012-05-02 07:00:24 +00:00
|
|
|
const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
|
|
|
|
&pos);
|
2013-03-18 04:08:31 +00:00
|
|
|
(*map)[bigramPos] = probability;
|
2012-05-02 10:05:27 +00:00
|
|
|
setInFilter(filter, bigramPos);
|
2013-04-26 10:58:14 +00:00
|
|
|
} while (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
|
2012-05-02 07:00:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
bool BigramDictionary::checkFirstCharacter(int *word, int *inputCodePoints) const {
|
2010-12-02 09:11:54 +00:00
|
|
|
// Checks whether this word starts with same character or neighboring characters of
|
|
|
|
// what user typed.
|
|
|
|
|
|
|
|
int maxAlt = MAX_ALTERNATIVES;
|
2013-01-11 16:18:00 +00:00
|
|
|
const int firstBaseLowerCodePoint = toBaseLowerCase(*word);
|
2010-12-02 09:11:54 +00:00
|
|
|
while (maxAlt > 0) {
|
2013-01-11 16:18:00 +00:00
|
|
|
if (toBaseLowerCase(*inputCodePoints) == firstBaseLowerCodePoint) {
|
2010-12-02 09:11:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-01-11 16:18:00 +00:00
|
|
|
inputCodePoints++;
|
2010-12-02 09:11:54 +00:00
|
|
|
maxAlt--;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-03 10:54:30 +00:00
|
|
|
bool BigramDictionary::isValidBigram(const int *word1, int length1, const int *word2,
|
2012-06-14 23:35:23 +00:00
|
|
|
int length2) const {
|
2013-01-11 16:18:00 +00:00
|
|
|
const uint8_t *const root = DICT_ROOT;
|
2012-06-28 12:01:29 +00:00
|
|
|
int pos = getBigramListPositionForWord(word1, length1, false /* forceLowerCaseSearch */);
|
2012-04-27 06:50:21 +00:00
|
|
|
// getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
|
|
|
|
if (0 == pos) return false;
|
2012-06-28 12:01:29 +00:00
|
|
|
int nextWordPos = BinaryFormat::getTerminalPosition(root, word2, length2,
|
|
|
|
false /* forceLowerCaseSearch */);
|
2012-04-27 06:50:21 +00:00
|
|
|
if (NOT_VALID_WORD == nextWordPos) return false;
|
2012-09-04 11:29:38 +00:00
|
|
|
uint8_t bigramFlags;
|
2012-04-27 06:50:21 +00:00
|
|
|
do {
|
|
|
|
bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
|
|
|
|
const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags,
|
|
|
|
&pos);
|
|
|
|
if (bigramPos == nextWordPos) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-31 14:45:32 +00:00
|
|
|
} while (BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags);
|
2012-04-27 06:50:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-01 12:22:15 +00:00
|
|
|
// TODO: Move functions related to bigram to here
|
|
|
|
} // namespace latinime
|