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

Unified Diff: services/prediction/proximity_info_service.cc

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, 5 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 side-by-side diff with in-line comments
Download patch
Index: services/prediction/proximity_info_service.cc
diff --git a/services/prediction/proximity_info_service.cc b/services/prediction/proximity_info_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3d5f6d25632f80e186603509b77f492074993580
--- /dev/null
+++ b/services/prediction/proximity_info_service.cc
@@ -0,0 +1,235 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <algorithm>
+#include <cmath>
+#include <new>
+
+#include "services/prediction/proximity_info_service.h"
+#include "services/prediction/touch_position_correction.h"
+
+// NOTE: This class has been translated to C++ and modified from the Android
+// Open Source Project. Specifically from some functions of the following file:
+// https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/
+// android-5.1.1_r8/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
+
+namespace prediction {
+
+const float ProximityInfoService::SEARCH_DISTANCE = 1.2f;
+
+const float ProximityInfoService::DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS =
+ 0.15f;
+
+ProximityInfoService::ProximityInfoService(
+ const std::string locale,
+ const int grid_width,
+ const int grid_height,
+ const int min_width,
+ const int height,
+ const int most_common_key_width,
+ const int most_common_key_height,
+ TouchPositionCorrection* touch_position_correction) {
+ plocale = locale;
+ pgrid_width = grid_width;
+ pgrid_height = grid_height;
+ pgrid_size = pgrid_width * pgrid_height;
+ pcell_width = (min_width + pgrid_width - 1) / pgrid_width;
+ pcell_height = (height + pgrid_height - 1) / pgrid_height;
+ pkeyboard_min_width = min_width;
+ pkeyboard_height = height;
+ pmost_common_key_height = most_common_key_height;
+ pmost_common_key_width = most_common_key_width;
+ pgrid_neighbors = new const Key** [pgrid_size];
+ if (min_width == 0 || height == 0) {
+ return;
+ }
+ neighbor_count_per_cell = new int[pgrid_size];
+ ProximityInfoService::ComputeNearestNeighbors();
APW 2015/07/23 20:11:27 You don't need the ProcimityInfoService:: part.
riajiang 2015/07/31 02:13:04 Done.
+ pnative_proximity_info =
+ ProximityInfoService::CreateNativeProximityInfoService(
+ touch_position_correction);
+}
+
+ProximityInfoService::~ProximityInfoService() {
+ for (int i = 0; i < pgrid_size; i++) {
+ delete[] pgrid_neighbors[i];
+ }
+ delete[] pgrid_neighbors;
+ delete[] neighbor_count_per_cell;
+ delete pnative_proximity_info;
+}
+
+bool ProximityInfoService::NeedsProximityInfoService(Key key) {
APW 2015/07/23 20:11:27 Maybe this doesn't need Service in name either.
riajiang 2015/07/31 02:13:04 Done.
+ return key.kcode >= ' ';
+}
+
APW 2015/07/23 20:11:27 Not creating a service. Remove Service from name
riajiang 2015/07/31 02:13:03 Done.
+latinime::ProximityInfo* ProximityInfoService::CreateNativeProximityInfoService(
+ TouchPositionCorrection* touch_position_correction) {
+ int* proximity_chars_array = new int[pgrid_size * MAX_PROXIMITY_CHARS_SIZE];
+ for (int i = 0; i < pgrid_size; i++) {
+ const Key** neighbor_keys = pgrid_neighbors[i];
+ int info_index = i * MAX_PROXIMITY_CHARS_SIZE;
+ for (int j = 0; j < neighbor_count_per_cell[i]; j++) {
+ Key neighbor_key = *neighbor_keys[j];
+ if (!ProximityInfoService::NeedsProximityInfoService(neighbor_key)) {
+ continue;
+ }
+ proximity_chars_array[info_index] = neighbor_key.kcode;
+ info_index++;
+ }
+ }
+
+ int* key_x_coordinates = new int[KeySet::key_count];
APW 2015/07/23 20:11:27 don't new and then delete if you don't need to - I
riajiang 2015/07/31 02:13:04 Done.
+ int* key_y_coordinates = new int[KeySet::key_count];
+ int* key_widths = new int[KeySet::key_count];
+ int* key_heights = new int[KeySet::key_count];
+ int* key_char_codes = new int[KeySet::key_count];
+ float* sweet_spot_center_xs = new float[KeySet::key_count];
+ float* sweet_spot_center_ys = new float[KeySet::key_count];
+ float* sweet_spot_radii = new float[KeySet::key_count];
+
+ for (int info_index = 0, key_index = 0; key_index < KeySet::key_count;
+ key_index++) {
+ Key key = KeySet::key_set[key_index];
+ if (!ProximityInfoService::NeedsProximityInfoService(key)) {
+ continue;
+ }
+ key_x_coordinates[info_index] = key.kx;
+ key_y_coordinates[info_index] = key.ky;
+ key_widths[info_index] = key.kwidth;
+ key_heights[info_index] = key.kheight;
+ key_char_codes[info_index] = key.kcode;
+ info_index++;
+ }
+
+ if (touch_position_correction->IsValid()) {
+ const int rows = touch_position_correction->GetRows();
+ const float default_radius =
+ DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS *
+ (float)std::hypot(pmost_common_key_width, pmost_common_key_height);
+ for (int info_index = 0, key_index = 0; key_index < KeySet::key_count;
+ key_index++) {
+ Key key = KeySet::key_set[key_index];
+ if (!ProximityInfoService::NeedsProximityInfoService(key)) {
+ continue;
+ }
+ sweet_spot_center_xs[info_index] =
+ (key.khit_box_left + key.khit_box_right) * 0.5f;
+ sweet_spot_center_ys[info_index] =
+ (key.khit_box_top + key.khit_box_bottom) * 0.5f;
+ sweet_spot_radii[info_index] = default_radius;
+ const int row = key.khit_box_top / pmost_common_key_height;
+ if (row < rows) {
+ const int hit_box_width = key.khit_box_right - key.khit_box_left;
+ const int hit_box_height = key.khit_box_bottom - key.khit_box_top;
+ const float hit_box_diagonal =
+ (float)std::hypot(hit_box_width, hit_box_height);
+ sweet_spot_center_xs[info_index] +=
+ touch_position_correction->GetX(row) * hit_box_width;
+ sweet_spot_center_ys[info_index] +=
+ touch_position_correction->GetY(row) * hit_box_height;
+ sweet_spot_radii[info_index] =
+ touch_position_correction->GetRadius(row) * hit_box_diagonal;
+ }
+ info_index++;
+ }
+ } else {
+ sweet_spot_center_xs = sweet_spot_center_ys = sweet_spot_radii = NULL;
+ }
+
+ latinime::ProximityInfo* proximity_info = new latinime::ProximityInfo(
APW 2015/07/23 20:11:27 Return without newing.
riajiang 2015/07/31 02:13:03 As discussed still newing this.
+ plocale, pkeyboard_min_width, pkeyboard_height, pgrid_width, pgrid_height,
+ pmost_common_key_width, pmost_common_key_height, proximity_chars_array,
+ pgrid_size * MAX_PROXIMITY_CHARS_SIZE, KeySet::key_count,
+ key_x_coordinates, key_y_coordinates, key_widths, key_heights,
+ key_char_codes, sweet_spot_center_xs, sweet_spot_center_ys,
+ sweet_spot_radii);
+
+ delete[] proximity_chars_array;
+ delete[] key_x_coordinates;
+ delete[] key_y_coordinates;
+ delete[] key_widths;
+ delete[] key_heights;
+ delete[] key_char_codes;
+ delete[] sweet_spot_center_xs;
+ delete[] sweet_spot_center_ys;
+ delete[] sweet_spot_radii;
+
+ return proximity_info;
+}
+
+latinime::ProximityInfo* ProximityInfoService::GetNativeProximityInfoService() {
APW 2015/07/23 20:11:27 You'ere not returning the Service - remove that pa
riajiang 2015/07/31 02:13:04 Done.
+ return pnative_proximity_info;
+}
+
+void ProximityInfoService::ComputeNearestNeighbors() {
+ const int default_width = pmost_common_key_width;
+ const int grid_size = pgrid_size;
+ const int threshold = (int)(default_width * SEARCH_DISTANCE);
+ const int threshold_squared = threshold * threshold;
+ const int last_pixel_x_coordinate = pgrid_width * pcell_width - 1;
+ const int last_pixel_y_coordinate = pgrid_height * pcell_height - 1;
+
+ const Key* neighbors_flat_buffer[grid_size * KeySet::key_count];
+ for (int i = 0; i < grid_size; i++) {
+ neighbor_count_per_cell[i] = 0;
+ }
+ const int half_cell_width = pcell_width / 2;
+ const int half_cell_height = pcell_height / 2;
+ for (int i = 0; i < KeySet::key_count; i++) {
+ const Key& key = KeySet::key_set[i];
+
+ const int key_x = key.kx;
+ const int key_y = key.ky;
+ const int top_pixel_within_threshold = key_y - threshold;
+ const int y_delta_to_grid = top_pixel_within_threshold % pcell_height;
+ const int y_middle_of_top_cell =
+ top_pixel_within_threshold - y_delta_to_grid + half_cell_height;
+ const int y_start =
+ std::max(half_cell_height,
+ y_middle_of_top_cell +
+ (y_delta_to_grid <= half_cell_height ? 0 : pcell_height));
+ const int y_end =
+ std::min(last_pixel_y_coordinate, key_y + key.kheight + threshold);
+
+ const int left_pixel_within_threshold = key_x - threshold;
+ const int x_delta_to_grid = left_pixel_within_threshold % pcell_width;
+ const int x_middle_of_left_cell =
+ left_pixel_within_threshold - x_delta_to_grid + half_cell_width;
+ const int x_start =
+ std::max(half_cell_width,
+ x_middle_of_left_cell +
+ (x_delta_to_grid <= half_cell_width ? 0 : pcell_width));
+ const int x_end =
+ std::min(last_pixel_x_coordinate, key_x + key.kwidth + threshold);
+
+ int base_index_of_current_row =
+ (y_start / pcell_height) * pgrid_width + (x_start / pcell_width);
+ for (int center_y = y_start; center_y <= y_end; center_y += pcell_height) {
+ int index = base_index_of_current_row;
+ for (int center_x = x_start; center_x <= x_end; center_x += pcell_width) {
+ if (KeySet::SquaredDistanceToEdge(center_x, center_y, key) <
+ threshold_squared) {
+ neighbors_flat_buffer[index * KeySet::key_count +
+ neighbor_count_per_cell[index]] =
+ &KeySet::key_set[i];
+ ++neighbor_count_per_cell[index];
+ }
+ ++index;
+ }
+ base_index_of_current_row += pgrid_width;
+ }
+ }
+
+ for (int i = 0; i < grid_size; ++i) {
+ const int index_start = i * KeySet::key_count;
+ const int index_end = index_start + neighbor_count_per_cell[i];
+ pgrid_neighbors[i] = new const Key* [index_end - index_start];
+ for (int index = index_start; index < index_end; index++) {
+ pgrid_neighbors[i][index - index_start] = neighbors_flat_buffer[index];
+ }
+ }
+}
+
+} // namespace prediction

Powered by Google App Engine
This is Rietveld 408576698