blob: f891e7457b295b2d8bde01a4b3c5faec137eeea5 [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"
satokd24df432011-07-14 15:43:42 +090021#include "char_utils.h"
satoke808e432010-12-02 14:53:24 +090022#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090023#include "proximity_info.h"
satok30088252010-12-01 21:22:15 +090024#include "unigram_dictionary.h"
25
The Android Open Source Project923bf412009-03-13 15:11:42 -070026namespace latinime {
27
28class Dictionary {
29public:
Ken Wakasae90b3332011-01-07 15:01:51 +090030 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
31 int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
satok8fbd5522011-02-22 17:28:55 +090032 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090033 int *codes, int codesSize, int flags, unsigned short *outWords, int *frequencies) {
satok8fbd5522011-02-22 17:28:55 +090034 return mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, codes,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090035 codesSize, flags, outWords, frequencies);
satok30088252010-12-01 21:22:15 +090036 }
37
38 // TODO: Call mBigramDictionary instead of mUnigramDictionary
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070039 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
40 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
satok30088252010-12-01 21:22:15 +090041 int maxAlternatives) {
satok18c28f42010-12-02 18:11:54 +090042 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok30088252010-12-01 21:22:15 +090043 maxWordLength, maxBigrams, maxAlternatives);
44 }
satok8fbd5522011-02-22 17:28:55 +090045
satoke808e432010-12-02 14:53:24 +090046 bool isValidWord(unsigned short *word, int length);
Ken Wakasae90b3332011-01-07 15:01:51 +090047 void *getDict() { return (void *)mDict; }
48 int getDictSize() { return mDictSize; }
49 int getMmapFd() { return mMmapFd; }
50 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070051 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070052
satoke808e432010-12-02 14:53:24 +090053 // public static utility methods
54 // static inline methods should be defined in the header file
55 static unsigned short getChar(const unsigned char *dict, int *pos);
56 static int getCount(const unsigned char *dict, int *pos);
57 static bool getTerminal(const unsigned char *dict, int *pos);
58 static int getAddress(const unsigned char *dict, int *pos);
59 static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
satok18c28f42010-12-02 18:11:54 +090060 static int wideStrLen(unsigned short *str);
satok48e432c2010-12-06 17:38:58 +090061 // returns next sibling's position
62 static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion,
63 const int pos, unsigned short *c, int *childrenPosition,
64 bool *terminal, int *freq);
Jean Chalard581335c2011-06-17 12:45:17 +090065
The Android Open Source Project923bf412009-03-13 15:11:42 -070066private:
satoke808e432010-12-02 14:53:24 +090067 bool hasBigram();
68
Ken Wakasae90b3332011-01-07 15:01:51 +090069 const unsigned char *mDict;
70
71 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
72 // also for the malloc version.
73 const int mDictSize;
74 const int mMmapFd;
75 const int mDictBufAdjust;
76
satoke808e432010-12-02 14:53:24 +090077 const bool IS_LATEST_DICT_VERSION;
satok30088252010-12-01 21:22:15 +090078 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090079 BigramDictionary *mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070080};
81
satoke808e432010-12-02 14:53:24 +090082// public static utility methods
83// static inline methods should be defined in the header file
84inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
85 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
86 // If the code is 255, then actual 16 bit code follows (in big endian)
87 if (ch == 0xFF) {
88 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
89 (*pos) += 2;
90 }
91 return ch;
92}
93
94inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
95 return dict[(*pos)++] & 0xFF;
96}
97
98inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
99 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
100}
101
102inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
103 int address = 0;
104 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
105 *pos += 1;
106 } else {
107 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
108 address += (dict[*pos + 1] & 0xFF) << 8;
109 address += (dict[*pos + 2] & 0xFF);
110 *pos += 3;
111 }
112 return address;
113}
114
115inline int Dictionary::getFreq(const unsigned char *dict,
116 const bool isLatestDictVersion, int *pos) {
117 int freq = dict[(*pos)++] & 0xFF;
118 if (isLatestDictVersion) {
119 // skipping bigram
120 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
121 if (bigramExist > 0) {
122 int nextBigramExist = 1;
123 while (nextBigramExist > 0) {
124 (*pos) += 3;
125 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
126 }
127 } else {
128 (*pos)++;
129 }
130 }
131 return freq;
132}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700133
satok18c28f42010-12-02 18:11:54 +0900134inline int Dictionary::wideStrLen(unsigned short *str) {
135 if (!str) return 0;
136 unsigned short *end = str;
137 while (*end)
138 end++;
139 return end - str;
140}
141
satok48e432c2010-12-06 17:38:58 +0900142inline int Dictionary::setDictionaryValues(const unsigned char *dict,
143 const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition,
144 bool *terminal, int *freq) {
145 int position = pos;
146 // -- at char
147 *c = Dictionary::getChar(dict, &position);
148 // -- at flag/add
149 *terminal = Dictionary::getTerminal(dict, &position);
150 *childrenPosition = Dictionary::getAddress(dict, &position);
151 // -- after address or flag
152 *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1;
153 // returns next sibling's position
154 return position;
155}
156
Ken Wakasace9e52a2011-06-18 13:09:55 +0900157} // namespace latinime
158
The Android Open Source Project923bf412009-03-13 15:11:42 -0700159#endif // LATINIME_DICTIONARY_H