blob: 9f23679044ae44ed10dfdf50525ca41569d241f0 [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
Jean Chalard1ff8dc42012-05-02 16:00:24 +090020#include <map>
21
satok30088252010-12-01 21:22:15 +090022#include "bigram_dictionary.h"
satokd24df432011-07-14 15:43:42 +090023#include "char_utils.h"
satok1147c7b2011-12-14 15:04:58 +090024#include "correction.h"
satoke808e432010-12-02 14:53:24 +090025#include "defines.h"
satok8fbd5522011-02-22 17:28:55 +090026#include "proximity_info.h"
satok30088252010-12-01 21:22:15 +090027#include "unigram_dictionary.h"
satoka7e5a5a2011-12-15 16:49:12 +090028#include "words_priority_queue_pool.h"
satok30088252010-12-01 21:22:15 +090029
The Android Open Source Project923bf412009-03-13 15:11:42 -070030namespace latinime {
31
32class Dictionary {
Ken Wakasae12e9b52012-01-06 12:24:38 +090033 public:
Ken Wakasae90b3332011-01-07 15:01:51 +090034 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
satok6ba8de22012-03-28 18:21:04 +090035 int fullWordMultiplier, int maxWordLength, int maxWords);
satok1147c7b2011-12-14 15:04:58 +090036
satok8fbd5522011-02-22 17:28:55 +090037 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
Jean Chalard351864b2012-04-24 18:06:51 +090038 int *codes, int codesSize, const int32_t* prevWordChars, const int prevWordLength,
39 bool useFullEditDistance, unsigned short *outWords, int *frequencies) {
Jean Chalard1ff8dc42012-05-02 16:00:24 +090040 std::map<int, int> bigramMap;
Jean Chalardf1634c82012-05-02 19:05:27 +090041 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
42 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
43 prevWordLength, &bigramMap, bigramFilter);
satoka7e5a5a2011-12-15 16:49:12 +090044 return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool,
Jean Chalard8950ce62012-05-07 16:28:30 +090045 mCorrection, xcoordinates, ycoordinates, codes, codesSize, &bigramMap,
46 bigramFilter, useFullEditDistance, outWords, frequencies);
satok30088252010-12-01 21:22:15 +090047 }
48
Jean Chalard522a04e2012-04-23 15:37:07 +090049 int getBigrams(const int32_t *word, int length, int *codes, int codesSize,
satok6ba8de22012-03-28 18:21:04 +090050 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams) {
satok18c28f42010-12-02 18:11:54 +090051 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
satok6ba8de22012-03-28 18:21:04 +090052 maxWordLength, maxBigrams);
satok30088252010-12-01 21:22:15 +090053 }
satok8fbd5522011-02-22 17:28:55 +090054
Satoshi Kataoka2f854e12012-05-29 15:58:13 +090055 int getFrequency(const int32_t *word, int length);
Tom Ouyang4d289d32012-04-26 23:50:21 -070056 bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2);
Ken Wakasae90b3332011-01-07 15:01:51 +090057 void *getDict() { return (void *)mDict; }
58 int getDictSize() { return mDictSize; }
59 int getMmapFd() { return mMmapFd; }
60 int getDictBufAdjust() { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070061 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070062
satoke808e432010-12-02 14:53:24 +090063 // public static utility methods
64 // static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +090065 static int wideStrLen(unsigned short *str);
Jean Chalard581335c2011-06-17 12:45:17 +090066
Ken Wakasae12e9b52012-01-06 12:24:38 +090067 private:
Ken Wakasae90b3332011-01-07 15:01:51 +090068 const unsigned char *mDict;
69
70 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
71 // also for the malloc version.
72 const int mDictSize;
73 const int mMmapFd;
74 const int mDictBufAdjust;
75
satok30088252010-12-01 21:22:15 +090076 UnigramDictionary *mUnigramDictionary;
Ken Wakasae90b3332011-01-07 15:01:51 +090077 BigramDictionary *mBigramDictionary;
satoka7e5a5a2011-12-15 16:49:12 +090078 WordsPriorityQueuePool *mWordsPriorityQueuePool;
satok1147c7b2011-12-14 15:04:58 +090079 Correction *mCorrection;
The Android Open Source Project923bf412009-03-13 15:11:42 -070080};
81
satoke808e432010-12-02 14:53:24 +090082// public static utility methods
83// static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +090084inline int Dictionary::wideStrLen(unsigned short *str) {
85 if (!str) return 0;
86 unsigned short *end = str;
87 while (*end)
88 end++;
89 return end - str;
90}
Ken Wakasace9e52a2011-06-18 13:09:55 +090091} // namespace latinime
92
The Android Open Source Project923bf412009-03-13 15:11:42 -070093#endif // LATINIME_DICTIONARY_H