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

Side by Side Diff: services/keyboard_native/predictor.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, 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 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <iterator>
6 #include <sstream>
7
8 #include "base/bind.h"
9 #include "services/keyboard_native/predictor.h"
10 #include "services/keyboard_native/text_update_key.h"
11
12 namespace keyboard {
13
14 Predictor::Predictor(mojo::Shell* shell) {
15 mojo::ServiceProviderPtr prediction_service_provider;
16 shell->ConnectToApplication("mojo:prediction_service",
17 mojo::GetProxy(&prediction_service_provider),
18 nullptr);
19 mojo::ConnectToService(prediction_service_provider.get(),
20 &prediction_service_impl_);
21 suggestion_keys_.clear();
22 }
23
24 Predictor::~Predictor() {
25 }
26
27 void Predictor::SetSuggestionKeys(
28 std::vector<KeyLayout::Key*> suggestion_keys) {
29 for (size_t i = 0; i < suggestion_keys.size(); i++) {
APW 2015/07/31 22:36:43 claer before adding - use begin and end to add ins
riajiang 2015/08/01 01:23:08 I changed it to use different suggestion keys in d
30 suggestion_keys_.push_back(suggestion_keys[i]);
31 }
32 }
33
34 void Predictor::SetUpdateCallback(base::Callback<void()> on_update_callback) {
35 on_update_callback_ = on_update_callback;
36 }
37
38 void Predictor::StoreCurWord(std::string new_word) {
39 if (new_word == " ") {
40 previous_words_.push_back(current_word_);
41 current_word_ = "";
42 Predictor::ShowEmptySuggestion();
43 } else {
44 current_word_ += new_word;
45 Predictor::GetSuggestion();
46 }
47 }
48
49 int Predictor::ChoseSuggestWord(std::string suggested) {
50 int old_size = (int)current_word_.size();
APW 2015/07/31 22:36:43 static_cast<int> or use size_t's
riajiang 2015/08/01 01:23:08 Done.
51 std::istringstream sug(suggested);
52 std::istream_iterator<std::string> beg(sug), end;
53 std::vector<std::string> sugs(beg, end);
APW 2015/07/31 22:36:43 Add a comment about crazy code is doing
riajiang 2015/08/01 01:23:08 Done.
54 previous_words_.insert(previous_words_.end(), sugs.begin(), sugs.end());
55 current_word_ = "";
56 Predictor::ShowEmptySuggestion();
57 return old_size;
58 }
59
60 void Predictor::DeleteCharInCurWord() {
61 if (current_word_.length() > 0) {
APW 2015/07/31 22:36:43 use !empty() or empty() instead of size() > 0 or =
riajiang 2015/08/01 01:23:08 Done.
62 current_word_ = current_word_.substr(0, current_word_.length() - 1);
APW 2015/07/31 22:36:43 use pop_back()
riajiang 2015/08/01 01:23:08 Done.
63 if (current_word_.length() == 0) {
64 Predictor::ShowEmptySuggestion();
65 } else {
66 Predictor::GetSuggestion();
67 }
68 } else if (previous_words_.size() != 0) {
APW 2015/07/31 22:36:43 !empty()
riajiang 2015/08/01 01:23:08 Done.
69 current_word_ = previous_words_.back();
70 previous_words_.pop_back();
71 if (current_word_.length() > 0)
APW 2015/07/31 22:36:43 !empty()
riajiang 2015/08/01 01:23:08 Done.
72 Predictor::GetSuggestion();
73 }
74 }
75
76 void Predictor::ShowEmptySuggestion() {
77 for (size_t i = 0; i < suggestion_keys_.size(); i++) {
78 ((TextUpdateKey*)suggestion_keys_[i])->ChangeText("");
APW 2015/07/31 22:36:43 static_cast<>
riajiang 2015/08/01 01:23:08 Done.
79 }
80 on_update_callback_.Run();
81 }
82
83 void Predictor::GetSuggestion() {
84 prediction::PredictionInfoPtr prediction_info =
85 prediction::PredictionInfo::New();
86 // we are not using bigram atm
87 prediction_info->previous_words =
88 mojo::Array<prediction::PrevWordInfoPtr>::New(0).Pass();
89 prediction_info->current_word = mojo::String(current_word_);
90
91 prediction_service_impl_->GetPredictionList(
92 prediction_info.Pass(),
93 base::Bind(&Predictor::GetPredictionListAndEnd, base::Unretained(this)));
94 }
95
96 void Predictor::GetPredictionListAndEnd(
97 const mojo::Array<mojo::String>& input_list) {
98 for (size_t i = 0; i < suggestion_keys_.size(); i++) {
99 std::string change_text;
100 if (i + 1 >= input_list.size()) {
APW 2015/07/31 22:36:43 invert and use i < input_list.size()
riajiang 2015/08/01 01:23:08 Done.
101 change_text = "";
102 } else {
103 change_text = std::string(input_list[i].data());
104 }
105 ((TextUpdateKey*)suggestion_keys_[i])->ChangeText(change_text);
106 }
107 on_update_callback_.Run();
108 }
109
110 } // namespace keyboard
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698