2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2012-07-25 08:51:43 +00:00
|
|
|
* Copyright (C) 2009, The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
#define LOG_TAG "LatinIME: dictionary.cpp"
|
|
|
|
|
2013-04-08 08:08:19 +00:00
|
|
|
#include "dictionary.h"
|
|
|
|
|
2013-02-23 00:34:01 +00:00
|
|
|
#include <map> // TODO: remove
|
2012-08-02 10:48:08 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "bigram_dictionary.h"
|
2012-02-27 10:48:47 +00:00
|
|
|
#include "binary_format.h"
|
2012-05-16 14:05:32 +00:00
|
|
|
#include "defines.h"
|
2012-08-09 14:23:08 +00:00
|
|
|
#include "dic_traverse_wrapper.h"
|
2013-04-08 08:08:19 +00:00
|
|
|
#include "suggest/core/suggest.h"
|
|
|
|
#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
|
|
|
|
#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
|
2012-08-02 10:48:08 +00:00
|
|
|
#include "unigram_dictionary.h"
|
2010-12-01 10:09:29 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
namespace latinime {
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust)
|
2012-08-24 05:51:15 +00:00
|
|
|
: mDict(static_cast<unsigned char *>(dict)),
|
2013-05-08 06:24:20 +00:00
|
|
|
mOffsetDict((static_cast<unsigned char *>(dict))
|
|
|
|
+ BinaryFormat::getHeaderSize(mDict, dictSize)),
|
2012-08-24 05:51:15 +00:00
|
|
|
mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
|
2013-05-08 06:24:20 +00:00
|
|
|
mUnigramDictionary(new UnigramDictionary(mOffsetDict,
|
|
|
|
BinaryFormat::getFlags(mDict, dictSize))),
|
2013-01-11 16:18:00 +00:00
|
|
|
mBigramDictionary(new BigramDictionary(mOffsetDict)),
|
2013-04-04 08:58:10 +00:00
|
|
|
mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
|
|
|
|
mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-12-08 08:05:39 +00:00
|
|
|
Dictionary::~Dictionary() {
|
2010-12-01 12:22:15 +00:00
|
|
|
delete mUnigramDictionary;
|
|
|
|
delete mBigramDictionary;
|
2012-12-20 09:32:44 +00:00
|
|
|
delete mGestureSuggest;
|
2013-02-23 00:34:01 +00:00
|
|
|
delete mTypingSuggest;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-12-02 05:53:24 +00:00
|
|
|
|
2012-08-08 12:23:25 +00:00
|
|
|
int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession,
|
2013-01-11 16:18:00 +00:00
|
|
|
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
|
|
|
|
int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint, bool isGesture,
|
2012-10-29 09:06:22 +00:00
|
|
|
bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices,
|
|
|
|
int *outputTypes) const {
|
2012-08-02 10:48:08 +00:00
|
|
|
int result = 0;
|
|
|
|
if (isGesture) {
|
2012-08-09 14:23:08 +00:00
|
|
|
DicTraverseWrapper::initDicTraverseSession(
|
2013-01-11 16:18:00 +00:00
|
|
|
traverseSession, this, prevWordCodePoints, prevWordLength);
|
|
|
|
result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
|
|
|
ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
|
|
|
|
frequencies, spaceIndices, outputTypes);
|
2012-08-06 02:20:54 +00:00
|
|
|
if (DEBUG_DICT) {
|
2013-01-11 16:18:00 +00:00
|
|
|
DUMP_RESULT(outWords, frequencies);
|
2012-08-06 02:20:54 +00:00
|
|
|
}
|
2012-08-02 10:48:08 +00:00
|
|
|
return result;
|
|
|
|
} else {
|
2013-02-23 00:34:01 +00:00
|
|
|
if (USE_SUGGEST_INTERFACE_FOR_TYPING) {
|
|
|
|
DicTraverseWrapper::initDicTraverseSession(
|
|
|
|
traverseSession, this, prevWordCodePoints, prevWordLength);
|
|
|
|
result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
|
|
|
ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
|
|
|
|
outWords, frequencies, spaceIndices, outputTypes);
|
|
|
|
if (DEBUG_DICT) {
|
|
|
|
DUMP_RESULT(outWords, frequencies);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
std::map<int, int> bigramMap;
|
|
|
|
uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
|
|
|
|
mBigramDictionary->fillBigramAddressToProbabilityMapAndFilter(prevWordCodePoints,
|
|
|
|
prevWordLength, &bigramMap, bigramFilter);
|
|
|
|
result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
|
|
|
|
inputCodePoints, inputSize, &bigramMap, bigramFilter, useFullEditDistance,
|
|
|
|
outWords, frequencies, outputTypes);
|
|
|
|
return result;
|
|
|
|
}
|
2012-08-02 10:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize,
|
2012-10-29 09:06:22 +00:00
|
|
|
int *outWords, int *frequencies, int *outputTypes) const {
|
2012-08-02 10:48:08 +00:00
|
|
|
if (length <= 0) return 0;
|
2013-01-11 16:18:00 +00:00
|
|
|
return mBigramDictionary->getBigrams(word, length, inputCodePoints, inputSize, outWords,
|
|
|
|
frequencies, outputTypes);
|
2012-08-02 10:48:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 04:08:31 +00:00
|
|
|
int Dictionary::getProbability(const int *word, int length) const {
|
|
|
|
return mUnigramDictionary->getProbability(word, length);
|
2010-12-02 05:53:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 10:54:30 +00:00
|
|
|
bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const {
|
2012-04-27 06:50:21 +00:00
|
|
|
return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
|
|
|
|
}
|
2013-04-03 00:23:13 +00:00
|
|
|
|
|
|
|
int Dictionary::getDictFlags() const {
|
|
|
|
return mUnigramDictionary->getDictFlags();
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
} // namespace latinime
|