dic_traverse_session.cpp revision 80ca7abea32a97acefcd8a8cb6145f0cdc8f0503
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
26void DicTraverseSession::init(const Dictionary *const dictionary, const int *prevWord,
27        int prevWordLength, const SuggestOptions *const suggestOptions) {
28    mDictionary = dictionary;
29    mMultiWordCostMultiplier = getDictionaryStructurePolicy()->getHeaderStructurePolicy()
30            ->getMultiWordCostMultiplier();
31    mSuggestOptions = suggestOptions;
32    if (!prevWord) {
33        mPrevWordPos = NOT_A_VALID_WORD_POS;
34        return;
35    }
36    // TODO: merge following similar calls to getTerminalPosition into one case-insensitive call.
37    mPrevWordPos = getDictionaryStructurePolicy()->getTerminalNodePositionOfWord(
38            prevWord, prevWordLength, false /* forceLowerCaseSearch */);
39    if (mPrevWordPos == NOT_A_VALID_WORD_POS) {
40        // Check bigrams for lower-cased previous word if original was not found. Useful for
41        // auto-capitalized words like "The [current_word]".
42        mPrevWordPos = getDictionaryStructurePolicy()->getTerminalNodePositionOfWord(
43                prevWord, prevWordLength, true /* forceLowerCaseSearch */);
44    }
45}
46
47void DicTraverseSession::setupForGetSuggestions(const ProximityInfo *pInfo,
48        const int *inputCodePoints, const int inputSize, const int *const inputXs,
49        const int *const inputYs, const int *const times, const int *const pointerIds,
50        const float maxSpatialDistance, const int maxPointerCount) {
51    mProximityInfo = pInfo;
52    mMaxPointerCount = maxPointerCount;
53    initializeProximityInfoStates(inputCodePoints, inputXs, inputYs, times, pointerIds, inputSize,
54            maxSpatialDistance, maxPointerCount);
55}
56
57const DictionaryStructureWithBufferPolicy *DicTraverseSession::getDictionaryStructurePolicy()
58        const {
59    return mDictionary->getDictionaryStructurePolicy();
60}
61
62void DicTraverseSession::resetCache(const int thresholdForNextActiveDicNodes, const int maxWords) {
63    mDicNodesCache.reset(thresholdForNextActiveDicNodes /* nextActiveSize */,
64            maxWords /* terminalSize */);
65    mMultiBigramMap.clear();
66    mPartiallyCommited = false;
67}
68
69void DicTraverseSession::initializeProximityInfoStates(const int *const inputCodePoints,
70        const int *const inputXs, const int *const inputYs, const int *const times,
71        const int *const pointerIds, const int inputSize, const float maxSpatialDistance,
72        const int maxPointerCount) {
73    ASSERT(1 <= maxPointerCount && maxPointerCount <= MAX_POINTER_COUNT_G);
74    mInputSize = 0;
75    for (int i = 0; i < maxPointerCount; ++i) {
76        mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(),
77                inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds,
78                maxPointerCount == MAX_POINTER_COUNT_G
79                /* TODO: this is a hack. fix proximity info state */);
80        mInputSize += mProximityInfoStates[i].size();
81    }
82}
83} // namespace latinime
84