blob: 445ff7a17c5b0f15f152a74645fad2358005fd31 [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 {
25public:
satoke808e432010-12-02 14:53:24 +090026 UnigramDictionary(const unsigned char *dict, int typedLetterMultipler, int fullWordMultiplier,
satok662fe692010-12-08 17:05:39 +090027 int maxWordLength, int maxWords, int maxProximityChars, const bool isLatestDictVersion);
satok30088252010-12-01 21:22:15 +090028 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
29 int *nextLetters, int nextLettersSize);
satok30088252010-12-01 21:22:15 +090030 ~UnigramDictionary();
31
32private:
33 void initSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies);
satok54fe9e02010-12-13 14:42:35 +090034 void getSuggestionCandidates(const int skipPos, const int excessivePos,
satoka3d78f62010-12-09 22:08:33 +090035 const int transposedPos, int *nextLetters, const int nextLettersSize,
36 const int maxDepth);
satok30088252010-12-01 21:22:15 +090037 void getVersionNumber();
38 bool checkIfDictVersionIsLatest();
39 int getAddress(int *pos);
satok30088252010-12-01 21:22:15 +090040 int getFreq(int *pos);
satok30088252010-12-01 21:22:15 +090041 int wideStrLen(unsigned short *str);
satok30088252010-12-01 21:22:15 +090042 bool sameAsTyped(unsigned short *word, int length);
satok30088252010-12-01 21:22:15 +090043 bool addWord(unsigned short *word, int length, int frequency);
satok30088252010-12-01 21:22:15 +090044 unsigned short toLowerCase(unsigned short c);
satok68319262010-12-03 19:38:08 +090045 void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth,
46 const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs,
satoka3d78f62010-12-09 22:08:33 +090047 const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters,
48 const int nextLettersSize);
satok662fe692010-12-08 17:05:39 +090049 bool getMissingSpaceWords(const int inputLength, const int missingSpacePos);
satokd2997922010-12-07 13:08:39 +090050 // Keep getWordsOld for comparing performance between getWords and getWordsOld
51 void getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +090052 const int excessivePos, const int transposedPos, int *nextLetters,
53 const int nextLettersSize);
satok30088252010-12-01 21:22:15 +090054 void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize);
satok54fe9e02010-12-13 14:42:35 +090055 int calculateFinalFreq(const int inputIndex, const int snr, const int skipPos,
56 const int excessivePos, const int transposedPos, const int freq, const bool sameLength);
satok715514d2010-12-02 20:19:59 +090057 void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word,
satok54fe9e02010-12-13 14:42:35 +090058 const int inputIndex, const int depth, const int snr, int *nextLetters,
satoka3d78f62010-12-09 22:08:33 +090059 const int nextLettersSize, const int skipPos, const int excessivePos,
60 const int transposedPos, const int freq);
satok54fe9e02010-12-13 14:42:35 +090061 void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word,
62 const int inputIndex, const int depth, const int snr, const int skipPos,
63 const int excessivePos, const int transposedPos, const int freq, const int addedWeight);
satok28bd03b2010-12-03 16:39:16 +090064 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +090065 const int inputIndex, const int skipPos, const int depth);
satoka3d78f62010-12-09 22:08:33 +090066 int getMatchedProximityId(const int *currentChars, const unsigned short c, const int skipPos,
67 const int excessivePos, const int transposedPos);
satok662fe692010-12-08 17:05:39 +090068 // Process a node by considering proximity, missing and excessive character
satok48e432c2010-12-06 17:38:58 +090069 bool processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +090070 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +090071 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
72 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
satokcdbbea72010-12-08 16:04:16 +090073 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
74 int *nextSiblingPosition);
satokaee09dc2010-12-09 19:21:51 +090075 int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word);
satok662fe692010-12-08 17:05:39 +090076 // Process a node by considering missing space
satokaee09dc2010-12-09 19:21:51 +090077 bool processCurrentNodeForExactMatch(const int firstChildPos,
78 const int startInputIndex, const int depth, unsigned short *word,
79 int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos);
satoke07baa62010-12-09 21:55:40 +090080 bool existsAdjacentProximityChars(const int inputIndex, const int inputLength);
81 int* getInputCharsAt(const int index) {return mInputCodes + (index * MAX_PROXIMITY_CHARS);}
satoke808e432010-12-02 14:53:24 +090082 const unsigned char *DICT;
satok30088252010-12-01 21:22:15 +090083 const int MAX_WORDS;
84 const int MAX_WORD_LENGTH;
satok662fe692010-12-08 17:05:39 +090085 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +090086 const bool IS_LATEST_DICT_VERSION;
satok662fe692010-12-08 17:05:39 +090087 const int ROOT_POS;
satok18c28f42010-12-02 18:11:54 +090088 const int TYPED_LETTER_MULTIPLIER;
89 const int FULL_WORD_MULTIPLIER;
satok30088252010-12-01 21:22:15 +090090
satok30088252010-12-01 21:22:15 +090091 int *mFrequencies;
satok30088252010-12-01 21:22:15 +090092 unsigned short *mOutputChars;
satok30088252010-12-01 21:22:15 +090093 int *mInputCodes;
94 int mInputLength;
satok715514d2010-12-02 20:19:59 +090095 // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH
96 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +090097 int mMaxEditDistance;
satokd2997922010-12-07 13:08:39 +090098
99 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];
100 bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL];
101 int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL];
102 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];
103 int mStackDiffs[MAX_WORD_LENGTH_INTERNAL];
104 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +0900105};
106
107// ----------------------------------------------------------------------------
108
109}; // namespace latinime
110
111#endif // LATINIME_UNIGRAM_DICTIONARY_H