satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_UNIGRAM_DICTIONARY_H |
| 18 | #define LATINIME_UNIGRAM_DICTIONARY_H |
| 19 | |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 20 | #include <map> |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 21 | #include <stdint.h> |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 22 | #include "correction.h" |
satok | 208268d | 2011-08-10 15:44:08 +0900 | [diff] [blame] | 23 | #include "correction_state.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 24 | #include "defines.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 25 | #include "proximity_info.h" |
satok | 16379df | 2011-12-12 20:53:22 +0900 | [diff] [blame] | 26 | #include "words_priority_queue.h" |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 27 | #include "words_priority_queue_pool.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 28 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 29 | namespace latinime { |
| 30 | |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 31 | class TerminalAttributes; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 32 | class UnigramDictionary { |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 33 | typedef struct { int first; int second; int replacement; } digraph_t; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 34 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 35 | public: |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 36 | // Mask and flags for children address type selection. |
| 37 | static const int MASK_GROUP_ADDRESS_TYPE = 0xC0; |
| 38 | static const int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00; |
| 39 | static const int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40; |
| 40 | static const int FLAG_GROUP_ADDRESS_TYPE_TWOBYTES = 0x80; |
| 41 | static const int FLAG_GROUP_ADDRESS_TYPE_THREEBYTES = 0xC0; |
| 42 | |
| 43 | // Flag for single/multiple char group |
| 44 | static const int FLAG_HAS_MULTIPLE_CHARS = 0x20; |
| 45 | |
| 46 | // Flag for terminal groups |
| 47 | static const int FLAG_IS_TERMINAL = 0x10; |
| 48 | |
Jean Chalard | c8c6585 | 2011-12-27 12:58:23 +0900 | [diff] [blame] | 49 | // Flag for shortcut targets presence |
| 50 | static const int FLAG_HAS_SHORTCUT_TARGETS = 0x08; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 51 | // Flag for bigram presence |
| 52 | static const int FLAG_HAS_BIGRAMS = 0x04; |
| 53 | |
| 54 | // Attribute (bigram/shortcut) related flags: |
| 55 | // Flag for presence of more attributes |
| 56 | static const int FLAG_ATTRIBUTE_HAS_NEXT = 0x80; |
| 57 | // Flag for sign of offset. If this flag is set, the offset value must be negated. |
| 58 | static const int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40; |
| 59 | |
| 60 | // Mask for attribute frequency, stored on 4 bits inside the flags byte. |
| 61 | static const int MASK_ATTRIBUTE_FREQUENCY = 0x0F; |
| 62 | |
| 63 | // Mask and flags for attribute address type selection. |
| 64 | static const int MASK_ATTRIBUTE_ADDRESS_TYPE = 0x30; |
| 65 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10; |
| 66 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20; |
| 67 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 68 | |
satok | 4d35598 | 2011-12-15 14:53:19 +0900 | [diff] [blame] | 69 | // Error tolerances |
| 70 | static const int DEFAULT_MAX_ERRORS = 2; |
| 71 | static const int MAX_ERRORS_FOR_TWO_WORDS = 1; |
| 72 | |
Satoshi Kataoka | 6cbe204 | 2012-05-30 17:28:34 +0900 | [diff] [blame] | 73 | static const int FLAG_MULTIPLE_SUGGEST_ABORT = 0; |
| 74 | static const int FLAG_MULTIPLE_SUGGEST_SKIP = 1; |
| 75 | static const int FLAG_MULTIPLE_SUGGEST_CONTINUE = 2; |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 76 | UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler, |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 77 | int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags); |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 78 | int getFrequency(const int32_t* const inWord, const int length) const; |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 79 | int getBigramPosition(int pos, unsigned short *word, int offset, int length) const; |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 80 | int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 81 | Correction *correction, const int *xcoordinates, const int *ycoordinates, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 82 | const int *codes, const int codesSize, const std::map<int, int> *bigramMap, |
| 83 | const uint8_t *bigramFilter, const bool useFullEditDistance, unsigned short *outWords, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 84 | int *frequencies) const; |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 85 | virtual ~UnigramDictionary(); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 86 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 87 | private: |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 88 | void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 89 | const int *ycoordinates, const int *codes, const int inputLength, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 90 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 91 | const bool useFullEditDistance, Correction *correction, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 92 | WordsPriorityQueuePool *queuePool) const; |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 93 | int getDigraphReplacement(const int *codes, const int i, const int codesSize, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 94 | const digraph_t* const digraphs, const unsigned int digraphsSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 95 | void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 96 | const int *xcoordinates, const int* ycoordinates, const int *codesBuffer, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 97 | int *xCoordinatesBuffer, int *yCoordinatesBuffer, const int codesBufferSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 98 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 99 | const bool useFullEditDistance, const int* codesSrc, const int codesRemain, |
| 100 | const int currentDepth, int* codesDest, Correction *correction, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 101 | WordsPriorityQueuePool* queuePool, const digraph_t* const digraphs, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 102 | const unsigned int digraphsSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 103 | void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 104 | const int *ycoordinates, const int *codes, const int codesSize, |
| 105 | Correction *correction) const; |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 106 | void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 107 | const int *ycoordinates, const int *codes, const std::map<int, int> *bigramMap, |
| 108 | const uint8_t *bigramFilter, const bool useFullEditDistance, const int inputLength, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 109 | Correction *correction, WordsPriorityQueuePool* queuePool) const; |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 110 | void getSuggestionCandidates( |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 111 | const bool useFullEditDistance, const int inputLength, |
| 112 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 113 | Correction *correction, WordsPriorityQueuePool* queuePool, const bool doAutoCompletion, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 114 | const int maxErrors, const int currentWordIndex) const; |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 115 | void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo, |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 116 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 117 | const bool useFullEditDistance, const int inputLength, |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 118 | Correction *correction, WordsPriorityQueuePool* queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 119 | const bool hasAutoCorrectionCandidate) const; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 120 | void onTerminal(const int freq, const TerminalAttributes& terminalAttributes, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 121 | Correction *correction, WordsPriorityQueuePool *queuePool, const bool addToMasterQueue, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 122 | const int currentWordIndex) const; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 123 | // Process a node by considering proximity, missing and excessive character |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 124 | bool processCurrentNode(const int initialPos, const std::map<int, int> *bigramMap, |
| 125 | const uint8_t *bigramFilter, Correction *correction, int *newCount, |
| 126 | int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 127 | const int currentWordIndex) const; |
Jean Chalard | bb15e77 | 2011-06-30 20:14:38 +0900 | [diff] [blame] | 128 | int getMostFrequentWordLike(const int startInputIndex, const int inputLength, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 129 | Correction *correction, unsigned short *word) const; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 130 | int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 131 | short unsigned int *outWord) const; |
Satoshi Kataoka | 6cbe204 | 2012-05-30 17:28:34 +0900 | [diff] [blame] | 132 | int getSubStringSuggestion( |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 133 | ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates, |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 134 | const int *codes, const bool useFullEditDistance, Correction *correction, |
| 135 | WordsPriorityQueuePool* queuePool, const int inputLength, |
| 136 | const bool hasAutoCorrectionCandidate, const int currentWordIndex, |
| 137 | const int inputWordStartPos, const int inputWordLength, |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 138 | const int outputWordStartPos, const bool isSpaceProximity, int *freqArray, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 139 | int *wordLengthArray, unsigned short* outputWord, int *outputWordLength) const; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 140 | void getMultiWordsSuggestionRec(ProximityInfo *proximityInfo, |
| 141 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
| 142 | const bool useFullEditDistance, const int inputLength, |
| 143 | Correction *correction, WordsPriorityQueuePool* queuePool, |
| 144 | const bool hasAutoCorrectionCandidate, const int startPos, const int startWordIndex, |
| 145 | const int outputWordLength, int *freqArray, int* wordLengthArray, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame^] | 146 | unsigned short* outputWord) const; |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 147 | |
| 148 | const uint8_t* const DICT_ROOT; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 149 | const int MAX_WORD_LENGTH; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 150 | const int MAX_WORDS; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 151 | const int TYPED_LETTER_MULTIPLIER; |
| 152 | const int FULL_WORD_MULTIPLIER; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 153 | const int ROOT_POS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 154 | const unsigned int BYTES_IN_ONE_CHAR; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 155 | const int MAX_DIGRAPH_SEARCH_DEPTH; |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 156 | const int FLAGS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 157 | |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 158 | static const digraph_t GERMAN_UMLAUT_DIGRAPHS[]; |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 159 | static const digraph_t FRENCH_LIGATURES_DIGRAPHS[]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 160 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 161 | } // namespace latinime |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 162 | |
| 163 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |