blob: 8d3290945a486573dfa526c067c13eff1a471e71 [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
The Android Open Source Project923bf412009-03-13 15:11:42 -070019
satoke808e432010-12-02 14:53:24 +090020#define LOG_TAG "LatinIME: dictionary.cpp"
21
The Android Open Source Project923bf412009-03-13 15:11:42 -070022#include "dictionary.h"
satokd4952c82010-12-01 19:09:29 +090023
The Android Open Source Project923bf412009-03-13 15:11:42 -070024namespace latinime {
25
satok30088252010-12-01 21:22:15 +090026Dictionary::Dictionary(void *dict, int typedLetterMultiplier, int fullWordMultiplier,
27 int maxWordLength, int maxWords, int maxAlternatives)
satoke808e432010-12-02 14:53:24 +090028 : DICT((unsigned char*) dict),
29 // Checks whether it has the latest dictionary or the old dictionary
satok662fe692010-12-08 17:05:39 +090030 IS_LATEST_DICT_VERSION((((unsigned char*) dict)[0] & 0xFF) >= DICTIONARY_VERSION_MIN) {
31 if (DEBUG_DICT) {
32 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
33 LOGI("Max word length (%d) is greater than %d",
34 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
35 LOGI("IN NATIVE SUGGEST Version: %d \n", (DICT[0] & 0xFF));
36 }
satok715514d2010-12-02 20:19:59 +090037 }
satoke808e432010-12-02 14:53:24 +090038 mUnigramDictionary = new UnigramDictionary(DICT, typedLetterMultiplier, fullWordMultiplier,
satok18c28f42010-12-02 18:11:54 +090039 maxWordLength, maxWords, maxAlternatives, IS_LATEST_DICT_VERSION);
40 mBigramDictionary = new BigramDictionary(DICT, maxWordLength, maxAlternatives,
41 IS_LATEST_DICT_VERSION, hasBigram(), this);
The Android Open Source Project923bf412009-03-13 15:11:42 -070042}
43
satok662fe692010-12-08 17:05:39 +090044Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090045 delete mUnigramDictionary;
46 delete mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070047}
satoke808e432010-12-02 14:53:24 +090048
49bool Dictionary::hasBigram() {
50 return ((DICT[1] & 0xFF) == 1);
51}
52
53// TODO: use uint16_t instead of unsigned short
54bool Dictionary::isValidWord(unsigned short *word, int length)
55{
56 if (IS_LATEST_DICT_VERSION) {
57 return (isValidWordRec(DICTIONARY_HEADER_SIZE, word, 0, length) != NOT_VALID_WORD);
58 } else {
59 return (isValidWordRec(0, word, 0, length) != NOT_VALID_WORD);
60 }
61}
62
63int Dictionary::isValidWordRec(int pos, unsigned short *word, int offset, int length) {
64 // returns address of bigram data of that word
65 // return -99 if not found
66
67 int count = Dictionary::getCount(DICT, &pos);
68 unsigned short currentChar = (unsigned short) word[offset];
69 for (int j = 0; j < count; j++) {
70 unsigned short c = Dictionary::getChar(DICT, &pos);
71 int terminal = Dictionary::getTerminal(DICT, &pos);
72 int childPos = Dictionary::getAddress(DICT, &pos);
73 if (c == currentChar) {
74 if (offset == length - 1) {
75 if (terminal) {
76 return (pos+1);
77 }
78 } else {
79 if (childPos != 0) {
80 int t = isValidWordRec(childPos, word, offset + 1, length);
81 if (t > 0) {
82 return t;
83 }
84 }
85 }
86 }
87 if (terminal) {
88 Dictionary::getFreq(DICT, IS_LATEST_DICT_VERSION, &pos);
89 }
90 // There could be two instances of each alphabet - upper and lower case. So continue
91 // looking ...
92 }
93 return NOT_VALID_WORD;
94}
The Android Open Source Project923bf412009-03-13 15:11:42 -070095} // namespace latinime