blob: e9a03ce55cf3056a3e7c15e44286e476b6e5fdd6 [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
Ken Wakasa77e8e812012-08-02 19:48:08 +090020#include <stdint.h>
Jean Chalard1ff8dc42012-05-02 16:00:24 +090021
satoke808e432010-12-02 14:53:24 +090022#include "defines.h"
satok30088252010-12-01 21:22:15 +090023
The Android Open Source Project923bf412009-03-13 15:11:42 -070024namespace latinime {
25
Ken Wakasa77e8e812012-08-02 19:48:08 +090026class BigramDictionary;
27class IncrementalDecoderInterface;
28class ProximityInfo;
29class UnigramDictionary;
30
The Android Open Source Project923bf412009-03-13 15:11:42 -070031class Dictionary {
Ken Wakasae12e9b52012-01-06 12:24:38 +090032 public:
Jean Chalardc7387a42012-07-12 15:21:11 +090033 // Taken from SuggestedWords.java
34 const static int KIND_TYPED = 0; // What user typed
35 const static int KIND_CORRECTION = 1; // Simple correction/suggestion
36 const static int KIND_COMPLETION = 2; // Completion (suggestion with appended chars)
37 const static int KIND_WHITELIST = 3; // Whitelisted word
38 const static int KIND_BLACKLIST = 4; // Blacklisted word
39 const static int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuation
40 const static int KIND_APP_DEFINED = 6; // Suggested by the application
41 const static int KIND_SHORTCUT = 7; // A shortcut
42 const static int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input)
43
Ken Wakasae90b3332011-01-07 15:01:51 +090044 Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090045 int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions);
satok1147c7b2011-12-14 15:04:58 +090046
Satoshi Kataoka91278112012-08-08 21:23:25 +090047 int getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, int *xcoordinates,
48 int *ycoordinates, int *times, int *pointerIds, int *codes, int codesSize,
49 int *prevWordChars, int prevWordLength, int commitPoint, bool isGesture,
Satoshi Kataoka73680092012-06-25 17:44:54 +090050 bool useFullEditDistance, unsigned short *outWords,
Satoshi Kataokaf6be15c2012-08-15 16:57:32 +090051 int *frequencies, int *spaceIndices, int *outputTypes) const;
satok30088252010-12-01 21:22:15 +090052
Jean Chalard522a04e2012-04-23 15:37:07 +090053 int getBigrams(const int32_t *word, int length, int *codes, int codesSize,
Ken Wakasa77e8e812012-08-02 19:48:08 +090054 unsigned short *outWords, int *frequencies, int *outputTypes) const;
satok8fbd5522011-02-22 17:28:55 +090055
satokb1ed1d42012-06-14 16:35:23 -070056 int getFrequency(const int32_t *word, int length) const;
57 bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const;
Ken Wakasa34710b02012-08-14 14:22:27 +090058 const uint8_t *getDict() const { // required to release dictionary buffer
59 return mDict;
Ken Wakasabcec82d2012-08-12 11:10:48 +090060 }
Ken Wakasa34710b02012-08-14 14:22:27 +090061 const uint8_t *getOffsetDict() const {
62 return mOffsetDict;
Ken Wakasabcec82d2012-08-12 11:10:48 +090063 }
satokb1ed1d42012-06-14 16:35:23 -070064 int getDictSize() const { return mDictSize; }
65 int getMmapFd() const { return mMmapFd; }
66 int getDictBufAdjust() const { return mDictBufAdjust; }
Ken Wakasa77e8e812012-08-02 19:48:08 +090067 virtual ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070068
satoke808e432010-12-02 14:53:24 +090069 // public static utility methods
70 // static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +090071 static int wideStrLen(unsigned short *str);
Jean Chalard581335c2011-06-17 12:45:17 +090072
Ken Wakasae12e9b52012-01-06 12:24:38 +090073 private:
satok1bc038c2012-06-14 11:25:50 -070074 DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
Ken Wakasa34710b02012-08-14 14:22:27 +090075 const uint8_t *mDict;
76 const uint8_t *mOffsetDict;
Ken Wakasae90b3332011-01-07 15:01:51 +090077
78 // Used only for the mmap version of dictionary loading, but we use these as dummy variables
79 // also for the malloc version.
80 const int mDictSize;
81 const int mMmapFd;
82 const int mDictBufAdjust;
83
satok1bc038c2012-06-14 11:25:50 -070084 const UnigramDictionary *mUnigramDictionary;
satokb1ed1d42012-06-14 16:35:23 -070085 const BigramDictionary *mBigramDictionary;
Ken Wakasa8658e552012-06-30 08:53:33 +090086 IncrementalDecoderInterface *mGestureDecoder;
The Android Open Source Project923bf412009-03-13 15:11:42 -070087};
88
satoke808e432010-12-02 14:53:24 +090089// public static utility methods
90// static inline methods should be defined in the header file
satok18c28f42010-12-02 18:11:54 +090091inline int Dictionary::wideStrLen(unsigned short *str) {
92 if (!str) return 0;
93 unsigned short *end = str;
Ken Wakasabcec82d2012-08-12 11:10:48 +090094 while (*end) {
satok18c28f42010-12-02 18:11:54 +090095 end++;
Ken Wakasabcec82d2012-08-12 11:10:48 +090096 }
satok18c28f42010-12-02 18:11:54 +090097 return end - str;
98}
Ken Wakasace9e52a2011-06-18 13:09:55 +090099} // namespace latinime
The Android Open Source Project923bf412009-03-13 15:11:42 -0700100#endif // LATINIME_DICTIONARY_H