blob: 1cd517cfb9b4e503e2f88cb46ccf9c20160e3fdf [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);
satoke808e432010-12-02 14:53:24 +090057
The Android Open Source Project923bf412009-03-13 15:11:42 -070058private:
satoke808e432010-12-02 14:53:24 +090059 bool hasBigram();
60
61 const unsigned char *DICT;
62 const bool IS_LATEST_DICT_VERSION;
The Android Open Source Project923bf412009-03-13 15:11:42 -070063 void *mAsset;
satok30088252010-12-01 21:22:15 +090064 BigramDictionary *mBigramDictionary;
65 UnigramDictionary *mUnigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070066};
67
68// ----------------------------------------------------------------------------
satoke808e432010-12-02 14:53:24 +090069// public static utility methods
70// static inline methods should be defined in the header file
71inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
72 unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
73 // If the code is 255, then actual 16 bit code follows (in big endian)
74 if (ch == 0xFF) {
75 ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
76 (*pos) += 2;
77 }
78 return ch;
79}
80
81inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
82 return dict[(*pos)++] & 0xFF;
83}
84
85inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
86 return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
87}
88
89inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
90 int address = 0;
91 if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
92 *pos += 1;
93 } else {
94 address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
95 address += (dict[*pos + 1] & 0xFF) << 8;
96 address += (dict[*pos + 2] & 0xFF);
97 *pos += 3;
98 }
99 return address;
100}
101
102inline int Dictionary::getFreq(const unsigned char *dict,
103 const bool isLatestDictVersion, int *pos) {
104 int freq = dict[(*pos)++] & 0xFF;
105 if (isLatestDictVersion) {
106 // skipping bigram
107 int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
108 if (bigramExist > 0) {
109 int nextBigramExist = 1;
110 while (nextBigramExist > 0) {
111 (*pos) += 3;
112 nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
113 }
114 } else {
115 (*pos)++;
116 }
117 }
118 return freq;
119}
The Android Open Source Project923bf412009-03-13 15:11:42 -0700120
satok18c28f42010-12-02 18:11:54 +0900121
122inline int Dictionary::wideStrLen(unsigned short *str) {
123 if (!str) return 0;
124 unsigned short *end = str;
125 while (*end)
126 end++;
127 return end - str;
128}
129
The Android Open Source Project923bf412009-03-13 15:11:42 -0700130}; // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700131#endif // LATINIME_DICTIONARY_H