13107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka/*
23107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * Copyright (C) 2012 The Android Open Source Project
33107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka *
43107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * Licensed under the Apache License, Version 2.0 (the "License");
53107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * you may not use this file except in compliance with the License.
63107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * You may obtain a copy of the License at
73107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka *
83107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka *      http://www.apache.org/licenses/LICENSE-2.0
93107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka *
103107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * Unless required by applicable law or agreed to in writing, software
113107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * distributed under the License is distributed on an "AS IS" BASIS,
123107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * See the License for the specific language governing permissions and
143107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka * limitations under the License.
153107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka */
163107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
17b68e73448104714e8f12f89a1e00fb10b5fd14c4Ken Wakasa#include "suggest/core/session/dic_traverse_session.h"
18b68e73448104714e8f12f89a1e00fb10b5fd14c4Ken Wakasa
193107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka#include "defines.h"
20a65c267b1f1207e54c6f821148c600e3899b7f9cKen Wakasa#include "suggest/core/dictionary/dictionary.h"
2176e579c7caf2ef04f440be21c27377fe0b4150ffKeisuke Kuroyanagi#include "suggest/core/policy/dictionary_header_structure_policy.h"
22d81654cd61bd10f7cb56bfa4c89b34e9cfb18598Keisuke Kuroyanagi#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
23b87fffb8be3dc6a79e49890a7700704d7fee616bKeisuke Kuroyanagi#include "suggest/core/session/prev_words_info.h"
243107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
253107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataokanamespace latinime {
263107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
274c2767857a02c9cf18a9579aa0391fd09b3fe411Keisuke Kuroyanagi// 256K bytes threshold is heuristically used to distinguish dictionaries containing many unigrams
284c2767857a02c9cf18a9579aa0391fd09b3fe411Keisuke Kuroyanagi// (e.g. main dictionary) from small dictionaries (e.g. contacts...)
294c2767857a02c9cf18a9579aa0391fd09b3fe411Keisuke Kuroyanagiconst int DicTraverseSession::DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION =
304c2767857a02c9cf18a9579aa0391fd09b3fe411Keisuke Kuroyanagi        256 * 1024;
314c2767857a02c9cf18a9579aa0391fd09b3fe411Keisuke Kuroyanagi
32b87fffb8be3dc6a79e49890a7700704d7fee616bKeisuke Kuroyanagivoid DicTraverseSession::init(const Dictionary *const dictionary,
33b87fffb8be3dc6a79e49890a7700704d7fee616bKeisuke Kuroyanagi        const PrevWordsInfo *const prevWordsInfo, const SuggestOptions *const suggestOptions) {
343107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    mDictionary = dictionary;
3576e579c7caf2ef04f440be21c27377fe0b4150ffKeisuke Kuroyanagi    mMultiWordCostMultiplier = getDictionaryStructurePolicy()->getHeaderStructurePolicy()
36d81654cd61bd10f7cb56bfa4c89b34e9cfb18598Keisuke Kuroyanagi            ->getMultiWordCostMultiplier();
37338ef65077f43d6a35665a5c8eb6a44928332547Keisuke Kuroyanagi    mSuggestOptions = suggestOptions;
3845d1a936a7a318286c4404951db1bd825e25cc7cKeisuke Kuroyanagi    prevWordsInfo->getPrevWordsTerminalPtNodePos(
399f8c9a0161924f515c5ff9617db2317cdc1d01e2Keisuke Kuroyanagi            getDictionaryStructurePolicy(), mPrevWordsPtNodePos, true /* tryLowerCaseSearch */);
403107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka}
413107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
423107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataokavoid DicTraverseSession::setupForGetSuggestions(const ProximityInfo *pInfo,
433107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const int *inputCodePoints, const int inputSize, const int *const inputXs,
443107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const int *const inputYs, const int *const times, const int *const pointerIds,
453107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const float maxSpatialDistance, const int maxPointerCount) {
463107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    mProximityInfo = pInfo;
473107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    mMaxPointerCount = maxPointerCount;
483107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    initializeProximityInfoStates(inputCodePoints, inputXs, inputYs, times, pointerIds, inputSize,
493107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka            maxSpatialDistance, maxPointerCount);
503107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka}
513107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
52d81654cd61bd10f7cb56bfa4c89b34e9cfb18598Keisuke Kuroyanagiconst DictionaryStructureWithBufferPolicy *DicTraverseSession::getDictionaryStructurePolicy()
53d81654cd61bd10f7cb56bfa4c89b34e9cfb18598Keisuke Kuroyanagi        const {
54d81654cd61bd10f7cb56bfa4c89b34e9cfb18598Keisuke Kuroyanagi    return mDictionary->getDictionaryStructurePolicy();
553107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka}
563107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
5780ca7abea32a97acefcd8a8cb6145f0cdc8f0503Keisuke Kuroyanagivoid DicTraverseSession::resetCache(const int thresholdForNextActiveDicNodes, const int maxWords) {
5880ca7abea32a97acefcd8a8cb6145f0cdc8f0503Keisuke Kuroyanagi    mDicNodesCache.reset(thresholdForNextActiveDicNodes /* nextActiveSize */,
5980ca7abea32a97acefcd8a8cb6145f0cdc8f0503Keisuke Kuroyanagi            maxWords /* terminalSize */);
609559dd2e30de288a9ff7069bfc59f8500b949a88Tom Ouyang    mMultiBigramMap.clear();
613107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka}
623107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka
633107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataokavoid DicTraverseSession::initializeProximityInfoStates(const int *const inputCodePoints,
643107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const int *const inputXs, const int *const inputYs, const int *const times,
653107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const int *const pointerIds, const int inputSize, const float maxSpatialDistance,
663107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        const int maxPointerCount) {
673107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    ASSERT(1 <= maxPointerCount && maxPointerCount <= MAX_POINTER_COUNT_G);
683107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    mInputSize = 0;
693107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    for (int i = 0; i < maxPointerCount; ++i) {
703107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(),
713107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka                inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds,
723107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka                maxPointerCount == MAX_POINTER_COUNT_G
733107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka                /* TODO: this is a hack. fix proximity info state */);
743107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka        mInputSize += mProximityInfoStates[i].size();
753107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka    }
763107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka}
773107b467c91c471ce4e00c5d8de559f7b0da2cd6Satoshi Kataoka} // namespace latinime
78