blob: 1b41f69dde8a8455670df5e3ad3b181039eaff76 [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
2 * Copyright (C) 2009 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_DICTIONARY_H
18#define LATINIME_DICTIONARY_H
19
satok30088252010-12-01 21:22:15 +090020#include "bigram_dictionary.h"
satoke808e432010-12-02 14:53:24 +090021#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090022#include "proximity_info.h"
satok30088252010-12-01 21:22:15 +090023#include "unigram_dictionary.h"
24
The Android Open Source Project923bf412009-03-13 15:11:42 -070025namespace latinime {
26
27class Dictionary {
28public:
Ken Wakasae90b3332011-01-07 15:01:51 +090029 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
30 int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
satok8fbd5522011-02-22 17:28:55 +090031 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090032 int *codes, int codesSize, int flags, unsigned short *outWords, int *frequencies) {
satok8fbd5522011-02-22 17:28:55 +090033 return mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, codes,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090034 codesSize, flags, outWords, frequencies);
satok30088252010-12-01 21:22:15 +090035 }
36
37 // TODO: Call mBigramDictionary instead of mUnigramDictionary
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070038 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
39 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
satok30088252010-12-01 21:22:15 +090040 int maxAlternatives) {
satok18c28f42010-12-02 18:11:54 +090041 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok30088252010-12-01 21:22:15 +090042 maxWordLength, maxBigrams, maxAlternatives);
43 }
satok8fbd5522011-02-22 17:28:55 +090044
satoke808e432010-12-02 14:53:24 +090045 bool isValidWord(unsigned short *word, int length);
Ken Wakasae90b3332011-01-07 15:01:51 +090046 void *getDict() { return (void *)mDict; }
47 int getDictSize() { return mDictSize; }
48 int getMmapFd() { return mMmapFd; }
49 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070050 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070051
satoke808e432010-12-02 14:53:24 +090052 // public static utility methods
53 // static inline methods should be defined in the header file
54 static unsigned short getChar(const unsigned char *dict, int *pos);
55 static int getCount(const unsigned char *dict, int *pos);
56 static bool getTerminal(const unsigned char *dict, int *pos);
57 static int getAddress(const unsigned char *dict, int *pos);
58 static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
satok18c28f42010-12-02 18:11:54 +090059 static int wideStrLen(unsigned short *str);
satok48e432c2010-12-06 17:38:58 +090060 // returns next sibling's position
61 static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion,
62 const int pos, unsigned short *c, int *childrenPosition,
63 bool *terminal, int *freq);
satoke808e432010-12-02 14:53:24 +090064
Jean Chalard581335c2011-06-17 12:45:17 +090065 // TODO: delete this
66 int getBigramPosition(unsigned short *word, int length);
67
The Android Open Source Project923bf412009-03-13 15:11:42 -070068private:
satoke808e432010-12-02 14:53:24 +090069 bool hasBigram();
70
Ken Wakasae90b3332011-01-07 15:01:51 +090071 const unsigned char *mDict;
72
73 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
74 // also for the malloc version.
75 const int mDictSize;
76 const int mMmapFd;
77 const int mDictBufAdjust;
78
satoke808e432010-12-02 14:53:24 +090079 const bool IS_LATEST_DICT_VERSION;
satok30088252010-12-01 21:22:15 +090080 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090081 BigramDictionary *mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070082};
83
84// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090085// public static utility methods
86// static inline methods should be defined in the header file
87inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
88 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
89 // If the code is 255, then actual 16 bit code follows (in big endian)
90 if (ch == 0xFF) {
91 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
92 (*pos) += 2;
93 }
94 return ch;
95}
96
97inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
98 return dict[(*pos)++] & 0xFF;
99}
100
101inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
102 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
103}
104
105inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
106 int address = 0;
107 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
108 *pos += 1;
109 } else {
110 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
111 address += (dict[*pos + 1] & 0xFF) << 8;
112 address += (dict[*pos + 2] & 0xFF);
113 *pos += 3;
114 }
115 return address;
116}
117
118inline int Dictionary::getFreq(const unsigned char *dict,
119 const bool isLatestDictVersion, int *pos) {
120 int freq = dict[(*pos)++] & 0xFF;
121 if (isLatestDictVersion) {
122 // skipping bigram
123 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
124 if (bigramExist > 0) {
125 int nextBigramExist = 1;
126 while (nextBigramExist > 0) {
127 (*pos) += 3;
128 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
129 }
130 } else {
131 (*pos)++;
132 }
133 }
134 return freq;
135}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700136
satok18c28f42010-12-02 18:11:54 +0900137
138inline int Dictionary::wideStrLen(unsigned short *str) {
139 if (!str) return 0;
140 unsigned short *end = str;
141 while (*end)
142 end++;
143 return end - str;
144}
145
satok48e432c2010-12-06 17:38:58 +0900146inline int Dictionary::setDictionaryValues(const unsigned char *dict,
147 const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition,
148 bool *terminal, int *freq) {
149 int position = pos;
150 // -- at char
151 *c = Dictionary::getChar(dict, &position);
152 // -- at flag/add
153 *terminal = Dictionary::getTerminal(dict, &position);
154 *childrenPosition = Dictionary::getAddress(dict, &position);
155 // -- after address or flag
156 *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1;
157 // returns next sibling's position
158 return position;
159}
160
The Android Open Source Project923bf412009-03-13 15:11:42 -0700161}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700162#endif // LATINIME_DICTIONARY_H