blob: 941bd191af1853ba68a66fe8e9a1c9a3461fc971 [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);
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +090030 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies) {
31 return mUnigramDictionary->getSuggestions(codes, codesSize, outWords, frequencies);
satok30088252010-12-01 21:22:15 +090032 }
33
34 // TODO: Call mBigramDictionary instead of mUnigramDictionary
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070035 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
36 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
satok30088252010-12-01 21:22:15 +090037 int maxAlternatives) {
satok18c28f42010-12-02 18:11:54 +090038 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok30088252010-12-01 21:22:15 +090039 maxWordLength, maxBigrams, maxAlternatives);
40 }
satoke808e432010-12-02 14:53:24 +090041 bool isValidWord(unsigned short *word, int length);
42 int isValidWordRec(int pos, unsigned short *word, int offset, int length);
Ken Wakasae90b3332011-01-07 15:01:51 +090043 void *getDict() { return (void *)mDict; }
44 int getDictSize() { return mDictSize; }
45 int getMmapFd() { return mMmapFd; }
46 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070047 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070048
satoke808e432010-12-02 14:53:24 +090049 // public static utility methods
50 // static inline methods should be defined in the header file
51 static unsigned short getChar(const unsigned char *dict, int *pos);
52 static int getCount(const unsigned char *dict, int *pos);
53 static bool getTerminal(const unsigned char *dict, int *pos);
54 static int getAddress(const unsigned char *dict, int *pos);
55 static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
satok18c28f42010-12-02 18:11:54 +090056 static int wideStrLen(unsigned short *str);
satok48e432c2010-12-06 17:38:58 +090057 // returns next sibling's position
58 static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion,
59 const int pos, unsigned short *c, int *childrenPosition,
60 bool *terminal, int *freq);
satoke808e432010-12-02 14:53:24 +090061
The Android Open Source Project923bf412009-03-13 15:11:42 -070062private:
satoke808e432010-12-02 14:53:24 +090063 bool hasBigram();
64
Ken Wakasae90b3332011-01-07 15:01:51 +090065 const unsigned char *mDict;
66
67 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
68 // also for the malloc version.
69 const int mDictSize;
70 const int mMmapFd;
71 const int mDictBufAdjust;
72
satoke808e432010-12-02 14:53:24 +090073 const bool IS_LATEST_DICT_VERSION;
satok30088252010-12-01 21:22:15 +090074 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090075 BigramDictionary *mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070076};
77
78// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090079// public static utility methods
80// static inline methods should be defined in the header file
81inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
82 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
83 // If the code is 255, then actual 16 bit code follows (in big endian)
84 if (ch == 0xFF) {
85 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
86 (*pos) += 2;
87 }
88 return ch;
89}
90
91inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
92 return dict[(*pos)++] & 0xFF;
93}
94
95inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
96 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
97}
98
99inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
100 int address = 0;
101 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
102 *pos += 1;
103 } else {
104 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
105 address += (dict[*pos + 1] & 0xFF) << 8;
106 address += (dict[*pos + 2] & 0xFF);
107 *pos += 3;
108 }
109 return address;
110}
111
112inline int Dictionary::getFreq(const unsigned char *dict,
113 const bool isLatestDictVersion, int *pos) {
114 int freq = dict[(*pos)++] & 0xFF;
115 if (isLatestDictVersion) {
116 // skipping bigram
117 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
118 if (bigramExist > 0) {
119 int nextBigramExist = 1;
120 while (nextBigramExist > 0) {
121 (*pos) += 3;
122 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
123 }
124 } else {
125 (*pos)++;
126 }
127 }
128 return freq;
129}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700130
satok18c28f42010-12-02 18:11:54 +0900131
132inline int Dictionary::wideStrLen(unsigned short *str) {
133 if (!str) return 0;
134 unsigned short *end = str;
135 while (*end)
136 end++;
137 return end - str;
138}
139
satok48e432c2010-12-06 17:38:58 +0900140inline int Dictionary::setDictionaryValues(const unsigned char *dict,
141 const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition,
142 bool *terminal, int *freq) {
143 int position = pos;
144 // -- at char
145 *c = Dictionary::getChar(dict, &position);
146 // -- at flag/add
147 *terminal = Dictionary::getTerminal(dict, &position);
148 *childrenPosition = Dictionary::getAddress(dict, &position);
149 // -- after address or flag
150 *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1;
151 // returns next sibling's position
152 return position;
153}
154
The Android Open Source Project923bf412009-03-13 15:11:42 -0700155}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700156#endif // LATINIME_DICTIONARY_H