dictionary.cpp revision a71ed8caa27c4a0174f25750171282980bc26880
1/*
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#define LOG_TAG "LatinIME: dictionary.cpp"
18
19#include "suggest/core/dictionary/dictionary.h"
20
21#include <map> // TODO: remove
22#include <stdint.h>
23
24#include "defines.h"
25#include "suggest/core/dictionary/bigram_dictionary.h"
26#include "suggest/core/dictionary/binary_format.h"
27#include "suggest/core/session/dic_traverse_session.h"
28#include "suggest/core/suggest.h"
29#include "suggest/core/suggest_options.h"
30#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
31#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
32
33namespace latinime {
34
35Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust)
36        : mBinaryDictionaryInfo(static_cast<const uint8_t *>(dict), dictSize),
37          mDictSize(dictSize),
38          mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
39          mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)),
40          mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
41          mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
42}
43
44Dictionary::~Dictionary() {
45    delete mBigramDictionary;
46    delete mGestureSuggest;
47    delete mTypingSuggest;
48}
49
50int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
51        int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
52        int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
53        const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
54        int *spaceIndices, int *outputTypes) const {
55    int result = 0;
56    if (suggestOptions->isGesture()) {
57        DicTraverseSession::initSessionInstance(
58                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
59        result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
60                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
61                frequencies, spaceIndices, outputTypes);
62        if (DEBUG_DICT) {
63            DUMP_RESULT(outWords, frequencies);
64        }
65        return result;
66    } else {
67        DicTraverseSession::initSessionInstance(
68                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
69        result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
70                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
71                outWords, frequencies, spaceIndices, outputTypes);
72        if (DEBUG_DICT) {
73            DUMP_RESULT(outWords, frequencies);
74        }
75        return result;
76    }
77}
78
79int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize,
80        int *outWords, int *frequencies, int *outputTypes) const {
81    if (length <= 0) return 0;
82    return mBigramDictionary->getPredictions(word, length, inputCodePoints, inputSize, outWords,
83            frequencies, outputTypes);
84}
85
86int Dictionary::getProbability(const int *word, int length) const {
87    const uint8_t *const root = mBinaryDictionaryInfo.getDictRoot();
88    int pos = BinaryFormat::getTerminalPosition(root, word, length,
89            false /* forceLowerCaseSearch */);
90    if (NOT_VALID_WORD == pos) {
91        return NOT_A_PROBABILITY;
92    }
93    const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
94    if (flags & (BinaryFormat::FLAG_IS_BLACKLISTED | BinaryFormat::FLAG_IS_NOT_A_WORD)) {
95        // If this is not a word, or if it's a blacklisted entry, it should behave as
96        // having no probability outside of the suggestion process (where it should be used
97        // for shortcuts).
98        return NOT_A_PROBABILITY;
99    }
100    const bool hasMultipleChars = (0 != (BinaryFormat::FLAG_HAS_MULTIPLE_CHARS & flags));
101    if (hasMultipleChars) {
102        pos = BinaryFormat::skipOtherCharacters(root, pos);
103    } else {
104        BinaryFormat::getCodePointAndForwardPointer(root, &pos);
105    }
106    const int unigramProbability = BinaryFormat::readProbabilityWithoutMovingPointer(root, pos);
107    return unigramProbability;
108}
109
110bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const {
111    return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
112}
113
114} // namespace latinime
115