Home | History | Annotate | Download | only in core
      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 #ifndef LATINIME_SUGGEST_IMPL_H
     18 #define LATINIME_SUGGEST_IMPL_H
     19 
     20 #include "defines.h"
     21 #include "suggest/core/suggest_interface.h"
     22 #include "suggest/core/policy/suggest_policy.h"
     23 
     24 namespace latinime {
     25 
     26 // Naming convention
     27 // - Distance: "Weighted" edit distance -- used both for spatial and language.
     28 // - Compound Distance: Spatial Distance + Language Distance -- used for pruning and scoring
     29 // - Cost: delta/diff for Distance -- used both for spatial and language
     30 // - Length: "Non-weighted" -- used only for spatial
     31 // - Probability: "Non-weighted" -- used only for language
     32 // - Score: Final calibrated score based on the compound distance, which is sent to java as the
     33 //       priority of a suggested word
     34 
     35 class DicNode;
     36 class DicTraverseSession;
     37 class ProximityInfo;
     38 class Scoring;
     39 class Traversal;
     40 class Weighting;
     41 
     42 class Suggest : public SuggestInterface {
     43  public:
     44     AK_FORCE_INLINE Suggest(const SuggestPolicy *const suggestPolicy)
     45             : TRAVERSAL(suggestPolicy ? suggestPolicy->getTraversal() : 0),
     46               SCORING(suggestPolicy ? suggestPolicy->getScoring() : 0),
     47               WEIGHTING(suggestPolicy ? suggestPolicy->getWeighting() : 0) {}
     48     AK_FORCE_INLINE virtual ~Suggest() {}
     49     int getSuggestions(ProximityInfo *pInfo, void *traverseSession, int *inputXs, int *inputYs,
     50             int *times, int *pointerIds, int *inputCodePoints, int inputSize, int commitPoint,
     51             int *outWords, int *frequencies, int *outputIndices, int *outputTypes) const;
     52 
     53  private:
     54     DISALLOW_IMPLICIT_CONSTRUCTORS(Suggest);
     55     void createNextWordDicNode(DicTraverseSession *traverseSession, DicNode *dicNode,
     56             const bool spaceSubstitution) const;
     57     int outputSuggestions(DicTraverseSession *traverseSession, int *frequencies,
     58             int *outputCodePoints, int *outputIndices, int *outputTypes) const;
     59     void initializeSearch(DicTraverseSession *traverseSession, int commitPoint) const;
     60     void expandCurrentDicNodes(DicTraverseSession *traverseSession) const;
     61     void processTerminalDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     62     void processExpandedDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     63     void weightChildNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     64     float getAutocorrectScore(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     65     void generateFeatures(
     66             DicTraverseSession *traverseSession, DicNode *dicNode, float *features) const;
     67     void processDicNodeAsOmission(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     68     void processDicNodeAsDigraph(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     69     void processDicNodeAsTransposition(DicTraverseSession *traverseSession,
     70             DicNode *dicNode) const;
     71     void processDicNodeAsInsertion(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     72     void processDicNodeAsAdditionalProximityChar(DicTraverseSession *traverseSession,
     73             DicNode *dicNode, DicNode *childDicNode) const;
     74     void processDicNodeAsSubstitution(DicTraverseSession *traverseSession, DicNode *dicNode,
     75             DicNode *childDicNode) const;
     76     void processDicNodeAsMatch(DicTraverseSession *traverseSession,
     77             DicNode *childDicNode) const;
     78 
     79     // Inputs longer than this will autocorrect if the suggestion is multi-word
     80     static const int MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT;
     81     static const int MIN_CONTINUOUS_SUGGESTION_INPUT_SIZE;
     82 
     83     // Threshold for autocorrection classifier
     84     static const float AUTOCORRECT_CLASSIFICATION_THRESHOLD;
     85 
     86     const Traversal *const TRAVERSAL;
     87     const Scoring *const SCORING;
     88     const Weighting *const WEIGHTING;
     89 };
     90 } // namespace latinime
     91 #endif // LATINIME_SUGGEST_IMPL_H
     92