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,
     52             int *outputAutoCommitFirstWordConfidence) const;
     53 
     54  private:
     55     DISALLOW_IMPLICIT_CONSTRUCTORS(Suggest);
     56     void createNextWordDicNode(DicTraverseSession *traverseSession, DicNode *dicNode,
     57             const bool spaceSubstitution) const;
     58     int outputSuggestions(DicTraverseSession *traverseSession, int *frequencies,
     59             int *outputCodePoints, int *outputIndicesToPartialCommit, int *outputTypes,
     60             int *outputAutoCommitFirstWordConfidence) const;
     61     int computeFirstWordConfidence(const DicNode *const terminalDicNode) const;
     62     void initializeSearch(DicTraverseSession *traverseSession, int commitPoint) const;
     63     void expandCurrentDicNodes(DicTraverseSession *traverseSession) const;
     64     void processTerminalDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     65     void processExpandedDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     66     void weightChildNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     67     float getAutocorrectScore(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     68     void generateFeatures(
     69             DicTraverseSession *traverseSession, DicNode *dicNode, float *features) const;
     70     void processDicNodeAsOmission(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     71     void processDicNodeAsDigraph(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     72     void processDicNodeAsTransposition(DicTraverseSession *traverseSession,
     73             DicNode *dicNode) const;
     74     void processDicNodeAsInsertion(DicTraverseSession *traverseSession, DicNode *dicNode) const;
     75     void processDicNodeAsAdditionalProximityChar(DicTraverseSession *traverseSession,
     76             DicNode *dicNode, DicNode *childDicNode) const;
     77     void processDicNodeAsSubstitution(DicTraverseSession *traverseSession, DicNode *dicNode,
     78             DicNode *childDicNode) const;
     79     void processDicNodeAsMatch(DicTraverseSession *traverseSession,
     80             DicNode *childDicNode) const;
     81 
     82     // Inputs longer than this will autocorrect if the suggestion is multi-word
     83     static const int MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT;
     84     static const int MIN_CONTINUOUS_SUGGESTION_INPUT_SIZE;
     85 
     86     // Threshold for autocorrection classifier
     87     static const float AUTOCORRECT_CLASSIFICATION_THRESHOLD;
     88 
     89     const Traversal *const TRAVERSAL;
     90     const Scoring *const SCORING;
     91     const Weighting *const WEIGHTING;
     92 };
     93 } // namespace latinime
     94 #endif // LATINIME_SUGGEST_IMPL_H
     95