blob: e84875b5902017efe2ad9956305a57938d6072a5 [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"
satok8fbd5522011-02-22 17:28:55 +090021#include "proximity_info.h"
satoke808e432010-12-02 14:53:24 +090022
satok30088252010-12-01 21:22:15 +090023namespace latinime {
24
satok30088252010-12-01 21:22:15 +090025class UnigramDictionary {
Jean Chalard8dc754a2011-01-27 14:20:22 +090026
27 typedef enum { // Used as a return value for character comparison
28 SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR, // Same char, possibly with different case or accent
29 NEAR_PROXIMITY_CHAR, // It is a char located nearby on the keyboard
30 UNRELATED_CHAR // It is an unrelated char
31 } ProximityType;
32
satok30088252010-12-01 21:22:15 +090033public:
satoke808e432010-12-02 14:53:24 +090034 UnigramDictionary(const unsigned char *dict, int typedLetterMultipler, int fullWordMultiplier,
satok662fe692010-12-08 17:05:39 +090035 int maxWordLength, int maxWords, int maxProximityChars, const bool isLatestDictVersion);
satok8fbd5522011-02-22 17:28:55 +090036 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
37 int *codes, int codesSize, unsigned short *outWords, int *frequencies);
satok30088252010-12-01 21:22:15 +090038 ~UnigramDictionary();
39
40private:
41 void initSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies);
satok54fe9e02010-12-13 14:42:35 +090042 void getSuggestionCandidates(const int skipPos, const int excessivePos,
satoka3d78f62010-12-09 22:08:33 +090043 const int transposedPos, int *nextLetters, const int nextLettersSize,
44 const int maxDepth);
satok30088252010-12-01 21:22:15 +090045 void getVersionNumber();
46 bool checkIfDictVersionIsLatest();
47 int getAddress(int *pos);
satok30088252010-12-01 21:22:15 +090048 int getFreq(int *pos);
satok30088252010-12-01 21:22:15 +090049 int wideStrLen(unsigned short *str);
satok30088252010-12-01 21:22:15 +090050 bool sameAsTyped(unsigned short *word, int length);
satok30088252010-12-01 21:22:15 +090051 bool addWord(unsigned short *word, int length, int frequency);
Jean Chalardf5f834a2011-02-22 15:12:46 +090052 unsigned short toBaseLowerCase(unsigned short c);
satok68319262010-12-03 19:38:08 +090053 void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth,
54 const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs,
satoka3d78f62010-12-09 22:08:33 +090055 const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters,
56 const int nextLettersSize);
satok662fe692010-12-08 17:05:39 +090057 bool getMissingSpaceWords(const int inputLength, const int missingSpacePos);
satokd2997922010-12-07 13:08:39 +090058 // Keep getWordsOld for comparing performance between getWords and getWordsOld
59 void getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +090060 const int excessivePos, const int transposedPos, int *nextLetters,
61 const int nextLettersSize);
satok30088252010-12-01 21:22:15 +090062 void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize);
satok58c49b92011-01-27 03:23:39 +090063 int calculateFinalFreq(const int inputIndex, const int depth, const int snr, const int skipPos,
satok54fe9e02010-12-13 14:42:35 +090064 const int excessivePos, const int transposedPos, const int freq, const bool sameLength);
satok715514d2010-12-02 20:19:59 +090065 void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word,
satok54fe9e02010-12-13 14:42:35 +090066 const int inputIndex, const int depth, const int snr, int *nextLetters,
satoka3d78f62010-12-09 22:08:33 +090067 const int nextLettersSize, const int skipPos, const int excessivePos,
68 const int transposedPos, const int freq);
satok54fe9e02010-12-13 14:42:35 +090069 void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word,
70 const int inputIndex, const int depth, const int snr, const int skipPos,
Jean Chalard8dc754a2011-01-27 14:20:22 +090071 const int excessivePos, const int transposedPos, const int freq);
satok28bd03b2010-12-03 16:39:16 +090072 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +090073 const int inputIndex, const int skipPos, const int depth);
Jean Chalard8dc754a2011-01-27 14:20:22 +090074 ProximityType getMatchedProximityId(const int *currentChars, const unsigned short c,
75 const int skipPos, const int excessivePos, const int transposedPos);
satok662fe692010-12-08 17:05:39 +090076 // Process a node by considering proximity, missing and excessive character
satok48e432c2010-12-06 17:38:58 +090077 bool processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +090078 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +090079 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
80 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
satokcdbbea72010-12-08 16:04:16 +090081 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
82 int *nextSiblingPosition);
satokaee09dc2010-12-09 19:21:51 +090083 int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word);
satok662fe692010-12-08 17:05:39 +090084 // Process a node by considering missing space
satokaee09dc2010-12-09 19:21:51 +090085 bool processCurrentNodeForExactMatch(const int firstChildPos,
86 const int startInputIndex, const int depth, unsigned short *word,
87 int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos);
satoke07baa62010-12-09 21:55:40 +090088 bool existsAdjacentProximityChars(const int inputIndex, const int inputLength);
satok8fbd5522011-02-22 17:28:55 +090089 inline int* getInputCharsAt(const int index) {
90 return mInputCodes + (index * MAX_PROXIMITY_CHARS);
91 }
satoke808e432010-12-02 14:53:24 +090092 const unsigned char *DICT;
satok30088252010-12-01 21:22:15 +090093 const int MAX_WORD_LENGTH;
Ken Wakasae90b3332011-01-07 15:01:51 +090094 const int MAX_WORDS;
satok662fe692010-12-08 17:05:39 +090095 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +090096 const bool IS_LATEST_DICT_VERSION;
satok18c28f42010-12-02 18:11:54 +090097 const int TYPED_LETTER_MULTIPLIER;
98 const int FULL_WORD_MULTIPLIER;
Ken Wakasae90b3332011-01-07 15:01:51 +090099 const int ROOT_POS;
satok30088252010-12-01 21:22:15 +0900100
satok30088252010-12-01 21:22:15 +0900101 int *mFrequencies;
satok30088252010-12-01 21:22:15 +0900102 unsigned short *mOutputChars;
satok30088252010-12-01 21:22:15 +0900103 int *mInputCodes;
104 int mInputLength;
satok715514d2010-12-02 20:19:59 +0900105 // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH
106 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900107 int mMaxEditDistance;
satokd2997922010-12-07 13:08:39 +0900108
109 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];
110 bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL];
111 int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL];
112 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];
113 int mStackDiffs[MAX_WORD_LENGTH_INTERNAL];
114 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900115 int mNextLettersFrequency[NEXT_LETTERS_SIZE];
satok30088252010-12-01 21:22:15 +0900116};
117
118// ----------------------------------------------------------------------------
119
120}; // namespace latinime
121
122#endif // LATINIME_UNIGRAM_DICTIONARY_H