blob: cb86da41ce895e85662876e3c4f7c6f2d967a847 [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>
satok2df30602011-07-15 13:49:00 +090021#include "correction_state.h"
satoke808e432010-12-02 14:53:24 +090022#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090023#include "proximity_info.h"
satoke808e432010-12-02 14:53:24 +090024
Jean Chalard293ece02011-06-16 20:55:16 +090025#ifndef NULL
26#define NULL 0
27#endif
28
satok30088252010-12-01 21:22:15 +090029namespace latinime {
30
satok30088252010-12-01 21:22:15 +090031class UnigramDictionary {
Jean Chalard8dc754a2011-01-27 14:20:22 +090032
satok30088252010-12-01 21:22:15 +090033public:
Jean Chalard1059f272011-06-28 20:45:05 +090034
35 // Mask and flags for children address type selection.
36 static const int MASK_GROUP_ADDRESS_TYPE = 0xC0;
37 static const int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00;
38 static const int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40;
39 static const int FLAG_GROUP_ADDRESS_TYPE_TWOBYTES = 0x80;
40 static const int FLAG_GROUP_ADDRESS_TYPE_THREEBYTES = 0xC0;
41
42 // Flag for single/multiple char group
43 static const int FLAG_HAS_MULTIPLE_CHARS = 0x20;
44
45 // Flag for terminal groups
46 static const int FLAG_IS_TERMINAL = 0x10;
47
48 // Flag for bigram presence
49 static const int FLAG_HAS_BIGRAMS = 0x04;
50
51 // Attribute (bigram/shortcut) related flags:
52 // Flag for presence of more attributes
53 static const int FLAG_ATTRIBUTE_HAS_NEXT = 0x80;
54 // Flag for sign of offset. If this flag is set, the offset value must be negated.
55 static const int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40;
56
57 // Mask for attribute frequency, stored on 4 bits inside the flags byte.
58 static const int MASK_ATTRIBUTE_FREQUENCY = 0x0F;
59
60 // Mask and flags for attribute address type selection.
61 static const int MASK_ATTRIBUTE_ADDRESS_TYPE = 0x30;
62 static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10;
63 static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20;
64 static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30;
Jean Chalard1059f272011-06-28 20:45:05 +090065
Jean Chalard293ece02011-06-16 20:55:16 +090066 UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler,
67 int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars,
68 const bool isLatestDictVersion);
Jean Chalard1059f272011-06-28 20:45:05 +090069 bool isValidWord(const uint16_t* const inWord, const int length) const;
Jean Chalard581335c2011-06-17 12:45:17 +090070 int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
satok1d7eaf82011-07-13 10:32:02 +090071 int getSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090072 const int *ycoordinates, const int *codes, const int codesSize, const int flags,
73 unsigned short *outWords, int *frequencies);
satok2df30602011-07-15 13:49:00 +090074 virtual ~UnigramDictionary();
satok30088252010-12-01 21:22:15 +090075
76private:
satok612c6e42011-08-01 19:35:27 +090077
satok1d7eaf82011-07-13 10:32:02 +090078 void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090079 const int *ycoordinates, const int *codes, const int codesSize,
80 unsigned short *outWords, int *frequencies);
81 bool isDigraph(const int* codes, const int i, const int codesSize) const;
satok1d7eaf82011-07-13 10:32:02 +090082 void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090083 const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
84 const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain,
satok3c4bb772011-03-04 22:50:19 -080085 const int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies);
satok1d7eaf82011-07-13 10:32:02 +090086 void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
87 const int *ycoordinates, const int *codes, const int codesSize,
88 unsigned short *outWords, int *frequencies);
satok54fe9e02010-12-13 14:42:35 +090089 void getSuggestionCandidates(const int skipPos, const int excessivePos,
satok0f6c8e82011-08-03 02:19:44 +090090 const int transposedPos, const int maxDepth);
satok30088252010-12-01 21:22:15 +090091 bool addWord(unsigned short *word, int length, int frequency);
satok612c6e42011-08-01 19:35:27 +090092 void getSplitTwoWordsSuggestion(const int inputLength, CorrectionState *correctionState);
93 void getMissingSpaceWords(
94 const int inputLength, const int missingSpacePos, CorrectionState *correctionState);
95 void getMistypedSpaceWords(
96 const int inputLength, const int spaceProximityPos, CorrectionState *correctionState);
satok4e4e74e2011-08-03 23:27:32 +090097 void onTerminal(unsigned short int* word, const int freq, CorrectionState *correctionState);
satok28bd03b2010-12-03 16:39:16 +090098 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +090099 const int inputIndex, const int skipPos, const int depth);
satok662fe692010-12-08 17:05:39 +0900100 // Process a node by considering proximity, missing and excessive character
satok4e4e74e2011-08-03 23:27:32 +0900101 bool processCurrentNode(const int initialPos, const int maxDepth,
102 const bool initialTraverseAllNodes, const int initialDiffs,
satok2df30602011-07-15 13:49:00 +0900103 CorrectionState *correctionState, int *newCount, int *newChildPosition,
satok4e4e74e2011-08-03 23:27:32 +0900104 bool *newTraverseAllNodes, int *newDiffs, int *nextSiblingPosition);
Jean Chalardbb15e772011-06-30 20:14:38 +0900105 int getMostFrequentWordLike(const int startInputIndex, const int inputLength,
106 unsigned short *word);
Jean Chalard1059f272011-06-28 20:45:05 +0900107 int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length,
108 short unsigned int* outWord);
Jean Chalard293ece02011-06-16 20:55:16 +0900109
110 const uint8_t* const DICT_ROOT;
satok30088252010-12-01 21:22:15 +0900111 const int MAX_WORD_LENGTH;
Ken Wakasae90b3332011-01-07 15:01:51 +0900112 const int MAX_WORDS;
satok662fe692010-12-08 17:05:39 +0900113 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +0900114 const bool IS_LATEST_DICT_VERSION;
satok18c28f42010-12-02 18:11:54 +0900115 const int TYPED_LETTER_MULTIPLIER;
116 const int FULL_WORD_MULTIPLIER;
Ken Wakasae90b3332011-01-07 15:01:51 +0900117 const int ROOT_POS;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900118 const unsigned int BYTES_IN_ONE_CHAR;
satok3c4bb772011-03-04 22:50:19 -0800119 const int MAX_UMLAUT_SEARCH_DEPTH;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900120
121 // Flags for special processing
122 // Those *must* match the flags in BinaryDictionary.Flags.ALL_FLAGS in BinaryDictionary.java
123 // or something very bad (like, the apocalypse) will happen.
124 // Please update both at the same time.
125 enum {
126 REQUIRES_GERMAN_UMLAUT_PROCESSING = 0x1
127 };
128 static const struct digraph_t { int first; int second; } GERMAN_UMLAUT_DIGRAPHS[];
satok30088252010-12-01 21:22:15 +0900129
satok30088252010-12-01 21:22:15 +0900130 int *mFrequencies;
satok30088252010-12-01 21:22:15 +0900131 unsigned short *mOutputChars;
satok2df30602011-07-15 13:49:00 +0900132 ProximityInfo *mProximityInfo;
133 CorrectionState *mCorrectionState;
satok30088252010-12-01 21:22:15 +0900134 int mInputLength;
satok715514d2010-12-02 20:19:59 +0900135 // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH
136 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900137 int mMaxEditDistance;
satokd2997922010-12-07 13:08:39 +0900138
satok0f6c8e82011-08-03 02:19:44 +0900139 int mStackMatchedCount[MAX_WORD_LENGTH_INTERNAL];
satokd2997922010-12-07 13:08:39 +0900140 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];
141 bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL];
satokd2997922010-12-07 13:08:39 +0900142 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];
143 int mStackDiffs[MAX_WORD_LENGTH_INTERNAL];
144 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
Jean Chalard17e44a72011-06-16 22:51:11 +0900145 int mStackOutputIndex[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900146};
Ken Wakasace9e52a2011-06-18 13:09:55 +0900147} // namespace latinime
satok30088252010-12-01 21:22:15 +0900148
149#endif // LATINIME_UNIGRAM_DICTIONARY_H