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

Side by Side Diff: third_party/prediction/suggest/core/dictionary/dictionary_utils.cpp

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: 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) 2014, 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 "third_party/prediction/suggest/core/dictionary/dictionary_utils.h"
18
19 #include "third_party/prediction/suggest/core/dicnode/dic_node.h"
20 #include "third_party/prediction/suggest/core/dicnode/dic_node_priority_queue.h"
21 #include "third_party/prediction/suggest/core/dicnode/dic_node_vector.h"
22 #include "third_party/prediction/suggest/core/dictionary/dictionary.h"
23 #include "third_party/prediction/suggest/core/dictionary/digraph_utils.h"
24 #include "third_party/prediction/suggest/core/session/prev_words_info.h"
25 #include "third_party/prediction/suggest/core/policy/dictionary_structure_with_b uffer_policy.h"
26
27 namespace latinime {
28
29 /* static */ int DictionaryUtils::getMaxProbabilityOfExactMatches(
30 const DictionaryStructureWithBufferPolicy* const dictionaryStructurePolicy,
31 const int* const codePoints,
32 const int codePointCount) {
33 std::vector<DicNode> current;
34 std::vector<DicNode> next;
35
36 // No prev words information.
37 PrevWordsInfo emptyPrevWordsInfo;
38 int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
39 emptyPrevWordsInfo.getPrevWordsTerminalPtNodePos(
40 dictionaryStructurePolicy, prevWordsPtNodePos,
41 false /* tryLowerCaseSearch */);
42 current.emplace_back();
43 DicNodeUtils::initAsRoot(dictionaryStructurePolicy, prevWordsPtNodePos,
44 &current.front());
45 for (int i = 0; i < codePointCount; ++i) {
46 // The base-lower input is used to ignore case errors and accent errors.
47 const int codePoint = CharUtils::toBaseLowerCase(codePoints[i]);
48 for (const DicNode& dicNode : current) {
49 if (dicNode.isInDigraph() && dicNode.getNodeCodePoint() == codePoint) {
50 next.emplace_back(dicNode);
51 next.back().advanceDigraphIndex();
52 continue;
53 }
54 processChildDicNodes(dictionaryStructurePolicy, codePoint, &dicNode,
55 &next);
56 }
57 current.clear();
58 current.swap(next);
59 }
60
61 int maxProbability = NOT_A_PROBABILITY;
62 for (const DicNode& dicNode : current) {
63 if (!dicNode.isTerminalDicNode()) {
64 continue;
65 }
66 // dicNode can contain case errors, accent errors, intentional omissions or
67 // digraphs.
68 maxProbability = std::max(maxProbability, dicNode.getProbability());
69 }
70 return maxProbability;
71 }
72
73 /* static */ void DictionaryUtils::processChildDicNodes(
74 const DictionaryStructureWithBufferPolicy* const dictionaryStructurePolicy,
75 const int inputCodePoint,
76 const DicNode* const parentDicNode,
77 std::vector<DicNode>* const outDicNodes) {
78 DicNodeVector childDicNodes;
79 DicNodeUtils::getAllChildDicNodes(parentDicNode, dictionaryStructurePolicy,
80 &childDicNodes);
81 for (int childIndex = 0; childIndex < childDicNodes.getSizeAndLock();
82 ++childIndex) {
83 DicNode* const childDicNode = childDicNodes[childIndex];
84 const int codePoint =
85 CharUtils::toBaseLowerCase(childDicNode->getNodeCodePoint());
86 if (inputCodePoint == codePoint) {
87 outDicNodes->emplace_back(*childDicNode);
88 }
89 if (childDicNode->canBeIntentionalOmission()) {
90 processChildDicNodes(dictionaryStructurePolicy, inputCodePoint,
91 childDicNode, outDicNodes);
92 }
93 if (DigraphUtils::hasDigraphForCodePoint(
94 dictionaryStructurePolicy->getHeaderStructurePolicy(),
95 childDicNode->getNodeCodePoint())) {
96 childDicNode->advanceDigraphIndex();
97 if (childDicNode->getNodeCodePoint() == codePoint) {
98 childDicNode->advanceDigraphIndex();
99 outDicNodes->emplace_back(*childDicNode);
100 }
101 }
102 }
103 }
104
105 } // namespace latinime
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698