blob: f625813a6261787cc83e94e5e9cd7308e7d6efa1 [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"
satoke808e432010-12-02 14:53:24 +090024#include "defines.h"
Ken Wakasa8658e552012-06-30 08:53:33 +090025#include "incremental_decoder_interface.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:
Jean Chalardc7387a42012-07-12 15:21:11 +090034 // Taken from SuggestedWords.java
35 const static int KIND_TYPED = 0; // What user typed
36 const static int KIND_CORRECTION = 1; // Simple correction/suggestion
37 const static int KIND_COMPLETION = 2; // Completion (suggestion with appended chars)
38 const static int KIND_WHITELIST = 3; // Whitelisted word
39 const static int KIND_BLACKLIST = 4; // Blacklisted word
40 const static int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuation
41 const static int KIND_APP_DEFINED = 6; // Suggested by the application
42 const static int KIND_SHORTCUT = 7; // A shortcut
43 const static int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input)
44
Ken Wakasae90b3332011-01-07 15:01:51 +090045 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090046 int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions);
satok1147c7b2011-12-14 15:04:58 +090047
satok8fbd5522011-02-22 17:28:55 +090048 int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
Satoshi Kataoka73680092012-06-25 17:44:54 +090049 int *times, int *pointerIds, int *codes, int codesSize, int *prevWordChars,
Jean Chalard05efe572012-06-27 17:31:09 +090050 int prevWordLength, int commitPoint, bool isGesture,
Satoshi Kataoka73680092012-06-25 17:44:54 +090051 bool useFullEditDistance, unsigned short *outWords,
Jean Chalard6931df92012-07-12 12:55:48 +090052 int *frequencies, int *spaceIndices, int *outputTypes) {
Satoshi Kataoka73680092012-06-25 17:44:54 +090053 int result = 0;
Satoshi Kataoka91eb4d82012-06-26 16:39:07 +090054 if (isGesture) {
55 mGestureDecoder->setPrevWord(prevWordChars, prevWordLength);
56 result = mGestureDecoder->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
Jean Chalard05efe572012-06-27 17:31:09 +090057 times, pointerIds, codes, codesSize, commitPoint,
Jean Chalard6931df92012-07-12 12:55:48 +090058 outWords, frequencies, spaceIndices, outputTypes);
Satoshi Kataokadeb09872012-07-03 17:45:50 +090059 return result;
Satoshi Kataoka91eb4d82012-06-26 16:39:07 +090060 } else {
61 std::map<int, int> bigramMap;
62 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
63 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
64 prevWordLength, &bigramMap, bigramFilter);
65 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates,
66 ycoordinates, codes, codesSize, &bigramMap, bigramFilter,
Jean Chalard6931df92012-07-12 12:55:48 +090067 useFullEditDistance, outWords, frequencies, outputTypes);
Satoshi Kataokadeb09872012-07-03 17:45:50 +090068 return result;
Satoshi Kataoka91eb4d82012-06-26 16:39:07 +090069 }
satok30088252010-12-01 21:22:15 +090070 }
71
Jean Chalard522a04e2012-04-23 15:37:07 +090072 int getBigrams(const int32_t *word, int length, int *codes, int codesSize,
Jean Chalard6931df92012-07-12 12:55:48 +090073 unsigned short *outWords, int *frequencies, int *outputTypes) const {
Jean Chalard3b576312012-07-11 16:47:22 +090074 if (length <= 0) return 0;
Jean Chalard6931df92012-07-12 12:55:48 +090075 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
76 outputTypes);
satok30088252010-12-01 21:22:15 +090077 }
satok8fbd5522011-02-22 17:28:55 +090078
satokb1ed1d42012-06-14 16:35:23 -070079 int getFrequency(const int32_t *word, int length) const;
80 bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const;
81 void *getDict() const { return (void *)mDict; }
82 int getDictSize() const { return mDictSize; }
83 int getMmapFd() const { return mMmapFd; }
84 int getDictBufAdjust() const { return mDictBufAdjust; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070085 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070086
satoke808e432010-12-02 14:53:24 +090087 // public static utility methods
88 // static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +090089 static int wideStrLen(unsigned short *str);
Jean Chalard581335c2011-06-17 12:45:17 +090090
Ken Wakasae12e9b52012-01-06 12:24:38 +090091 private:
satok1bc038c2012-06-14 11:25:50 -070092 DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
Ken Wakasae90b3332011-01-07 15:01:51 +090093 const unsigned char *mDict;
94
95 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
96 // also for the malloc version.
97 const int mDictSize;
98 const int mMmapFd;
99 const int mDictBufAdjust;
100
satok1bc038c2012-06-14 11:25:50 -0700101 const UnigramDictionary *mUnigramDictionary;
satokb1ed1d42012-06-14 16:35:23 -0700102 const BigramDictionary *mBigramDictionary;
Ken Wakasa8658e552012-06-30 08:53:33 +0900103 IncrementalDecoderInterface *mGestureDecoder;
The Android Open Source Project923bf412009-03-13 15:11:42 -0700104};
105
satoke808e432010-12-02 14:53:24 +0900106// public static utility methods
107// static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +0900108inline int Dictionary::wideStrLen(unsigned short *str) {
109 if (!str) return 0;
110 unsigned short *end = str;
111 while (*end)
112 end++;
113 return end - str;
114}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900115} // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700116#endif // LATINIME_DICTIONARY_H