2009-03-13 22:11:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LATINIME_DICTIONARY_H
|
|
|
|
#define LATINIME_DICTIONARY_H
|
|
|
|
|
2010-12-01 12:22:15 +00:00
|
|
|
#include "bigram_dictionary.h"
|
2010-12-02 05:53:24 +00:00
|
|
|
#include "defines.h"
|
2010-12-01 12:22:15 +00:00
|
|
|
#include "unigram_dictionary.h"
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
namespace latinime {
|
|
|
|
|
|
|
|
class Dictionary {
|
|
|
|
public:
|
2010-12-01 12:22:15 +00:00
|
|
|
Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier, int maxWordLength,
|
|
|
|
int maxWords, int maxAlternatives);
|
2009-03-31 17:51:17 +00:00
|
|
|
int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
|
2010-12-01 12:22:15 +00:00
|
|
|
int *nextLetters, int nextLettersSize) {
|
|
|
|
return mUnigramDictionary->getSuggestions(codes, codesSize, outWords, frequencies,
|
|
|
|
nextLetters, nextLettersSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Call mBigramDictionary instead of mUnigramDictionary
|
2010-07-26 18:43:29 +00:00
|
|
|
int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
|
|
|
|
unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
|
2010-12-01 12:22:15 +00:00
|
|
|
int maxAlternatives) {
|
2010-12-02 09:11:54 +00:00
|
|
|
return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
|
2010-12-01 12:22:15 +00:00
|
|
|
maxWordLength, maxBigrams, maxAlternatives);
|
|
|
|
}
|
2010-12-02 05:53:24 +00:00
|
|
|
bool isValidWord(unsigned short *word, int length);
|
|
|
|
int isValidWordRec(int pos, unsigned short *word, int offset, int length);
|
2009-03-13 22:11:42 +00:00
|
|
|
void setAsset(void *asset) { mAsset = asset; }
|
|
|
|
void *getAsset() { return mAsset; }
|
|
|
|
~Dictionary();
|
2009-03-31 17:51:17 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
// public static utility methods
|
|
|
|
// static inline methods should be defined in the header file
|
|
|
|
static unsigned short getChar(const unsigned char *dict, int *pos);
|
|
|
|
static int getCount(const unsigned char *dict, int *pos);
|
|
|
|
static bool getTerminal(const unsigned char *dict, int *pos);
|
|
|
|
static int getAddress(const unsigned char *dict, int *pos);
|
|
|
|
static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos);
|
2010-12-02 09:11:54 +00:00
|
|
|
static int wideStrLen(unsigned short *str);
|
2010-12-02 05:53:24 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private:
|
2010-12-02 05:53:24 +00:00
|
|
|
bool hasBigram();
|
|
|
|
|
|
|
|
const unsigned char *DICT;
|
|
|
|
const bool IS_LATEST_DICT_VERSION;
|
2009-03-13 22:11:42 +00:00
|
|
|
void *mAsset;
|
2010-12-01 12:22:15 +00:00
|
|
|
BigramDictionary *mBigramDictionary;
|
|
|
|
UnigramDictionary *mUnigramDictionary;
|
2009-03-13 22:11:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2010-12-02 05:53:24 +00:00
|
|
|
// public static utility methods
|
|
|
|
// static inline methods should be defined in the header file
|
|
|
|
inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) {
|
|
|
|
unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF);
|
|
|
|
// If the code is 255, then actual 16 bit code follows (in big endian)
|
|
|
|
if (ch == 0xFF) {
|
|
|
|
ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF);
|
|
|
|
(*pos) += 2;
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
inline int Dictionary::getCount(const unsigned char *dict, int *pos) {
|
|
|
|
return dict[(*pos)++] & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) {
|
|
|
|
return (dict[*pos] & FLAG_TERMINAL_MASK) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int Dictionary::getAddress(const unsigned char *dict, int *pos) {
|
|
|
|
int address = 0;
|
|
|
|
if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) {
|
|
|
|
*pos += 1;
|
|
|
|
} else {
|
|
|
|
address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16;
|
|
|
|
address += (dict[*pos + 1] & 0xFF) << 8;
|
|
|
|
address += (dict[*pos + 2] & 0xFF);
|
|
|
|
*pos += 3;
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
inline int Dictionary::getFreq(const unsigned char *dict,
|
|
|
|
const bool isLatestDictVersion, int *pos) {
|
|
|
|
int freq = dict[(*pos)++] & 0xFF;
|
|
|
|
if (isLatestDictVersion) {
|
|
|
|
// skipping bigram
|
|
|
|
int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ);
|
|
|
|
if (bigramExist > 0) {
|
|
|
|
int nextBigramExist = 1;
|
|
|
|
while (nextBigramExist > 0) {
|
|
|
|
(*pos) += 3;
|
|
|
|
nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*pos)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:11:54 +00:00
|
|
|
|
|
|
|
inline int Dictionary::wideStrLen(unsigned short *str) {
|
|
|
|
if (!str) return 0;
|
|
|
|
unsigned short *end = str;
|
|
|
|
while (*end)
|
|
|
|
end++;
|
|
|
|
return end - str;
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:53:24 +00:00
|
|
|
}; // namespace latinime
|
2009-03-13 22:11:42 +00:00
|
|
|
#endif // LATINIME_DICTIONARY_H
|