blob: 6c20f122138bcf65417b963be37d4d812f4551a2 [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 Chalard293ece02011-06-16 20:55:16 +090020#include <stdint.h>
satokcfca3c62011-08-10 14:30:10 +090021#include "correction.h"
satok208268d2011-08-10 15:44:08 +090022#include "correction_state.h"
satoke808e432010-12-02 14:53:24 +090023#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090024#include "proximity_info.h"
satok16379df2011-12-12 20:53:22 +090025#include "words_priority_queue.h"
satoka7e5a5a2011-12-15 16:49:12 +090026#include "words_priority_queue_pool.h"
satoke808e432010-12-02 14:53:24 +090027
satok30088252010-12-01 21:22:15 +090028namespace latinime {
29
satok30088252010-12-01 21:22:15 +090030class UnigramDictionary {
Ken Wakasae12e9b52012-01-06 12:24:38 +090031 public:
Jean Chalard1059f272011-06-28 20:45:05 +090032 // Mask and flags for children address type selection.
33 static const int MASK_GROUP_ADDRESS_TYPE = 0xC0;
34 static const int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00;
35 static const int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40;
36 static const int FLAG_GROUP_ADDRESS_TYPE_TWOBYTES = 0x80;
37 static const int FLAG_GROUP_ADDRESS_TYPE_THREEBYTES = 0xC0;
38
39 // Flag for single/multiple char group
40 static const int FLAG_HAS_MULTIPLE_CHARS = 0x20;
41
42 // Flag for terminal groups
43 static const int FLAG_IS_TERMINAL = 0x10;
44
Jean Chalardc8c65852011-12-27 12:58:23 +090045 // Flag for shortcut targets presence
46 static const int FLAG_HAS_SHORTCUT_TARGETS = 0x08;
Jean Chalard1059f272011-06-28 20:45:05 +090047 // Flag for bigram presence
48 static const int FLAG_HAS_BIGRAMS = 0x04;
Jean Chalardc8c65852011-12-27 12:58:23 +090049 // Flag for shortcut-only words. Some words are shortcut-only, which means they match when
50 // the user types them but they don't pop in the suggestion strip, only the words they are
51 // shortcuts for do.
52 static const int FLAG_IS_SHORTCUT_ONLY = 0x02;
Jean Chalard1059f272011-06-28 20:45:05 +090053
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
Jean Chalard293ece02011-06-16 20:55:16 +090073 UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler,
74 int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars,
75 const bool isLatestDictVersion);
Jean Chalard1059f272011-06-28 20:45:05 +090076 bool isValidWord(const uint16_t* const inWord, const int length) const;
Jean Chalard581335c2011-06-17 12:45:17 +090077 int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
satoka7e5a5a2011-12-15 16:49:12 +090078 int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool,
satok1147c7b2011-12-14 15:04:58 +090079 Correction *correction, const int *xcoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090080 const int *ycoordinates, const int *codes, const int codesSize, const int flags,
81 unsigned short *outWords, int *frequencies);
satok2df30602011-07-15 13:49:00 +090082 virtual ~UnigramDictionary();
satok30088252010-12-01 21:22:15 +090083
Ken Wakasae12e9b52012-01-06 12:24:38 +090084 private:
satok1d7eaf82011-07-13 10:32:02 +090085 void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
satok1147c7b2011-12-14 15:04:58 +090086 const int *ycoordinates, const int *codes, const int inputLength,
satoka7e5a5a2011-12-15 16:49:12 +090087 const int flags, Correction *correction, WordsPriorityQueuePool *queuePool);
satok1147c7b2011-12-14 15:04:58 +090088 bool isDigraph(const int *codes, const int i, const int codesSize) const;
satok1d7eaf82011-07-13 10:32:02 +090089 void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090090 const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
satok1147c7b2011-12-14 15:04:58 +090091 const int codesBufferSize, const int flags, const int* codesSrc,
92 const int codesRemain, const int currentDepth, int* codesDest, Correction *correction,
satoka7e5a5a2011-12-15 16:49:12 +090093 WordsPriorityQueuePool* queuePool);
satok1d7eaf82011-07-13 10:32:02 +090094 void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
satok1147c7b2011-12-14 15:04:58 +090095 const int *ycoordinates, const int *codes, const int codesSize,
satok1a6da632011-12-16 23:15:06 +090096 WordsPriorityQueue *queue, Correction *correction);
satok744dab62011-12-15 22:29:05 +090097 void getOneWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
98 const int *ycoordinates, const int *codes, const bool useFullEditDistance,
99 const int inputLength, Correction *correction, WordsPriorityQueuePool* queuePool);
satok1147c7b2011-12-14 15:04:58 +0900100 void getSuggestionCandidates(
101 const bool useFullEditDistance, const int inputLength, Correction *correction,
satok1a6da632011-12-16 23:15:06 +0900102 WordsPriorityQueue* queue, const bool doAutoCompletion, const int maxErrors);
satok744dab62011-12-15 22:29:05 +0900103 void getSplitTwoWordsSuggestions(ProximityInfo *proximityInfo,
104 const int *xcoordinates, const int *ycoordinates, const int *codes,
105 const bool useFullEditDistance, const int inputLength, const int spaceProximityPos,
106 const int missingSpacePos, Correction *correction, WordsPriorityQueuePool* queuePool);
107 void getMissingSpaceWords(ProximityInfo *proximityInfo, const int *xcoordinates,
108 const int *ycoordinates, const int *codes, const bool useFullEditDistance,
109 const int inputLength, const int missingSpacePos, Correction *correction,
110 WordsPriorityQueuePool* queuePool);
111 void getMistypedSpaceWords(ProximityInfo *proximityInfo, const int *xcoordinates,
112 const int *ycoordinates, const int *codes, const bool useFullEditDistance,
113 const int inputLength, const int spaceProximityPos, Correction *correction,
114 WordsPriorityQueuePool* queuePool);
satok1147c7b2011-12-14 15:04:58 +0900115 void onTerminal(const int freq, Correction *correction, WordsPriorityQueue *queue);
satok28bd03b2010-12-03 16:39:16 +0900116 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900117 const int inputIndex, const int skipPos, const int depth);
satok662fe692010-12-08 17:05:39 +0900118 // Process a node by considering proximity, missing and excessive character
satok1147c7b2011-12-14 15:04:58 +0900119 bool processCurrentNode(const int initialPos, Correction *correction, int *newCount,
120 int *newChildPosition, int *nextSiblingPosition, WordsPriorityQueue *queue);
Jean Chalardbb15e772011-06-30 20:14:38 +0900121 int getMostFrequentWordLike(const int startInputIndex, const int inputLength,
satok1147c7b2011-12-14 15:04:58 +0900122 ProximityInfo *proximityInfo, unsigned short *word);
Jean Chalard1059f272011-06-28 20:45:05 +0900123 int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length,
satok1147c7b2011-12-14 15:04:58 +0900124 short unsigned int *outWord);
Jean Chalard293ece02011-06-16 20:55:16 +0900125
126 const uint8_t* const DICT_ROOT;
satok30088252010-12-01 21:22:15 +0900127 const int MAX_WORD_LENGTH;
Ken Wakasae90b3332011-01-07 15:01:51 +0900128 const int MAX_WORDS;
satok662fe692010-12-08 17:05:39 +0900129 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +0900130 const bool IS_LATEST_DICT_VERSION;
satok18c28f42010-12-02 18:11:54 +0900131 const int TYPED_LETTER_MULTIPLIER;
132 const int FULL_WORD_MULTIPLIER;
Ken Wakasae90b3332011-01-07 15:01:51 +0900133 const int ROOT_POS;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900134 const unsigned int BYTES_IN_ONE_CHAR;
satok3c4bb772011-03-04 22:50:19 -0800135 const int MAX_UMLAUT_SEARCH_DEPTH;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900136
137 // Flags for special processing
138 // Those *must* match the flags in BinaryDictionary.Flags.ALL_FLAGS in BinaryDictionary.java
139 // or something very bad (like, the apocalypse) will happen.
140 // Please update both at the same time.
141 enum {
satok40a5f6f2011-09-29 18:36:56 +0900142 REQUIRES_GERMAN_UMLAUT_PROCESSING = 0x1,
143 USE_FULL_EDIT_DISTANCE = 0x2
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900144 };
145 static const struct digraph_t { int first; int second; } GERMAN_UMLAUT_DIGRAPHS[];
satok30088252010-12-01 21:22:15 +0900146
satok1147c7b2011-12-14 15:04:58 +0900147 // Still bundled members
148 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
satok208268d2011-08-10 15:44:08 +0900149 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
150 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
151 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];// TODO: remove
satok30088252010-12-01 21:22:15 +0900152};
Ken Wakasace9e52a2011-06-18 13:09:55 +0900153} // namespace latinime
satok30088252010-12-01 21:22:15 +0900154
155#endif // LATINIME_UNIGRAM_DICTIONARY_H