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 | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 22 | #include "defines.h" |
| 23 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 24 | namespace latinime { |
| 25 | |
Ken Wakasa | f1008c5 | 2012-07-31 17:56:40 +0900 | [diff] [blame] | 26 | class Correction; |
| 27 | class ProximityInfo; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 28 | class TerminalAttributes; |
Ken Wakasa | f1008c5 | 2012-07-31 17:56:40 +0900 | [diff] [blame] | 29 | class WordsPriorityQueuePool; |
| 30 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 31 | class UnigramDictionary { |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 32 | typedef struct { int first; int second; int replacement; } digraph_t; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 33 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 34 | public: |
satok | 4d35598 | 2011-12-15 14:53:19 +0900 | [diff] [blame] | 35 | // Error tolerances |
| 36 | static const int DEFAULT_MAX_ERRORS = 2; |
| 37 | static const int MAX_ERRORS_FOR_TWO_WORDS = 1; |
| 38 | |
Satoshi Kataoka | 6cbe204 | 2012-05-30 17:28:34 +0900 | [diff] [blame] | 39 | static const int FLAG_MULTIPLE_SUGGEST_ABORT = 0; |
| 40 | static const int FLAG_MULTIPLE_SUGGEST_SKIP = 1; |
| 41 | static const int FLAG_MULTIPLE_SUGGEST_CONTINUE = 2; |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 42 | UnigramDictionary(const uint8_t *const streamStart, int typedLetterMultipler, |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 43 | int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags); |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 44 | int getFrequency(const int32_t *const inWord, const int length) const; |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 45 | int getBigramPosition(int pos, unsigned short *word, int offset, int length) const; |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 46 | int getSuggestions( |
| 47 | ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 48 | const int *codes, const int codesSize, const std::map<int, int> *bigramMap, |
| 49 | const uint8_t *bigramFilter, const bool useFullEditDistance, unsigned short *outWords, |
Jean Chalard | 6931df9 | 2012-07-12 12:55:48 +0900 | [diff] [blame] | 50 | int *frequencies, int *outputTypes) const; |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 51 | virtual ~UnigramDictionary(); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 52 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 53 | private: |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 54 | DISALLOW_IMPLICIT_CONSTRUCTORS(UnigramDictionary); |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 55 | void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 56 | const int *ycoordinates, const int *codes, const int inputSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 57 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 58 | const bool useFullEditDistance, Correction *correction, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 59 | WordsPriorityQueuePool *queuePool) const; |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 60 | int getDigraphReplacement(const int *codes, const int i, const int codesSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 61 | const digraph_t *const digraphs, const unsigned int digraphsSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 62 | void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 63 | const int *xcoordinates, const int *ycoordinates, const int *codesBuffer, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 64 | int *xCoordinatesBuffer, int *yCoordinatesBuffer, const int codesBufferSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 65 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 66 | const bool useFullEditDistance, const int *codesSrc, const int codesRemain, |
| 67 | const int currentDepth, int *codesDest, Correction *correction, |
| 68 | WordsPriorityQueuePool *queuePool, const digraph_t *const digraphs, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 69 | const unsigned int digraphsSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 70 | void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 71 | const int *ycoordinates, const int *codes, const int codesSize, |
| 72 | Correction *correction) const; |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 73 | void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 74 | const int *ycoordinates, const int *codes, const std::map<int, int> *bigramMap, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 75 | const uint8_t *bigramFilter, const bool useFullEditDistance, const int inputSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 76 | Correction *correction, WordsPriorityQueuePool *queuePool) const; |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 77 | void getSuggestionCandidates( |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 78 | const bool useFullEditDistance, const int inputSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 79 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 80 | Correction *correction, WordsPriorityQueuePool *queuePool, const bool doAutoCompletion, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 81 | const int maxErrors, const int currentWordIndex) const; |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 82 | void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo, |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 83 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 84 | const bool useFullEditDistance, const int inputSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 85 | Correction *correction, WordsPriorityQueuePool *queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 86 | const bool hasAutoCorrectionCandidate) const; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 87 | void onTerminal(const int freq, const TerminalAttributes& terminalAttributes, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 88 | Correction *correction, WordsPriorityQueuePool *queuePool, const bool addToMasterQueue, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 89 | const int currentWordIndex) const; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 90 | // Process a node by considering proximity, missing and excessive character |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 91 | bool processCurrentNode(const int initialPos, const std::map<int, int> *bigramMap, |
| 92 | const uint8_t *bigramFilter, Correction *correction, int *newCount, |
| 93 | int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 94 | const int currentWordIndex) const; |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 95 | int getMostFrequentWordLike(const int startInputIndex, const int inputSize, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 96 | Correction *correction, unsigned short *word) const; |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 97 | int getMostFrequentWordLikeInner(const uint16_t *const inWord, const int length, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 98 | short unsigned int *outWord) const; |
Satoshi Kataoka | 6cbe204 | 2012-05-30 17:28:34 +0900 | [diff] [blame] | 99 | int getSubStringSuggestion( |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 100 | ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates, |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 101 | const int *codes, const bool useFullEditDistance, Correction *correction, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 102 | WordsPriorityQueuePool *queuePool, const int inputSize, |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 103 | const bool hasAutoCorrectionCandidate, const int currentWordIndex, |
| 104 | const int inputWordStartPos, const int inputWordLength, |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 105 | const int outputWordStartPos, const bool isSpaceProximity, int *freqArray, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 106 | int *wordLengthArray, unsigned short *outputWord, int *outputWordLength) const; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 107 | void getMultiWordsSuggestionRec(ProximityInfo *proximityInfo, |
| 108 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 109 | const bool useFullEditDistance, const int inputSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 110 | Correction *correction, WordsPriorityQueuePool *queuePool, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 111 | const bool hasAutoCorrectionCandidate, const int startPos, const int startWordIndex, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 112 | const int outputWordLength, int *freqArray, int *wordLengthArray, |
| 113 | unsigned short *outputWord) const; |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 114 | |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 115 | const uint8_t *const DICT_ROOT; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 116 | const int MAX_WORD_LENGTH; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 117 | const int MAX_WORDS; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 118 | const int TYPED_LETTER_MULTIPLIER; |
| 119 | const int FULL_WORD_MULTIPLIER; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 120 | const int ROOT_POS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 121 | const unsigned int BYTES_IN_ONE_CHAR; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 122 | const int MAX_DIGRAPH_SEARCH_DEPTH; |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 123 | const int FLAGS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 124 | |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 125 | static const digraph_t GERMAN_UMLAUT_DIGRAPHS[]; |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 126 | static const digraph_t FRENCH_LIGATURES_DIGRAPHS[]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 127 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 128 | } // namespace latinime |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 129 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |