blob: f6aef2bfdd2716679e9b137ef633167328304b72 [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
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 Chalard8950ce62012-05-07 16:28:30 +090020#include <map>
Jean Chalard293ece02011-06-16 20:55:16 +090021#include <stdint.h>
satokcfca3c62011-08-10 14:30:10 +090022#include "correction.h"
satok208268d2011-08-10 15:44:08 +090023#include "correction_state.h"
satoke808e432010-12-02 14:53:24 +090024#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090025#include "proximity_info.h"
satok16379df2011-12-12 20:53:22 +090026#include "words_priority_queue.h"
satoka7e5a5a2011-12-15 16:49:12 +090027#include "words_priority_queue_pool.h"
satoke808e432010-12-02 14:53:24 +090028
satok30088252010-12-01 21:22:15 +090029namespace latinime {
30
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090031class TerminalAttributes;
satok30088252010-12-01 21:22:15 +090032class UnigramDictionary {
Jean Chalardd3043382012-03-21 18:38:25 +090033 typedef struct { int first; int second; int replacement; } digraph_t;
Jean Chalard6c300612012-03-06 19:54:03 +090034
Ken Wakasae12e9b52012-01-06 12:24:38 +090035 public:
Jean Chalard1059f272011-06-28 20:45:05 +090036 // 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 Chalardc8c65852011-12-27 12:58:23 +090049 // Flag for shortcut targets presence
50 static const int FLAG_HAS_SHORTCUT_TARGETS = 0x08;
Jean Chalard1059f272011-06-28 20:45:05 +090051 // 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 Chalard1059f272011-06-28 20:45:05 +090068
satok4d355982011-12-15 14:53:19 +090069 // Error tolerances
70 static const int DEFAULT_MAX_ERRORS = 2;
71 static const int MAX_ERRORS_FOR_TWO_WORDS = 1;
72
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +090073 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;
Ken Wakasa0bbb9172012-07-25 17:51:43 +090076 UnigramDictionary(const uint8_t *const streamStart, int typedLetterMultipler,
Jean Chalardcd274b12012-04-06 18:26:00 +090077 int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags);
Ken Wakasa0bbb9172012-07-25 17:51:43 +090078 int getFrequency(const int32_t *const inWord, const int length) const;
Jean Chalard581335c2011-06-17 12:45:17 +090079 int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
satokb1ed1d42012-06-14 16:35:23 -070080 int getSuggestions(
81 ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates,
Jean Chalard8950ce62012-05-07 16:28:30 +090082 const int *codes, const int codesSize, const std::map<int, int> *bigramMap,
83 const uint8_t *bigramFilter, const bool useFullEditDistance, unsigned short *outWords,
Jean Chalard6931df92012-07-12 12:55:48 +090084 int *frequencies, int *outputTypes) const;
satok2df30602011-07-15 13:49:00 +090085 virtual ~UnigramDictionary();
satok30088252010-12-01 21:22:15 +090086
Ken Wakasae12e9b52012-01-06 12:24:38 +090087 private:
satok1bc038c2012-06-14 11:25:50 -070088 DISALLOW_IMPLICIT_CONSTRUCTORS(UnigramDictionary);
satok1d7eaf82011-07-13 10:32:02 +090089 void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
satok1147c7b2011-12-14 15:04:58 +090090 const int *ycoordinates, const int *codes, const int inputLength,
Jean Chalard8950ce62012-05-07 16:28:30 +090091 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
92 const bool useFullEditDistance, Correction *correction,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +090093 WordsPriorityQueuePool *queuePool) const;
Jean Chalardd3043382012-03-21 18:38:25 +090094 int getDigraphReplacement(const int *codes, const int i, const int codesSize,
Ken Wakasa0bbb9172012-07-25 17:51:43 +090095 const digraph_t *const digraphs, const unsigned int digraphsSize) const;
satok1d7eaf82011-07-13 10:32:02 +090096 void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
Ken Wakasa0bbb9172012-07-25 17:51:43 +090097 const int *xcoordinates, const int *ycoordinates, const int *codesBuffer,
Jean Chalard4d9b2022012-04-23 19:25:28 +090098 int *xCoordinatesBuffer, int *yCoordinatesBuffer, const int codesBufferSize,
Jean Chalard8950ce62012-05-07 16:28:30 +090099 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900100 const bool useFullEditDistance, const int *codesSrc, const int codesRemain,
101 const int currentDepth, int *codesDest, Correction *correction,
102 WordsPriorityQueuePool *queuePool, const digraph_t *const digraphs,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900103 const unsigned int digraphsSize) const;
satok1d7eaf82011-07-13 10:32:02 +0900104 void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900105 const int *ycoordinates, const int *codes, const int codesSize,
106 Correction *correction) const;
satok744dab62011-12-15 22:29:05 +0900107 void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
Jean Chalard8950ce62012-05-07 16:28:30 +0900108 const int *ycoordinates, const int *codes, const std::map<int, int> *bigramMap,
109 const uint8_t *bigramFilter, const bool useFullEditDistance, const int inputLength,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900110 Correction *correction, WordsPriorityQueuePool *queuePool) const;
Jean Chalard4d9b2022012-04-23 19:25:28 +0900111 void getSuggestionCandidates(
Jean Chalard8950ce62012-05-07 16:28:30 +0900112 const bool useFullEditDistance, const int inputLength,
113 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900114 Correction *correction, WordsPriorityQueuePool *queuePool, const bool doAutoCompletion,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900115 const int maxErrors, const int currentWordIndex) const;
satoka85f4922012-01-30 18:18:30 +0900116 void getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo,
satok744dab62011-12-15 22:29:05 +0900117 const int *xcoordinates, const int *ycoordinates, const int *codes,
satok1f6b52e2012-01-30 13:53:58 +0900118 const bool useFullEditDistance, const int inputLength,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900119 Correction *correction, WordsPriorityQueuePool *queuePool,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900120 const bool hasAutoCorrectionCandidate) const;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +0900121 void onTerminal(const int freq, const TerminalAttributes& terminalAttributes,
satok8330b482012-01-23 16:52:37 +0900122 Correction *correction, WordsPriorityQueuePool *queuePool, const bool addToMasterQueue,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900123 const int currentWordIndex) const;
satok662fe692010-12-08 17:05:39 +0900124 // Process a node by considering proximity, missing and excessive character
Jean Chalard8950ce62012-05-07 16:28:30 +0900125 bool processCurrentNode(const int initialPos, const std::map<int, int> *bigramMap,
126 const uint8_t *bigramFilter, Correction *correction, int *newCount,
127 int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueuePool *queuePool,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900128 const int currentWordIndex) const;
Jean Chalardbb15e772011-06-30 20:14:38 +0900129 int getMostFrequentWordLike(const int startInputIndex, const int inputLength,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900130 Correction *correction, unsigned short *word) const;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900131 int getMostFrequentWordLikeInner(const uint16_t *const inWord, const int length,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900132 short unsigned int *outWord) const;
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900133 int getSubStringSuggestion(
satok7409d152012-01-26 16:13:25 +0900134 ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates,
satok3c09bb12012-01-26 18:36:19 +0900135 const int *codes, const bool useFullEditDistance, Correction *correction,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900136 WordsPriorityQueuePool *queuePool, const int inputLength,
satok3c09bb12012-01-26 18:36:19 +0900137 const bool hasAutoCorrectionCandidate, const int currentWordIndex,
138 const int inputWordStartPos, const int inputWordLength,
satok99557162012-01-26 22:49:13 +0900139 const int outputWordStartPos, const bool isSpaceProximity, int *freqArray,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900140 int *wordLengthArray, unsigned short *outputWord, int *outputWordLength) const;
satok1f6b52e2012-01-30 13:53:58 +0900141 void getMultiWordsSuggestionRec(ProximityInfo *proximityInfo,
142 const int *xcoordinates, const int *ycoordinates, const int *codes,
143 const bool useFullEditDistance, const int inputLength,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900144 Correction *correction, WordsPriorityQueuePool *queuePool,
satok1f6b52e2012-01-30 13:53:58 +0900145 const bool hasAutoCorrectionCandidate, const int startPos, const int startWordIndex,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900146 const int outputWordLength, int *freqArray, int *wordLengthArray,
147 unsigned short *outputWord) const;
Jean Chalard293ece02011-06-16 20:55:16 +0900148
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900149 const uint8_t *const DICT_ROOT;
satok30088252010-12-01 21:22:15 +0900150 const int MAX_WORD_LENGTH;
Ken Wakasae90b3332011-01-07 15:01:51 +0900151 const int MAX_WORDS;
satok18c28f42010-12-02 18:11:54 +0900152 const int TYPED_LETTER_MULTIPLIER;
153 const int FULL_WORD_MULTIPLIER;
Ken Wakasae90b3332011-01-07 15:01:51 +0900154 const int ROOT_POS;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900155 const unsigned int BYTES_IN_ONE_CHAR;
Jean Chalard6c300612012-03-06 19:54:03 +0900156 const int MAX_DIGRAPH_SEARCH_DEPTH;
Jean Chalardcd274b12012-04-06 18:26:00 +0900157 const int FLAGS;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900158
Jean Chalard6c300612012-03-06 19:54:03 +0900159 static const digraph_t GERMAN_UMLAUT_DIGRAPHS[];
Jean Chalardcc78d032012-03-23 16:48:49 +0900160 static const digraph_t FRENCH_LIGATURES_DIGRAPHS[];
satok30088252010-12-01 21:22:15 +0900161};
Ken Wakasace9e52a2011-06-18 13:09:55 +0900162} // namespace latinime
satok30088252010-12-01 21:22:15 +0900163#endif // LATINIME_UNIGRAM_DICTIONARY_H