blob: cef1cf9ebda30d589d3e62f89550c6695d3e888e [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"
satok30088252010-12-01 21:22:15 +090022#include "unigram_dictionary.h"
23
The Android Open Source Project923bf412009-03-13 15:11:42 -070024namespace latinime {
25
26class Dictionary {
27public:
Ken Wakasae90b3332011-01-07 15:01:51 +090028 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
29 int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070030 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
satok30088252010-12-01 21:22:15 +090031 int *nextLetters, int nextLettersSize) {
32 return mUnigramDictionary->getSuggestions(codes, codesSize, outWords, frequencies,
33 nextLetters, nextLettersSize);
34 }
35
36 // TODO: Call mBigramDictionary instead of mUnigramDictionary
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070037 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
38 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
satok30088252010-12-01 21:22:15 +090039 int maxAlternatives) {
satok18c28f42010-12-02 18:11:54 +090040 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok30088252010-12-01 21:22:15 +090041 maxWordLength, maxBigrams, maxAlternatives);
42 }
satoke808e432010-12-02 14:53:24 +090043 bool isValidWord(unsigned short *word, int length);
44 int isValidWordRec(int pos, unsigned short *word, int offset, int length);
Ken Wakasae90b3332011-01-07 15:01:51 +090045 void *getDict() { return (void *)mDict; }
46 int getDictSize() { return mDictSize; }
47 int getMmapFd() { return mMmapFd; }
48 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070049 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070050
satoke808e432010-12-02 14:53:24 +090051 // public static utility methods
52 // static inline methods should be defined in the header file
53 static unsigned short getChar(const unsigned char *dict, int *pos);
54 static int getCount(const unsigned char *dict, int *pos);
55 static bool getTerminal(const unsigned char *dict, int *pos);
56 static int getAddress(const unsigned char *dict, int *pos);
57 static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
satok18c28f42010-12-02 18:11:54 +090058 static int wideStrLen(unsigned short *str);
satok48e432c2010-12-06 17:38:58 +090059 // returns next sibling's position
60 static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion,
61 const int pos, unsigned short *c, int *childrenPosition,
62 bool *terminal, int *freq);
satoke808e432010-12-02 14:53:24 +090063
The Android Open Source Project923bf412009-03-13 15:11:42 -070064private:
satoke808e432010-12-02 14:53:24 +090065 bool hasBigram();
66
Ken Wakasae90b3332011-01-07 15:01:51 +090067 const unsigned char *mDict;
68
69 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
70 // also for the malloc version.
71 const int mDictSize;
72 const int mMmapFd;
73 const int mDictBufAdjust;
74
satoke808e432010-12-02 14:53:24 +090075 const bool IS_LATEST_DICT_VERSION;
satok30088252010-12-01 21:22:15 +090076 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090077 BigramDictionary *mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070078};
79
80// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090081// public static utility methods
82// static inline methods should be defined in the header file
83inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
84 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
85 // If the code is 255, then actual 16 bit code follows (in big endian)
86 if (ch == 0xFF) {
87 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
88 (*pos) += 2;
89 }
90 return ch;
91}
92
93inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
94 return dict[(*pos)++] & 0xFF;
95}
96
97inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
98 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
99}
100
101inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
102 int address = 0;
103 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
104 *pos += 1;
105 } else {
106 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
107 address += (dict[*pos + 1] & 0xFF) << 8;
108 address += (dict[*pos + 2] & 0xFF);
109 *pos += 3;
110 }
111 return address;
112}
113
114inline int Dictionary::getFreq(const unsigned char *dict,
115 const bool isLatestDictVersion, int *pos) {
116 int freq = dict[(*pos)++] & 0xFF;
117 if (isLatestDictVersion) {
118 // skipping bigram
119 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
120 if (bigramExist > 0) {
121 int nextBigramExist = 1;
122 while (nextBigramExist > 0) {
123 (*pos) += 3;
124 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
125 }
126 } else {
127 (*pos)++;
128 }
129 }
130 return freq;
131}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700132
satok18c28f42010-12-02 18:11:54 +0900133
134inline 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
The Android Open Source Project923bf412009-03-13 15:11:42 -0700157}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700158#endif // LATINIME_DICTIONARY_H