• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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_MULTI_BIGRAM_MAP_H
18 #define LATINIME_MULTI_BIGRAM_MAP_H
19 
20 #include <cstring>
21 #include <stdint.h>
22 
23 #include "defines.h"
24 #include "binary_format.h"
25 #include "hash_map_compat.h"
26 
27 namespace latinime {
28 
29 // Class for caching bigram maps for multiple previous word contexts. This is useful since the
30 // algorithm needs to look up the set of bigrams for every word pair that occurs in every
31 // multi-word suggestion.
32 class MultiBigramMap {
33  public:
MultiBigramMap()34     MultiBigramMap() : mBigramMaps() {}
~MultiBigramMap()35     ~MultiBigramMap() {}
36 
37     // Look up the bigram probability for the given word pair from the cached bigram maps.
38     // Also caches the bigrams if there is space remaining and they have not been cached already.
getBigramProbability(const uint8_t * const dicRoot,const int wordPosition,const int nextWordPosition,const int unigramProbability)39     int getBigramProbability(const uint8_t *const dicRoot, const int wordPosition,
40             const int nextWordPosition, const int unigramProbability) {
41         hash_map_compat<int, BigramMap>::const_iterator mapPosition =
42                 mBigramMaps.find(wordPosition);
43         if (mapPosition != mBigramMaps.end()) {
44             return mapPosition->second.getBigramProbability(nextWordPosition, unigramProbability);
45         }
46         if (mBigramMaps.size() < MAX_CACHED_PREV_WORDS_IN_BIGRAM_MAP) {
47             addBigramsForWordPosition(dicRoot, wordPosition);
48             return mBigramMaps[wordPosition].getBigramProbability(
49                     nextWordPosition, unigramProbability);
50         }
51         return BinaryFormat::getBigramProbability(
52                 dicRoot, wordPosition, nextWordPosition, unigramProbability);
53     }
54 
clear()55     void clear() {
56         mBigramMaps.clear();
57     }
58 
59  private:
60     DISALLOW_COPY_AND_ASSIGN(MultiBigramMap);
61 
62     class BigramMap {
63      public:
BigramMap()64         BigramMap() : mBigramMap(DEFAULT_HASH_MAP_SIZE_FOR_EACH_BIGRAM_MAP) {}
~BigramMap()65         ~BigramMap() {}
66 
init(const uint8_t * const dicRoot,int position)67         void init(const uint8_t *const dicRoot, int position) {
68             BinaryFormat::fillBigramProbabilityToHashMap(dicRoot, position, &mBigramMap);
69         }
70 
getBigramProbability(const int nextWordPosition,const int unigramProbability)71         inline int getBigramProbability(const int nextWordPosition, const int unigramProbability)
72                 const {
73            return BinaryFormat::getBigramProbabilityFromHashMap(
74                    nextWordPosition, &mBigramMap, unigramProbability);
75         }
76 
77      private:
78         // Note: Default copy constructor needed for use in hash_map.
79         hash_map_compat<int, int> mBigramMap;
80     };
81 
addBigramsForWordPosition(const uint8_t * const dicRoot,const int position)82     void addBigramsForWordPosition(const uint8_t *const dicRoot, const int position) {
83         mBigramMaps[position].init(dicRoot, position);
84     }
85 
86     hash_map_compat<int, BigramMap> mBigramMaps;
87 };
88 } // namespace latinime
89 #endif // LATINIME_MULTI_BIGRAM_MAP_H
90