5db594abba
Change-Id: Ie11ff000675601acff5fbb00e9e9f48eb32c5071
88 lines
3.6 KiB
C++
88 lines
3.6 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#define LOG_TAG "LatinIME: dictionary.cpp"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "bigram_dictionary.h"
|
|
#include "binary_format.h"
|
|
#include "defines.h"
|
|
#include "dictionary.h"
|
|
#include "dic_traverse_wrapper.h"
|
|
#include "gesture_suggest.h"
|
|
#include "unigram_dictionary.h"
|
|
|
|
namespace latinime {
|
|
|
|
Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust)
|
|
: mDict(static_cast<unsigned char *>(dict)),
|
|
mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)),
|
|
mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
|
|
mUnigramDictionary(new UnigramDictionary(mOffsetDict, BinaryFormat::getFlags(mDict))),
|
|
mBigramDictionary(new BigramDictionary(mOffsetDict)),
|
|
mGestureSuggest(new GestureSuggest()) {
|
|
}
|
|
|
|
Dictionary::~Dictionary() {
|
|
delete mUnigramDictionary;
|
|
delete mBigramDictionary;
|
|
delete mGestureSuggest;
|
|
}
|
|
|
|
int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession,
|
|
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
|
|
int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint, bool isGesture,
|
|
bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices,
|
|
int *outputTypes) const {
|
|
int result = 0;
|
|
if (isGesture) {
|
|
DicTraverseWrapper::initDicTraverseSession(
|
|
traverseSession, this, prevWordCodePoints, prevWordLength);
|
|
result = mGestureSuggest->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->fillBigramAddressToFrequencyMapAndFilter(prevWordCodePoints,
|
|
prevWordLength, &bigramMap, bigramFilter);
|
|
result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
|
|
inputCodePoints, inputSize, &bigramMap, bigramFilter, useFullEditDistance, outWords,
|
|
frequencies, outputTypes);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize,
|
|
int *outWords, int *frequencies, int *outputTypes) const {
|
|
if (length <= 0) return 0;
|
|
return mBigramDictionary->getBigrams(word, length, inputCodePoints, inputSize, outWords,
|
|
frequencies, outputTypes);
|
|
}
|
|
|
|
int Dictionary::getFrequency(const int *word, int length) const {
|
|
return mUnigramDictionary->getFrequency(word, length);
|
|
}
|
|
|
|
bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const {
|
|
return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
|
|
}
|
|
} // namespace latinime
|