blob: 61f7cf07499683a0a079fce180ec63c35a3734d5 [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) {
40 return mUnigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
41 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);
56
The Android Open Source Project923bf412009-03-13 15:11:42 -070057private:
satoke808e432010-12-02 14:53:24 +090058 bool hasBigram();
59
60 const unsigned char *DICT;
61 const bool IS_LATEST_DICT_VERSION;
The Android Open Source Project923bf412009-03-13 15:11:42 -070062 void *mAsset;
satok30088252010-12-01 21:22:15 +090063 BigramDictionary *mBigramDictionary;
64 UnigramDictionary *mUnigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070065};
66
67// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090068// public static utility methods
69// static inline methods should be defined in the header file
70inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
71 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
72 // If the code is 255, then actual 16 bit code follows (in big endian)
73 if (ch == 0xFF) {
74 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
75 (*pos) += 2;
76 }
77 return ch;
78}
79
80inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
81 return dict[(*pos)++] & 0xFF;
82}
83
84inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
85 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
86}
87
88inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
89 int address = 0;
90 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
91 *pos += 1;
92 } else {
93 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
94 address += (dict[*pos + 1] & 0xFF) << 8;
95 address += (dict[*pos + 2] & 0xFF);
96 *pos += 3;
97 }
98 return address;
99}
100
101inline int Dictionary::getFreq(const unsigned char *dict,
102 const bool isLatestDictVersion, int *pos) {
103 int freq = dict[(*pos)++] & 0xFF;
104 if (isLatestDictVersion) {
105 // skipping bigram
106 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
107 if (bigramExist > 0) {
108 int nextBigramExist = 1;
109 while (nextBigramExist > 0) {
110 (*pos) += 3;
111 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
112 }
113 } else {
114 (*pos)++;
115 }
116 }
117 return freq;
118}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700119
120}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700121#endif // LATINIME_DICTIONARY_H