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 #include "dictionary/utils/multi_bigram_map.h"
18
19 #include <cstddef>
20 #include <unordered_map>
21
22 namespace latinime {
23
24 // Max number of bigram maps (previous word contexts) to be cached. Increasing this number
25 // could improve bigram lookup speed for multi-word suggestions, but at the cost of more memory
26 // usage. Also, there are diminishing returns since the most frequently used bigrams are
27 // typically near the beginning of the input and are thus the first ones to be cached. Note
28 // that these bigrams are reset for each new composing word.
29 const size_t MultiBigramMap::MAX_CACHED_PREV_WORDS_IN_BIGRAM_MAP = 25;
30
31 // Most common previous word contexts currently have 100 bigrams
32 const int MultiBigramMap::BigramMap::DEFAULT_HASH_MAP_SIZE_FOR_EACH_BIGRAM_MAP = 100;
33
34 // Look up the bigram probability for the given word pair from the cached bigram maps.
35 // Also caches the bigrams if there is space remaining and they have not been cached already.
getBigramProbability(const DictionaryStructureWithBufferPolicy * const structurePolicy,const WordIdArrayView prevWordIds,const int nextWordId,const int unigramProbability)36 int MultiBigramMap::getBigramProbability(
37 const DictionaryStructureWithBufferPolicy *const structurePolicy,
38 const WordIdArrayView prevWordIds, const int nextWordId,
39 const int unigramProbability) {
40 if (prevWordIds.empty() || prevWordIds[0] == NOT_A_WORD_ID) {
41 return structurePolicy->getProbability(unigramProbability, NOT_A_PROBABILITY);
42 }
43 const auto mapPosition = mBigramMaps.find(prevWordIds[0]);
44 if (mapPosition != mBigramMaps.end()) {
45 return mapPosition->second.getBigramProbability(structurePolicy, nextWordId,
46 unigramProbability);
47 }
48 if (mBigramMaps.size() < MAX_CACHED_PREV_WORDS_IN_BIGRAM_MAP) {
49 addBigramsForWord(structurePolicy, prevWordIds);
50 return mBigramMaps[prevWordIds[0]].getBigramProbability(structurePolicy,
51 nextWordId, unigramProbability);
52 }
53 return readBigramProbabilityFromBinaryDictionary(structurePolicy, prevWordIds,
54 nextWordId, unigramProbability);
55 }
56
init(const DictionaryStructureWithBufferPolicy * const structurePolicy,const WordIdArrayView prevWordIds)57 void MultiBigramMap::BigramMap::init(
58 const DictionaryStructureWithBufferPolicy *const structurePolicy,
59 const WordIdArrayView prevWordIds) {
60 structurePolicy->iterateNgramEntries(prevWordIds, this /* listener */);
61 }
62
getBigramProbability(const DictionaryStructureWithBufferPolicy * const structurePolicy,const int nextWordId,const int unigramProbability) const63 int MultiBigramMap::BigramMap::getBigramProbability(
64 const DictionaryStructureWithBufferPolicy *const structurePolicy,
65 const int nextWordId, const int unigramProbability) const {
66 int bigramProbability = NOT_A_PROBABILITY;
67 if (mBloomFilter.isInFilter(nextWordId)) {
68 const auto bigramProbabilityIt = mBigramMap.find(nextWordId);
69 if (bigramProbabilityIt != mBigramMap.end()) {
70 bigramProbability = bigramProbabilityIt->second;
71 }
72 }
73 return structurePolicy->getProbability(unigramProbability, bigramProbability);
74 }
75
onVisitEntry(const int ngramProbability,const int targetWordId)76 void MultiBigramMap::BigramMap::onVisitEntry(const int ngramProbability, const int targetWordId) {
77 if (targetWordId == NOT_A_WORD_ID) {
78 return;
79 }
80 mBigramMap[targetWordId] = ngramProbability;
81 mBloomFilter.setInFilter(targetWordId);
82 }
83
addBigramsForWord(const DictionaryStructureWithBufferPolicy * const structurePolicy,const WordIdArrayView prevWordIds)84 void MultiBigramMap::addBigramsForWord(
85 const DictionaryStructureWithBufferPolicy *const structurePolicy,
86 const WordIdArrayView prevWordIds) {
87 mBigramMaps[prevWordIds[0]].init(structurePolicy, prevWordIds);
88 }
89
readBigramProbabilityFromBinaryDictionary(const DictionaryStructureWithBufferPolicy * const structurePolicy,const WordIdArrayView prevWordIds,const int nextWordId,const int unigramProbability)90 int MultiBigramMap::readBigramProbabilityFromBinaryDictionary(
91 const DictionaryStructureWithBufferPolicy *const structurePolicy,
92 const WordIdArrayView prevWordIds, const int nextWordId, const int unigramProbability) {
93 const int bigramProbability = structurePolicy->getProbabilityOfWord(prevWordIds, nextWordId);
94 if (bigramProbability != NOT_A_PROBABILITY) {
95 return bigramProbability;
96 }
97 return structurePolicy->getProbability(unigramProbability, NOT_A_PROBABILITY);
98 }
99
100 } // namespace latinime
101