blob: 90c98149bf73c0ab02ed92128a5ad4ed00c6edb7 [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
satoke808e432010-12-02 14:53:24 +090020#include "defines.h"
21
satok30088252010-12-01 21:22:15 +090022namespace latinime {
23
satok30088252010-12-01 21:22:15 +090024class UnigramDictionary {
Jean Chalard8dc754a2011-01-27 14:20:22 +090025
26 typedef enum { // Used as a return value for character comparison
27 SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR, // Same char, possibly with different case or accent
28 NEAR_PROXIMITY_CHAR, // It is a char located nearby on the keyboard
29 UNRELATED_CHAR // It is an unrelated char
30 } ProximityType;
31
satok30088252010-12-01 21:22:15 +090032public:
satoke808e432010-12-02 14:53:24 +090033 UnigramDictionary(const unsigned char *dict, int typedLetterMultipler, int fullWordMultiplier,
satok662fe692010-12-08 17:05:39 +090034 int maxWordLength, int maxWords, int maxProximityChars, const bool isLatestDictVersion);
satok30088252010-12-01 21:22:15 +090035 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
36 int *nextLetters, int nextLettersSize);
satok30088252010-12-01 21:22:15 +090037 ~UnigramDictionary();
38
39private:
40 void initSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies);
satok54fe9e02010-12-13 14:42:35 +090041 void getSuggestionCandidates(const int skipPos, const int excessivePos,
satoka3d78f62010-12-09 22:08:33 +090042 const int transposedPos, int *nextLetters, const int nextLettersSize,
43 const int maxDepth);
satok30088252010-12-01 21:22:15 +090044 void getVersionNumber();
45 bool checkIfDictVersionIsLatest();
46 int getAddress(int *pos);
satok30088252010-12-01 21:22:15 +090047 int getFreq(int *pos);
satok30088252010-12-01 21:22:15 +090048 int wideStrLen(unsigned short *str);
satok30088252010-12-01 21:22:15 +090049 bool sameAsTyped(unsigned short *word, int length);
satok30088252010-12-01 21:22:15 +090050 bool addWord(unsigned short *word, int length, int frequency);
satok30088252010-12-01 21:22:15 +090051 unsigned short toLowerCase(unsigned short c);
satok68319262010-12-03 19:38:08 +090052 void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth,
53 const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs,
satoka3d78f62010-12-09 22:08:33 +090054 const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters,
55 const int nextLettersSize);
satok662fe692010-12-08 17:05:39 +090056 bool getMissingSpaceWords(const int inputLength, const int missingSpacePos);
satokd2997922010-12-07 13:08:39 +090057 // Keep getWordsOld for comparing performance between getWords and getWordsOld
58 void getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +090059 const int excessivePos, const int transposedPos, int *nextLetters,
60 const int nextLettersSize);
satok30088252010-12-01 21:22:15 +090061 void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize);
satok58c49b92011-01-27 03:23:39 +090062 int calculateFinalFreq(const int inputIndex, const int depth, const int snr, const int skipPos,
satok54fe9e02010-12-13 14:42:35 +090063 const int excessivePos, const int transposedPos, const int freq, const bool sameLength);
satok715514d2010-12-02 20:19:59 +090064 void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word,
satok54fe9e02010-12-13 14:42:35 +090065 const int inputIndex, const int depth, const int snr, int *nextLetters,
satoka3d78f62010-12-09 22:08:33 +090066 const int nextLettersSize, const int skipPos, const int excessivePos,
67 const int transposedPos, const int freq);
satok54fe9e02010-12-13 14:42:35 +090068 void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word,
69 const int inputIndex, const int depth, const int snr, const int skipPos,
Jean Chalard8dc754a2011-01-27 14:20:22 +090070 const int excessivePos, const int transposedPos, const int freq);
satok28bd03b2010-12-03 16:39:16 +090071 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +090072 const int inputIndex, const int skipPos, const int depth);
Jean Chalard8dc754a2011-01-27 14:20:22 +090073 ProximityType getMatchedProximityId(const int *currentChars, const unsigned short c,
74 const int skipPos, const int excessivePos, const int transposedPos);
satok662fe692010-12-08 17:05:39 +090075 // Process a node by considering proximity, missing and excessive character
satok48e432c2010-12-06 17:38:58 +090076 bool processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +090077 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +090078 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
79 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
satokcdbbea72010-12-08 16:04:16 +090080 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
81 int *nextSiblingPosition);
satokaee09dc2010-12-09 19:21:51 +090082 int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word);
satok662fe692010-12-08 17:05:39 +090083 // Process a node by considering missing space
satokaee09dc2010-12-09 19:21:51 +090084 bool processCurrentNodeForExactMatch(const int firstChildPos,
85 const int startInputIndex, const int depth, unsigned short *word,
86 int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos);
satoke07baa62010-12-09 21:55:40 +090087 bool existsAdjacentProximityChars(const int inputIndex, const int inputLength);
88 int* getInputCharsAt(const int index) {return mInputCodes + (index * MAX_PROXIMITY_CHARS);}
satoke808e432010-12-02 14:53:24 +090089 const unsigned char *DICT;
satok30088252010-12-01 21:22:15 +090090 const int MAX_WORD_LENGTH;
Ken Wakasae90b3332011-01-07 15:01:51 +090091 const int MAX_WORDS;
satok662fe692010-12-08 17:05:39 +090092 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +090093 const bool IS_LATEST_DICT_VERSION;
satok18c28f42010-12-02 18:11:54 +090094 const int TYPED_LETTER_MULTIPLIER;
95 const int FULL_WORD_MULTIPLIER;
Ken Wakasae90b3332011-01-07 15:01:51 +090096 const int ROOT_POS;
satok30088252010-12-01 21:22:15 +090097
satok30088252010-12-01 21:22:15 +090098 int *mFrequencies;
satok30088252010-12-01 21:22:15 +090099 unsigned short *mOutputChars;
satok30088252010-12-01 21:22:15 +0900100 int *mInputCodes;
101 int mInputLength;
satok715514d2010-12-02 20:19:59 +0900102 // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH
103 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900104 int mMaxEditDistance;
satokd2997922010-12-07 13:08:39 +0900105
106 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];
107 bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL];
108 int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL];
109 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];
110 int mStackDiffs[MAX_WORD_LENGTH_INTERNAL];
111 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900112};
113
114// ----------------------------------------------------------------------------
115
116}; // namespace latinime
117
118#endif // LATINIME_UNIGRAM_DICTIONARY_H