blob: da876242df0037b25a4d21acaac2cf618bd26e62 [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:
satok30088252010-12-01 21:22:15 +090028 Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier, int maxWordLength,
29 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);
The Android Open Source Project923bf412009-03-13 15:11:42 -070045 void setAsset(void *asset) { mAsset = asset; }
46 void *getAsset() { return mAsset; }
47 ~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
65 const unsigned char *DICT;
66 const bool IS_LATEST_DICT_VERSION;
The Android Open Source Project923bf412009-03-13 15:11:42 -070067 void *mAsset;
satok30088252010-12-01 21:22:15 +090068 BigramDictionary *mBigramDictionary;
69 UnigramDictionary *mUnigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070070};
71
72// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090073// public static utility methods
74// static inline methods should be defined in the header file
75inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
76 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
77 // If the code is 255, then actual 16 bit code follows (in big endian)
78 if (ch == 0xFF) {
79 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
80 (*pos) += 2;
81 }
82 return ch;
83}
84
85inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
86 return dict[(*pos)++] & 0xFF;
87}
88
89inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
90 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
91}
92
93inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
94 int address = 0;
95 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
96 *pos += 1;
97 } else {
98 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
99 address += (dict[*pos + 1] & 0xFF) << 8;
100 address += (dict[*pos + 2] & 0xFF);
101 *pos += 3;
102 }
103 return address;
104}
105
106inline int Dictionary::getFreq(const unsigned char *dict,
107 const bool isLatestDictVersion, int *pos) {
108 int freq = dict[(*pos)++] & 0xFF;
109 if (isLatestDictVersion) {
110 // skipping bigram
111 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
112 if (bigramExist > 0) {
113 int nextBigramExist = 1;
114 while (nextBigramExist > 0) {
115 (*pos) += 3;
116 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
117 }
118 } else {
119 (*pos)++;
120 }
121 }
122 return freq;
123}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700124
satok18c28f42010-12-02 18:11:54 +0900125
126inline int Dictionary::wideStrLen(unsigned short *str) {
127 if (!str) return 0;
128 unsigned short *end = str;
129 while (*end)
130 end++;
131 return end - str;
132}
133
satok48e432c2010-12-06 17:38:58 +0900134inline int Dictionary::setDictionaryValues(const unsigned char *dict,
135 const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition,
136 bool *terminal, int *freq) {
137 int position = pos;
138 // -- at char
139 *c = Dictionary::getChar(dict, &position);
140 // -- at flag/add
141 *terminal = Dictionary::getTerminal(dict, &position);
142 *childrenPosition = Dictionary::getAddress(dict, &position);
143 // -- after address or flag
144 *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1;
145 // returns next sibling's position
146 return position;
147}
148
The Android Open Source Project923bf412009-03-13 15:11:42 -0700149}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700150#endif // LATINIME_DICTIONARY_H