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#ifndef LATINIME_DICTIONARY_H
18#define LATINIME_DICTIONARY_H
19
20#include <map>
21
22#include "bigram_dictionary.h"
23#include "char_utils.h"
24#include "correction.h"
25#include "defines.h"
26#include "proximity_info.h"
27#include "unigram_dictionary.h"
28#include "words_priority_queue_pool.h"
29
30namespace latinime {
31
32class Dictionary {
33 public:
34    Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
35            int fullWordMultiplier, int maxWordLength, int maxWords);
36
37    int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
38            int *codes, int codesSize, const int32_t* prevWordChars, const int prevWordLength,
39            bool useFullEditDistance, unsigned short *outWords, int *frequencies) {
40        std::map<int, int> bigramMap;
41        uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
42        mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
43                prevWordLength, &bigramMap, bigramFilter);
44        return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool,
45                mCorrection, xcoordinates, ycoordinates, codes, codesSize, &bigramMap,
46                bigramFilter, useFullEditDistance, outWords, frequencies);
47    }
48
49    int getBigrams(const int32_t *word, int length, int *codes, int codesSize,
50            unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams) {
51        return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
52                maxWordLength, maxBigrams);
53    }
54
55    int getFrequency(const int32_t *word, int length);
56    bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2);
57    void *getDict() { return (void *)mDict; }
58    int getDictSize() { return mDictSize; }
59    int getMmapFd() { return mMmapFd; }
60    int getDictBufAdjust() { return mDictBufAdjust; }
61    ~Dictionary();
62
63    // public static utility methods
64    // static inline methods should be defined in the header file
65    static int wideStrLen(unsigned short *str);
66
67 private:
68    const unsigned char *mDict;
69
70    // Used only for the mmap version of dictionary loading, but we use these as dummy variables
71    // also for the malloc version.
72    const int mDictSize;
73    const int mMmapFd;
74    const int mDictBufAdjust;
75
76    UnigramDictionary *mUnigramDictionary;
77    BigramDictionary *mBigramDictionary;
78    WordsPriorityQueuePool *mWordsPriorityQueuePool;
79    Correction *mCorrection;
80};
81
82// public static utility methods
83// static inline methods should be defined in the header file
84inline int Dictionary::wideStrLen(unsigned short *str) {
85    if (!str) return 0;
86    unsigned short *end = str;
87    while (*end)
88        end++;
89    return end - str;
90}
91} // namespace latinime
92
93#endif // LATINIME_DICTIONARY_H
94