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 | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 42 | UnigramDictionary(const uint8_t *const streamStart, const unsigned int flags); |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 43 | int getFrequency(const int *const inWord, const int length) const; |
| 44 | int getBigramPosition(int pos, int *word, int offset, int length) const; |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 45 | int getSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 46 | const int *ycoordinates, const int *inputCodePoints, const int inputSize, |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 47 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 48 | const bool useFullEditDistance, int *outWords, int *frequencies, |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 49 | int *outputTypes) const; |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 50 | virtual ~UnigramDictionary(); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 51 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 52 | private: |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 53 | DISALLOW_IMPLICIT_CONSTRUCTORS(UnigramDictionary); |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 54 | void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 55 | const int *ycoordinates, const int *inputCodePoints, const int inputSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 56 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 57 | const bool useFullEditDistance, Correction *correction, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 58 | WordsPriorityQueuePool *queuePool) const; |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 59 | int getDigraphReplacement(const int *codes, const int i, const int inputSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 60 | const digraph_t *const digraphs, const unsigned int digraphsSize) const; |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 61 | void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, const int *xcoordinates, |
| 62 | const int *ycoordinates, const int *codesBuffer, int *xCoordinatesBuffer, |
| 63 | int *yCoordinatesBuffer, const int codesBufferSize, const std::map<int, int> *bigramMap, |
| 64 | const uint8_t *bigramFilter, const bool useFullEditDistance, const int *codesSrc, |
| 65 | const int codesRemain, const int currentDepth, int *codesDest, Correction *correction, |
| 66 | WordsPriorityQueuePool *queuePool, const digraph_t *const digraphs, |
| 67 | const unsigned int digraphsSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 68 | void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 69 | const int *ycoordinates, const int *codes, const int inputSize, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 70 | Correction *correction) const; |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 71 | void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 72 | const int *ycoordinates, const int *codes, const std::map<int, int> *bigramMap, |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 73 | const uint8_t *bigramFilter, const bool useFullEditDistance, const int inputSize, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 74 | Correction *correction, WordsPriorityQueuePool *queuePool) const; |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 75 | void getSuggestionCandidates( |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 76 | const bool useFullEditDistance, const int inputSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 77 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 78 | Correction *correction, WordsPriorityQueuePool *queuePool, const bool doAutoCompletion, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 79 | const int maxErrors, const int currentWordIndex) const; |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 80 | void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
| 81 | const int *ycoordinates, const int *codes, const bool useFullEditDistance, |
| 82 | const int inputSize, Correction *correction, WordsPriorityQueuePool *queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 83 | const bool hasAutoCorrectionCandidate) const; |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 84 | void onTerminal(const int freq, const TerminalAttributes &terminalAttributes, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 85 | Correction *correction, WordsPriorityQueuePool *queuePool, const bool addToMasterQueue, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 86 | const int currentWordIndex) const; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 87 | // Process a node by considering proximity, missing and excessive character |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 88 | bool processCurrentNode(const int initialPos, const std::map<int, int> *bigramMap, |
| 89 | const uint8_t *bigramFilter, Correction *correction, int *newCount, |
| 90 | int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool, |
Satoshi Kataoka | 6bc051d | 2012-06-08 19:52:19 +0900 | [diff] [blame] | 91 | const int currentWordIndex) const; |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 92 | int getMostFrequentWordLike(const int startInputIndex, const int inputSize, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 93 | Correction *correction, int *word) const; |
| 94 | int getMostFrequentWordLikeInner(const int *const inWord, const int inputSize, |
| 95 | int *outWord) const; |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 96 | int getSubStringSuggestion(ProximityInfo *proximityInfo, const int *xcoordinates, |
| 97 | const int *ycoordinates, const int *codes, const bool useFullEditDistance, |
| 98 | Correction *correction, WordsPriorityQueuePool *queuePool, const int inputSize, |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 99 | const bool hasAutoCorrectionCandidate, const int currentWordIndex, |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 100 | const int inputWordStartPos, const int inputWordLength, const int outputWordStartPos, |
| 101 | const bool isSpaceProximity, int *freqArray, int *wordLengthArray, int *outputWord, |
| 102 | int *outputWordLength) const; |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 103 | void getMultiWordsSuggestionRec(ProximityInfo *proximityInfo, const int *xcoordinates, |
| 104 | const int *ycoordinates, const int *codes, const bool useFullEditDistance, |
| 105 | const int inputSize, Correction *correction, WordsPriorityQueuePool *queuePool, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 106 | const bool hasAutoCorrectionCandidate, const int startPos, const int startWordIndex, |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 107 | const int outputWordLength, int *freqArray, int *wordLengthArray, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 108 | int *outputWord) const; |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 109 | |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 110 | const uint8_t *const DICT_ROOT; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 111 | const int ROOT_POS; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 112 | const int MAX_DIGRAPH_SEARCH_DEPTH; |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 113 | const int FLAGS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 114 | |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 115 | static const digraph_t GERMAN_UMLAUT_DIGRAPHS[]; |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 116 | static const digraph_t FRENCH_LIGATURES_DIGRAPHS[]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 117 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 118 | } // namespace latinime |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 119 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |