Home | History | Annotate | Download | only in policy
      1 /*
      2  * Copyright (C) 2013, 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_DICTIONARY_STRUCTURE_POLICY_H
     18 #define LATINIME_DICTIONARY_STRUCTURE_POLICY_H
     19 
     20 #include "defines.h"
     21 
     22 namespace latinime {
     23 
     24 class DicNode;
     25 class DicNodeVector;
     26 class DictionaryBigramsStructurePolicy;
     27 class DictionaryHeaderStructurePolicy;
     28 class DictionaryShortcutsStructurePolicy;
     29 
     30 /*
     31  * This class abstracts structure of dictionaries.
     32  * Implement this policy to support additional dictionaries.
     33  */
     34 class DictionaryStructureWithBufferPolicy {
     35  public:
     36     virtual ~DictionaryStructureWithBufferPolicy() {}
     37 
     38     virtual int getRootPosition() const = 0;
     39 
     40     virtual void createAndGetAllChildNodes(const DicNode *const dicNode,
     41             DicNodeVector *const childDicNodes) const = 0;
     42 
     43     virtual int getCodePointsAndProbabilityAndReturnCodePointCount(
     44             const int nodePos, const int maxCodePointCount, int *const outCodePoints,
     45             int *const outUnigramProbability) const = 0;
     46 
     47     virtual int getTerminalNodePositionOfWord(const int *const inWord,
     48             const int length, const bool forceLowerCaseSearch) const = 0;
     49 
     50     virtual int getProbability(const int unigramProbability,
     51             const int bigramProbability) const = 0;
     52 
     53     virtual int getUnigramProbabilityOfPtNode(const int nodePos) const = 0;
     54 
     55     virtual int getShortcutPositionOfPtNode(const int nodePos) const = 0;
     56 
     57     virtual int getBigramsPositionOfPtNode(const int nodePos) const = 0;
     58 
     59     virtual const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const = 0;
     60 
     61     virtual const DictionaryBigramsStructurePolicy *getBigramsStructurePolicy() const = 0;
     62 
     63     virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;
     64 
     65     // Returns whether the update was success or not.
     66     virtual bool addUnigramWord(const int *const word, const int length,
     67             const int probability) = 0;
     68 
     69     // Returns whether the update was success or not.
     70     virtual bool addBigramWords(const int *const word0, const int length0, const int *const word1,
     71             const int length1, const int probability) = 0;
     72 
     73     // Returns whether the update was success or not.
     74     virtual bool removeBigramWords(const int *const word0, const int length0,
     75             const int *const word1, const int length1) = 0;
     76 
     77     virtual void flush(const char *const filePath) = 0;
     78 
     79     virtual void flushWithGC(const char *const filePath) = 0;
     80 
     81     virtual bool needsToRunGC(const bool mindsBlockByGC) const = 0;
     82 
     83     // Currently, this method is used only for testing. You may want to consider creating new
     84     // dedicated method instead of this if you want to use this in the production.
     85     virtual void getProperty(const char *const query, char *const outResult,
     86             const int maxResultLength) = 0;
     87 
     88  protected:
     89     DictionaryStructureWithBufferPolicy() {}
     90 
     91  private:
     92     DISALLOW_COPY_AND_ASSIGN(DictionaryStructureWithBufferPolicy);
     93 };
     94 } // namespace latinime
     95 #endif /* LATINIME_DICTIONARY_STRUCTURE_POLICY_H */
     96