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