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 "dictionary/interface/dictionary_header_structure_policy.h"
21 #include "dictionary/interface/dictionary_structure_with_buffer_policy.h"
22 #include "dictionary/property/ngram_context.h"
23 #include "suggest/core/dictionary/dictionary.h"
24 
25 namespace latinime {
26 
27 // 256K bytes threshold is heuristically used to distinguish dictionaries containing many unigrams
28 // (e.g. main dictionary) from small dictionaries (e.g. contacts...)
29 const int DicTraverseSession::DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION =
30         256 * 1024;
31 
init(const Dictionary * const dictionary,const NgramContext * const ngramContext,const SuggestOptions * const suggestOptions)32 void DicTraverseSession::init(const Dictionary *const dictionary,
33         const NgramContext *const ngramContext, const SuggestOptions *const suggestOptions) {
34     mDictionary = dictionary;
35     mMultiWordCostMultiplier = getDictionaryStructurePolicy()->getHeaderStructurePolicy()
36             ->getMultiWordCostMultiplier();
37     mSuggestOptions = suggestOptions;
38     mPrevWordIdCount = ngramContext->getPrevWordIds(getDictionaryStructurePolicy(),
39             &mPrevWordIdArray, true /* tryLowerCaseSearch */).size();
40 }
41 
setupForGetSuggestions(const ProximityInfo * pInfo,const int * inputCodePoints,const int inputSize,const int * const inputXs,const int * const inputYs,const int * const times,const int * const pointerIds,const float maxSpatialDistance,const int maxPointerCount)42 void DicTraverseSession::setupForGetSuggestions(const ProximityInfo *pInfo,
43         const int *inputCodePoints, const int inputSize, const int *const inputXs,
44         const int *const inputYs, const int *const times, const int *const pointerIds,
45         const float maxSpatialDistance, const int maxPointerCount) {
46     mProximityInfo = pInfo;
47     mMaxPointerCount = maxPointerCount;
48     initializeProximityInfoStates(inputCodePoints, inputXs, inputYs, times, pointerIds, inputSize,
49             maxSpatialDistance, maxPointerCount);
50 }
51 
getDictionaryStructurePolicy() const52 const DictionaryStructureWithBufferPolicy *DicTraverseSession::getDictionaryStructurePolicy()
53         const {
54     return mDictionary->getDictionaryStructurePolicy();
55 }
56 
resetCache(const int thresholdForNextActiveDicNodes,const int maxWords)57 void DicTraverseSession::resetCache(const int thresholdForNextActiveDicNodes, const int maxWords) {
58     mDicNodesCache.reset(thresholdForNextActiveDicNodes /* nextActiveSize */,
59             maxWords /* terminalSize */);
60     mMultiBigramMap.clear();
61 }
62 
initializeProximityInfoStates(const int * const inputCodePoints,const int * const inputXs,const int * const inputYs,const int * const times,const int * const pointerIds,const int inputSize,const float maxSpatialDistance,const int maxPointerCount)63 void DicTraverseSession::initializeProximityInfoStates(const int *const inputCodePoints,
64         const int *const inputXs, const int *const inputYs, const int *const times,
65         const int *const pointerIds, const int inputSize, const float maxSpatialDistance,
66         const int maxPointerCount) {
67     ASSERT(1 <= maxPointerCount && maxPointerCount <= MAX_POINTER_COUNT_G);
68     mInputSize = 0;
69     for (int i = 0; i < maxPointerCount; ++i) {
70         mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(),
71                 inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds,
72                 // Right now the line below is trying to figure out whether this is a gesture by
73                 // looking at the pointer count and assuming whatever is above the cutoff is
74                 // a gesture and whatever is below is type. This is hacky and incorrect, we
75                 // should pass the correct information instead.
76                 maxPointerCount == MAX_POINTER_COUNT_G,
77                 getDictionaryStructurePolicy()->getHeaderStructurePolicy()->getLocale());
78         mInputSize += mProximityInfoStates[i].size();
79     }
80 }
81 } // namespace latinime
82