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

Side by Side Diff: third_party/prediction/suggest/policyimpl/utils/edit_distance.h

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) 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_EDIT_DISTANCE_H
18 #define LATINIME_EDIT_DISTANCE_H
19
20 #include <algorithm>
21
22 #include "third_party/prediction/defines.h"
23 #include "third_party/prediction/suggest/policyimpl/utils/edit_distance_policy.h "
24
25 namespace latinime {
26
27 class EditDistance {
28 public:
29 // CAVEAT: There may be performance penalty if you need the edit distance as
30 // an integer value.
31 AK_FORCE_INLINE static float getEditDistance(
32 const EditDistancePolicy* const policy) {
33 const int beforeLength = policy->getString0Length();
34 const int afterLength = policy->getString1Length();
35 float dp[(beforeLength + 1) * (afterLength + 1)];
36 for (int i = 0; i <= beforeLength; ++i) {
37 dp[(afterLength + 1) * i] = i * policy->getInsertionCost(i - 1, -1);
38 }
39 for (int i = 0; i <= afterLength; ++i) {
40 dp[i] = i * policy->getDeletionCost(-1, i - 1);
41 }
42
43 for (int i = 0; i < beforeLength; ++i) {
44 for (int j = 0; j < afterLength; ++j) {
45 dp[(afterLength + 1) * (i + 1) + (j + 1)] =
46 std::min(dp[(afterLength + 1) * i + (j + 1)] +
47 policy->getInsertionCost(i, j),
48 std::min(dp[(afterLength + 1) * (i + 1) + j] +
49 policy->getDeletionCost(i, j),
50 dp[(afterLength + 1) * i + j] +
51 policy->getSubstitutionCost(i, j)));
52 if (policy->allowTransposition(i, j)) {
53 dp[(afterLength + 1) * (i + 1) + (j + 1)] =
54 std::min(dp[(afterLength + 1) * (i + 1) + (j + 1)],
55 dp[(afterLength + 1) * (i - 1) + (j - 1)] +
56 policy->getTranspositionCost(i, j));
57 }
58 }
59 }
60 if (DEBUG_EDIT_DISTANCE) {
61 AKLOGI("IN = %d, OUT = %d", beforeLength, afterLength);
62 for (int i = 0; i < beforeLength + 1; ++i) {
63 for (int j = 0; j < afterLength + 1; ++j) {
64 AKLOGI("EDIT[%d][%d], %f", i, j, dp[(afterLength + 1) * i + j]);
65 }
66 }
67 }
68 return dp[(beforeLength + 1) * (afterLength + 1) - 1];
69 }
70
71 AK_FORCE_INLINE static void dumpEditDistance10ForDebug(
72 const float* const editDistanceTable,
73 const int editDistanceTableWidth,
74 const int outputLength) {
75 if (DEBUG_DICT) {
76 AKLOGI("EditDistanceTable");
77 for (int i = 0; i <= 10; ++i) {
78 float c[11];
79 for (int j = 0; j <= 10; ++j) {
80 if (j < editDistanceTableWidth + 1 && i < outputLength + 1) {
81 c[j] = (editDistanceTable + i * (editDistanceTableWidth + 1))[j];
82 } else {
83 c[j] = -1.0f;
84 }
85 }
86 AKLOGI("[ %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f ]", c[0], c[1],
87 c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10]);
88 (void)c; // To suppress compiler warning
89 }
90 }
91 }
92
93 private:
94 DISALLOW_IMPLICIT_CONSTRUCTORS(EditDistance);
95 };
96 } // namespace latinime
97
98 #endif // LATINIME_EDIT_DISTANCE_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698