blob: cdec46557534d66cd860ff1a8c0a918fb061c84a [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);
satok662fe692010-12-08 17:05:39 +090034 void getSuggestionCandidates(const int inputLength, const int skipPos, const int excessivePos,
35 int *nextLetters, const int nextLettersSize);
satok30088252010-12-01 21:22:15 +090036 void getVersionNumber();
37 bool checkIfDictVersionIsLatest();
38 int getAddress(int *pos);
satok30088252010-12-01 21:22:15 +090039 int getFreq(int *pos);
satok30088252010-12-01 21:22:15 +090040 int wideStrLen(unsigned short *str);
satok30088252010-12-01 21:22:15 +090041 bool sameAsTyped(unsigned short *word, int length);
satok30088252010-12-01 21:22:15 +090042 bool addWord(unsigned short *word, int length, int frequency);
satok30088252010-12-01 21:22:15 +090043 unsigned short toLowerCase(unsigned short c);
satok68319262010-12-03 19:38:08 +090044 void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth,
45 const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs,
satokcdbbea72010-12-08 16:04:16 +090046 const int skipPos, const int excessivePos, int *nextLetters, const int nextLettersSize);
satok662fe692010-12-08 17:05:39 +090047 bool getMissingSpaceWords(const int inputLength, const int missingSpacePos);
satokd2997922010-12-07 13:08:39 +090048 // Keep getWordsOld for comparing performance between getWords and getWordsOld
49 void getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satokcdbbea72010-12-08 16:04:16 +090050 const int excessivePos, int *nextLetters, const int nextLettersSize);
satok30088252010-12-01 21:22:15 +090051 void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize);
satok715514d2010-12-02 20:19:59 +090052 void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word,
53 const int mInputLength, const int depth, const int snr, int *nextLetters,
54 const int nextLettersSize, const int skipPos, const int freq);
satok715514d2010-12-02 20:19:59 +090055 void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word, const int depth,
56 const int snr, const int skipPos, const int freq, const int addedWeight);
satok28bd03b2010-12-03 16:39:16 +090057 bool needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +090058 const int inputIndex, const int skipPos, const int depth);
satok48e432c2010-12-06 17:38:58 +090059 int getMatchedProximityId(const int *currentChars, const unsigned short c, const int skipPos);
satok662fe692010-12-08 17:05:39 +090060 // Process a node by considering proximity, missing and excessive character
satok48e432c2010-12-06 17:38:58 +090061 bool processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +090062 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
63 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
64 const int nextLettersSize, int *newCount, int *newChildPosition,
65 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
66 int *nextSiblingPosition);
satokaee09dc2010-12-09 19:21:51 +090067 int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word);
satok662fe692010-12-08 17:05:39 +090068 // Process a node by considering missing space
satokaee09dc2010-12-09 19:21:51 +090069 bool processCurrentNodeForExactMatch(const int firstChildPos,
70 const int startInputIndex, const int depth, unsigned short *word,
71 int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos);
satok662fe692010-12-08 17:05:39 +090072
satoke808e432010-12-02 14:53:24 +090073 const unsigned char *DICT;
satok30088252010-12-01 21:22:15 +090074 const int MAX_WORDS;
75 const int MAX_WORD_LENGTH;
satok662fe692010-12-08 17:05:39 +090076 const int MAX_PROXIMITY_CHARS;
satoke808e432010-12-02 14:53:24 +090077 const bool IS_LATEST_DICT_VERSION;
satok662fe692010-12-08 17:05:39 +090078 const int ROOT_POS;
satok18c28f42010-12-02 18:11:54 +090079 const int TYPED_LETTER_MULTIPLIER;
80 const int FULL_WORD_MULTIPLIER;
satok30088252010-12-01 21:22:15 +090081
satok30088252010-12-01 21:22:15 +090082 int *mFrequencies;
satok30088252010-12-01 21:22:15 +090083 unsigned short *mOutputChars;
satok30088252010-12-01 21:22:15 +090084 int *mInputCodes;
85 int mInputLength;
satok715514d2010-12-02 20:19:59 +090086 // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH
87 unsigned short mWord[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +090088 int mMaxEditDistance;
satokd2997922010-12-07 13:08:39 +090089
90 int mStackChildCount[MAX_WORD_LENGTH_INTERNAL];
91 bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL];
92 int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL];
93 int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL];
94 int mStackDiffs[MAX_WORD_LENGTH_INTERNAL];
95 int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
satok30088252010-12-01 21:22:15 +090096};
97
98// ----------------------------------------------------------------------------
99
100}; // namespace latinime
101
102#endif // LATINIME_UNIGRAM_DICTIONARY_H