blob: 2be1f4f39655838abf9c5d8f59591afb71321a2e [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
Ken Wakasa0bbb9172012-07-25 17:51:43 +09002 * 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 */
The Android Open Source Project923bf412009-03-13 15:11:42 -070016
satoke808e432010-12-02 14:53:24 +090017#define LOG_TAG "LatinIME: dictionary.cpp"
18
Ken Wakasa77e8e812012-08-02 19:48:08 +090019#include <stdint.h>
20
21#include "bigram_dictionary.h"
Jean Chalard46a1eec2012-02-27 19:48:47 +090022#include "binary_format.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090023#include "defines.h"
The Android Open Source Project923bf412009-03-13 15:11:42 -070024#include "dictionary.h"
Satoshi Kataokae9f3e182012-08-09 23:23:08 +090025#include "dic_traverse_wrapper.h"
Ken Wakasaffd08e32012-12-20 18:32:44 +090026#include "gesture_suggest.h"
Ken Wakasa77e8e812012-08-02 19:48:08 +090027#include "unigram_dictionary.h"
satokd4952c82010-12-01 19:09:29 +090028
The Android Open Source Project923bf412009-03-13 15:11:42 -070029namespace latinime {
30
Ken Wakasa5db594a2013-01-12 01:18:00 +090031Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust)
Ken Wakasa162c2112012-08-24 14:51:15 +090032 : mDict(static_cast<unsigned char *>(dict)),
33 mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)),
34 mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
Ken Wakasa5db594a2013-01-12 01:18:00 +090035 mUnigramDictionary(new UnigramDictionary(mOffsetDict, BinaryFormat::getFlags(mDict))),
36 mBigramDictionary(new BigramDictionary(mOffsetDict)),
37 mGestureSuggest(new GestureSuggest()) {
The Android Open Source Project923bf412009-03-13 15:11:42 -070038}
39
satok662fe692010-12-08 17:05:39 +090040Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090041 delete mUnigramDictionary;
42 delete mBigramDictionary;
Ken Wakasaffd08e32012-12-20 18:32:44 +090043 delete mGestureSuggest;
The Android Open Source Project923bf412009-03-13 15:11:42 -070044}
satoke808e432010-12-02 14:53:24 +090045
Satoshi Kataoka91278112012-08-08 21:23:25 +090046int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession,
Ken Wakasa5db594a2013-01-12 01:18:00 +090047 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
48 int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint, bool isGesture,
Ken Wakasa1e614932012-10-29 18:06:22 +090049 bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices,
50 int *outputTypes) const {
Ken Wakasa77e8e812012-08-02 19:48:08 +090051 int result = 0;
52 if (isGesture) {
Satoshi Kataokae9f3e182012-08-09 23:23:08 +090053 DicTraverseWrapper::initDicTraverseSession(
Ken Wakasa5db594a2013-01-12 01:18:00 +090054 traverseSession, this, prevWordCodePoints, prevWordLength);
55 result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
56 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
57 frequencies, spaceIndices, outputTypes);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090058 if (DEBUG_DICT) {
Ken Wakasa5db594a2013-01-12 01:18:00 +090059 DUMP_RESULT(outWords, frequencies);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090060 }
Ken Wakasa77e8e812012-08-02 19:48:08 +090061 return result;
62 } else {
63 std::map<int, int> bigramMap;
64 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
Ken Wakasa5db594a2013-01-12 01:18:00 +090065 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordCodePoints,
Ken Wakasa77e8e812012-08-02 19:48:08 +090066 prevWordLength, &bigramMap, bigramFilter);
Ken Wakasa5db594a2013-01-12 01:18:00 +090067 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
68 inputCodePoints, inputSize, &bigramMap, bigramFilter, useFullEditDistance, outWords,
69 frequencies, outputTypes);
Ken Wakasa77e8e812012-08-02 19:48:08 +090070 return result;
71 }
72}
73
Ken Wakasa5db594a2013-01-12 01:18:00 +090074int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize,
Ken Wakasa1e614932012-10-29 18:06:22 +090075 int *outWords, int *frequencies, int *outputTypes) const {
Ken Wakasa77e8e812012-08-02 19:48:08 +090076 if (length <= 0) return 0;
Ken Wakasa5db594a2013-01-12 01:18:00 +090077 return mBigramDictionary->getBigrams(word, length, inputCodePoints, inputSize, outWords,
78 frequencies, outputTypes);
Ken Wakasa77e8e812012-08-02 19:48:08 +090079}
80
Ken Wakasaaa5a3e82012-12-03 19:54:30 +090081int Dictionary::getFrequency(const int *word, int length) const {
Satoshi Kataoka2f854e12012-05-29 15:58:13 +090082 return mUnigramDictionary->getFrequency(word, length);
satoke808e432010-12-02 14:53:24 +090083}
84
Ken Wakasaaa5a3e82012-12-03 19:54:30 +090085bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const {
Tom Ouyang4d289d32012-04-26 23:50:21 -070086 return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
87}
The Android Open Source Project923bf412009-03-13 15:11:42 -070088} // namespace latinime