dic_traverse_session.cpp revision 2fa3693c264a4c150ac307d9bb7f6f8f18cc4ffc
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "suggest/core/session/dic_traverse_session.h"
18
19#include "defines.h"
20#include "suggest/core/dictionary/dictionary.h"
21#include "suggest/core/policy/dictionary_header_structure_policy.h"
22#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
23
24namespace latinime {
25
26// 256K bytes threshold is heuristically used to distinguish dictionaries containing many unigrams
27// (e.g. main dictionary) from small dictionaries (e.g. contacts...)
28const int DicTraverseSession::DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION =
29        256 * 1024;
30
31void DicTraverseSession::init(const Dictionary *const dictionary, const int *prevWord,
32        int prevWordLength, const SuggestOptions *const suggestOptions) {
33    mDictionary = dictionary;
34    mMultiWordCostMultiplier = getDictionaryStructurePolicy()->getHeaderStructurePolicy()
35            ->getMultiWordCostMultiplier();
36    mSuggestOptions = suggestOptions;
37    if (!prevWord) {
38        mPrevWordPtNodePos = NOT_A_DICT_POS;
39        return;
40    }
41    // TODO: merge following similar calls to getTerminalPosition into one case-insensitive call.
42    mPrevWordPtNodePos = getDictionaryStructurePolicy()->getTerminalPtNodePositionOfWord(
43            prevWord, prevWordLength, false /* forceLowerCaseSearch */);
44    if (mPrevWordPtNodePos == NOT_A_DICT_POS) {
45        // Check bigrams for lower-cased previous word if original was not found. Useful for
46        // auto-capitalized words like "The [current_word]".
47        mPrevWordPtNodePos = getDictionaryStructurePolicy()->getTerminalPtNodePositionOfWord(
48                prevWord, prevWordLength, true /* forceLowerCaseSearch */);
49    }
50}
51
52void DicTraverseSession::setupForGetSuggestions(const ProximityInfo *pInfo,
53        const int *inputCodePoints, const int inputSize, const int *const inputXs,
54        const int *const inputYs, const int *const times, const int *const pointerIds,
55        const float maxSpatialDistance, const int maxPointerCount) {
56    mProximityInfo = pInfo;
57    mMaxPointerCount = maxPointerCount;
58    initializeProximityInfoStates(inputCodePoints, inputXs, inputYs, times, pointerIds, inputSize,
59            maxSpatialDistance, maxPointerCount);
60}
61
62const DictionaryStructureWithBufferPolicy *DicTraverseSession::getDictionaryStructurePolicy()
63        const {
64    return mDictionary->getDictionaryStructurePolicy();
65}
66
67void DicTraverseSession::resetCache(const int thresholdForNextActiveDicNodes, const int maxWords) {
68    mDicNodesCache.reset(thresholdForNextActiveDicNodes /* nextActiveSize */,
69            maxWords /* terminalSize */);
70    mMultiBigramMap.clear();
71    mPartiallyCommited = false;
72}
73
74void DicTraverseSession::initializeProximityInfoStates(const int *const inputCodePoints,
75        const int *const inputXs, const int *const inputYs, const int *const times,
76        const int *const pointerIds, const int inputSize, const float maxSpatialDistance,
77        const int maxPointerCount) {
78    ASSERT(1 <= maxPointerCount && maxPointerCount <= MAX_POINTER_COUNT_G);
79    mInputSize = 0;
80    for (int i = 0; i < maxPointerCount; ++i) {
81        mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(),
82                inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds,
83                maxPointerCount == MAX_POINTER_COUNT_G
84                /* TODO: this is a hack. fix proximity info state */);
85        mInputSize += mProximityInfoStates[i].size();
86    }
87}
88} // namespace latinime
89