Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1129)

Side by Side Diff: third_party/android_prediction/suggest/core/dictionary/dictionary.cpp

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: changed third_party/prediction to third_party/android_prediction; added CHROMIUM.diff Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "third_party/android_prediction/suggest/core/dictionary/dictionary.h"
20
21 #include "third_party/android_prediction/defines.h"
22 #include "third_party/android_prediction/suggest/core/dictionary/dictionary_util s.h"
23 #include "third_party/android_prediction/suggest/core/policy/dictionary_header_s tructure_policy.h"
24 #include "third_party/android_prediction/suggest/core/result/suggestion_results. h"
25 #include "third_party/android_prediction/suggest/core/session/dic_traverse_sessi on.h"
26 #include "third_party/android_prediction/suggest/core/session/prev_words_info.h"
27 #include "third_party/android_prediction/suggest/core/suggest.h"
28 #include "third_party/android_prediction/suggest/core/suggest_options.h"
29 #include "third_party/android_prediction/suggest/policyimpl/gesture/gesture_sugg est_policy_factory.h"
30 #include "third_party/android_prediction/suggest/policyimpl/typing/typing_sugges t_policy_factory.h"
31 #include "third_party/android_prediction/utils/time_keeper.h"
32
33 namespace latinime {
34
35 const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32;
36
37 Dictionary::Dictionary(DictionaryStructureWithBufferPolicy::StructurePolicyPtr
38 dictionaryStructureWithBufferPolicy)
39 : mDictionaryStructureWithBufferPolicy(std::move(dictionaryStructureWith BufferPolicy)),
40 mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSug gestPolicy())),
41 mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSugges tPolicy())) {
42 }
43
44 void Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
45 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int * inputCodePoints,
46 int inputSize, const PrevWordsInfo *const prevWordsInfo,
47 const SuggestOptions *const suggestOptions, const float languageWeight,
48 SuggestionResults *const outSuggestionResults) const {
49 TimeKeeper::setCurrentTime();
50 traverseSession->init(this, prevWordsInfo, suggestOptions);
51 const auto &suggest = suggestOptions->isGesture() ? mGestureSuggest : mTypin gSuggest;
52 suggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
53 ycoordinates, times, pointerIds, inputCodePoints, inputSize,
54 languageWeight, outSuggestionResults);
55 if (DEBUG_DICT) {
56 outSuggestionResults->dumpSuggestions();
57 }
58 }
59
60 Dictionary::NgramListenerForPrediction::NgramListenerForPrediction(
61 const PrevWordsInfo *const prevWordsInfo, SuggestionResults *const sugge stionResults,
62 const DictionaryStructureWithBufferPolicy *const dictStructurePolicy)
63 : mPrevWordsInfo(prevWordsInfo), mSuggestionResults(suggestionResults),
64 mDictStructurePolicy(dictStructurePolicy) {}
65
66 void Dictionary::NgramListenerForPrediction::onVisitEntry(const int ngramProbabi lity,
67 const int targetPtNodePos) {
68 if (targetPtNodePos == NOT_A_DICT_POS) {
69 return;
70 }
71 if (mPrevWordsInfo->isNthPrevWordBeginningOfSentence(1 /* n */)
72 && ngramProbability == NOT_A_PROBABILITY) {
73 return;
74 }
75 int targetWordCodePoints[MAX_WORD_LENGTH];
76 int unigramProbability = 0;
77 const int codePointCount = mDictStructurePolicy->
78 getCodePointsAndProbabilityAndReturnCodePointCount(targetPtNodePos,
79 MAX_WORD_LENGTH, targetWordCodePoints, &unigramProbability);
80 if (codePointCount <= 0) {
81 return;
82 }
83 const int probability = mDictStructurePolicy->getProbability(
84 unigramProbability, ngramProbability);
85 mSuggestionResults->addPrediction(targetWordCodePoints, codePointCount, prob ability);
86 }
87
88 void Dictionary::getPredictions(const PrevWordsInfo *const prevWordsInfo,
89 SuggestionResults *const outSuggestionResults) const {
90 TimeKeeper::setCurrentTime();
91 NgramListenerForPrediction listener(prevWordsInfo, outSuggestionResults,
92 mDictionaryStructureWithBufferPolicy.get());
93 int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
94 prevWordsInfo->getPrevWordsTerminalPtNodePos(
95 mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos,
96 true /* tryLowerCaseSearch */);
97 mDictionaryStructureWithBufferPolicy->iterateNgramEntries(prevWordsPtNodePos , &listener);
98 }
99
100 int Dictionary::getProbability(const int *word, int length) const {
101 return getNgramProbability(nullptr /* prevWordsInfo */, word, length);
102 }
103
104 int Dictionary::getMaxProbabilityOfExactMatches(const int *word, int length) con st {
105 TimeKeeper::setCurrentTime();
106 return DictionaryUtils::getMaxProbabilityOfExactMatches(
107 mDictionaryStructureWithBufferPolicy.get(), word, length);
108 }
109
110 int Dictionary::getNgramProbability(const PrevWordsInfo *const prevWordsInfo, co nst int *word,
111 int length) const {
112 TimeKeeper::setCurrentTime();
113 int nextWordPos = mDictionaryStructureWithBufferPolicy->getTerminalPtNodePos itionOfWord(word,
114 length, false /* forceLowerCaseSearch */);
115 if (NOT_A_DICT_POS == nextWordPos) return NOT_A_PROBABILITY;
116 if (!prevWordsInfo) {
117 return getDictionaryStructurePolicy()->getProbabilityOfPtNode(
118 nullptr /* prevWordsPtNodePos */, nextWordPos);
119 }
120 int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
121 prevWordsInfo->getPrevWordsTerminalPtNodePos(
122 mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos,
123 true /* tryLowerCaseSearch */);
124 return getDictionaryStructurePolicy()->getProbabilityOfPtNode(prevWordsPtNod ePos, nextWordPos);
125 }
126
127 bool Dictionary::addUnigramEntry(const int *const word, const int length,
128 const UnigramProperty *const unigramProperty) {
129 if (unigramProperty->representsBeginningOfSentence()
130 && !mDictionaryStructureWithBufferPolicy->getHeaderStructurePolicy()
131 ->supportsBeginningOfSentence()) {
132 AKLOGE("The dictionary doesn't support Beginning-of-Sentence.");
133 return false;
134 }
135 TimeKeeper::setCurrentTime();
136 return mDictionaryStructureWithBufferPolicy->addUnigramEntry(word, length, u nigramProperty);
137 }
138
139 bool Dictionary::removeUnigramEntry(const int *const codePoints, const int codeP ointCount) {
140 TimeKeeper::setCurrentTime();
141 return mDictionaryStructureWithBufferPolicy->removeUnigramEntry(codePoints, codePointCount);
142 }
143
144 bool Dictionary::addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
145 const BigramProperty *const bigramProperty) {
146 TimeKeeper::setCurrentTime();
147 return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, bi gramProperty);
148 }
149
150 bool Dictionary::removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
151 const int *const word, const int length) {
152 TimeKeeper::setCurrentTime();
153 return mDictionaryStructureWithBufferPolicy->removeNgramEntry(prevWordsInfo, word, length);
154 }
155
156 bool Dictionary::flush(const char *const filePath) {
157 TimeKeeper::setCurrentTime();
158 return mDictionaryStructureWithBufferPolicy->flush(filePath);
159 }
160
161 bool Dictionary::flushWithGC(const char *const filePath) {
162 TimeKeeper::setCurrentTime();
163 return mDictionaryStructureWithBufferPolicy->flushWithGC(filePath);
164 }
165
166 bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
167 TimeKeeper::setCurrentTime();
168 return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC);
169 }
170
171 void Dictionary::getProperty(const char *const query, const int queryLength, cha r *const outResult,
172 const int maxResultLength) {
173 TimeKeeper::setCurrentTime();
174 return mDictionaryStructureWithBufferPolicy->getProperty(query, queryLength, outResult,
175 maxResultLength);
176 }
177
178 const WordProperty Dictionary::getWordProperty(const int *const codePoints,
179 const int codePointCount) {
180 TimeKeeper::setCurrentTime();
181 return mDictionaryStructureWithBufferPolicy->getWordProperty(
182 codePoints, codePointCount);
183 }
184
185 int Dictionary::getNextWordAndNextToken(const int token, int *const outCodePoint s,
186 int *const outCodePointCount) {
187 TimeKeeper::setCurrentTime();
188 return mDictionaryStructureWithBufferPolicy->getNextWordAndNextToken(
189 token, outCodePoints, outCodePointCount);
190 }
191
192 } // namespace latinime
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698