blob: 79d377a4fdd24cbc7cd868c67dac51124b079fa7 [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"
satok1147c7b2011-12-14 15:04:58 +090022#include "correction.h"
satoke808e432010-12-02 14:53:24 +090023#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090024#include "proximity_info.h"
satok30088252010-12-01 21:22:15 +090025#include "unigram_dictionary.h"
satoka7e5a5a2011-12-15 16:49:12 +090026#include "words_priority_queue_pool.h"
satok30088252010-12-01 21:22:15 +090027
The Android Open Source Project923bf412009-03-13 15:11:42 -070028namespace latinime {
29
30class Dictionary {
Ken Wakasae12e9b52012-01-06 12:24:38 +090031 public:
Ken Wakasae90b3332011-01-07 15:01:51 +090032 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
33 int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
satok1147c7b2011-12-14 15:04:58 +090034
satok8fbd5522011-02-22 17:28:55 +090035 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090036 int *codes, int codesSize, int flags, unsigned short *outWords, int *frequencies) {
satoka7e5a5a2011-12-15 16:49:12 +090037 return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool,
satok1147c7b2011-12-14 15:04:58 +090038 mCorrection, xcoordinates, ycoordinates, codes,
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090039 codesSize, flags, outWords, frequencies);
satok30088252010-12-01 21:22:15 +090040 }
41
42 // TODO: Call mBigramDictionary instead of mUnigramDictionary
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070043 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
44 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
satok30088252010-12-01 21:22:15 +090045 int maxAlternatives) {
satok18c28f42010-12-02 18:11:54 +090046 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok30088252010-12-01 21:22:15 +090047 maxWordLength, maxBigrams, maxAlternatives);
48 }
satok8fbd5522011-02-22 17:28:55 +090049
satoke808e432010-12-02 14:53:24 +090050 bool isValidWord(unsigned short *word, int length);
Ken Wakasae90b3332011-01-07 15:01:51 +090051 void *getDict() { return (void *)mDict; }
52 int getDictSize() { return mDictSize; }
53 int getMmapFd() { return mMmapFd; }
54 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070055 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070056
satoke808e432010-12-02 14:53:24 +090057 // public static utility methods
58 // static inline methods should be defined in the header file
59 static unsigned short getChar(const unsigned char *dict, int *pos);
60 static int getCount(const unsigned char *dict, int *pos);
61 static bool getTerminal(const unsigned char *dict, int *pos);
62 static int getAddress(const unsigned char *dict, int *pos);
63 static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
satok18c28f42010-12-02 18:11:54 +090064 static int wideStrLen(unsigned short *str);
satok48e432c2010-12-06 17:38:58 +090065 // returns next sibling's position
66 static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion,
67 const int pos, unsigned short *c, int *childrenPosition,
68 bool *terminal, int *freq);
Jean Chalard581335c2011-06-17 12:45:17 +090069
Ken Wakasae12e9b52012-01-06 12:24:38 +090070 private:
satoke808e432010-12-02 14:53:24 +090071 bool hasBigram();
72
Ken Wakasae90b3332011-01-07 15:01:51 +090073 const unsigned char *mDict;
74
75 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
76 // also for the malloc version.
77 const int mDictSize;
78 const int mMmapFd;
79 const int mDictBufAdjust;
80
satoke808e432010-12-02 14:53:24 +090081 const bool IS_LATEST_DICT_VERSION;
satok30088252010-12-01 21:22:15 +090082 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090083 BigramDictionary *mBigramDictionary;
satoka7e5a5a2011-12-15 16:49:12 +090084 WordsPriorityQueuePool *mWordsPriorityQueuePool;
satok1147c7b2011-12-14 15:04:58 +090085 Correction *mCorrection;
The Android Open Source Project923bf412009-03-13 15:11:42 -070086};
87
satoke808e432010-12-02 14:53:24 +090088// public static utility methods
89// static inline methods should be defined in the header file
90inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
91 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
92 // If the code is 255, then actual 16 bit code follows (in big endian)
93 if (ch == 0xFF) {
94 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
95 (*pos) += 2;
96 }
97 return ch;
98}
99
100inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
101 return dict[(*pos)++] & 0xFF;
102}
103
104inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
105 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
106}
107
108inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
109 int address = 0;
110 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
111 *pos += 1;
112 } else {
113 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
114 address += (dict[*pos + 1] & 0xFF) << 8;
115 address += (dict[*pos + 2] & 0xFF);
116 *pos += 3;
117 }
118 return address;
119}
120
121inline int Dictionary::getFreq(const unsigned char *dict,
122 const bool isLatestDictVersion, int *pos) {
123 int freq = dict[(*pos)++] & 0xFF;
124 if (isLatestDictVersion) {
125 // skipping bigram
126 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
127 if (bigramExist > 0) {
128 int nextBigramExist = 1;
129 while (nextBigramExist > 0) {
130 (*pos) += 3;
131 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
132 }
133 } else {
134 (*pos)++;
135 }
136 }
137 return freq;
138}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700139
satok18c28f42010-12-02 18:11:54 +0900140inline int Dictionary::wideStrLen(unsigned short *str) {
141 if (!str) return 0;
142 unsigned short *end = str;
143 while (*end)
144 end++;
145 return end - str;
146}
147
satok48e432c2010-12-06 17:38:58 +0900148inline int Dictionary::setDictionaryValues(const unsigned char *dict,
149 const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition,
150 bool *terminal, int *freq) {
151 int position = pos;
152 // -- at char
153 *c = Dictionary::getChar(dict, &position);
154 // -- at flag/add
155 *terminal = Dictionary::getTerminal(dict, &position);
156 *childrenPosition = Dictionary::getAddress(dict, &position);
157 // -- after address or flag
158 *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1;
159 // returns next sibling's position
160 return position;
161}
162
Ken Wakasace9e52a2011-06-18 13:09:55 +0900163} // namespace latinime
164
The Android Open Source Project923bf412009-03-13 15:11:42 -0700165#endif // LATINIME_DICTIONARY_H