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

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: format README and 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
« no previous file with comments | « services/keyboard_native/predictor.h ('k') | services/keyboard_native/text_update_key.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include <string>
8
9 #include "base/bind.h"
10 #include "services/keyboard_native/predictor.h"
11 #include "services/keyboard_native/text_update_key.h"
12
13 namespace keyboard {
14
15 Predictor::Predictor(mojo::Shell* shell) {
16 mojo::ServiceProviderPtr prediction_service_provider;
17 shell->ConnectToApplication("mojo:prediction_service",
18 mojo::GetProxy(&prediction_service_provider),
19 nullptr);
20 mojo::ConnectToService(prediction_service_provider.get(),
21 &prediction_service_impl_);
22 suggestion_keys_.clear();
23 }
24
25 Predictor::~Predictor() {
26 }
27
28 void Predictor::SetSuggestionKeys(
29 std::vector<KeyLayout::Key*> suggestion_keys) {
30 size_t old_size = suggestion_keys_.size();
31 size_t new_size = suggestion_keys.size();
32 size_t keyloop_size = std::min(old_size, new_size);
33 for (size_t i = 0; i < keyloop_size; i++) {
34 if (old_size != 0) {
35 static_cast<TextUpdateKey*>(suggestion_keys[i])
36 ->ChangeText(suggestion_keys_[i]->ToText());
37 suggestion_keys_[i] = suggestion_keys[i];
38 } else {
39 suggestion_keys_.push_back(suggestion_keys[i]);
40 }
41 }
42 if (new_size < old_size) {
43 suggestion_keys_.erase(suggestion_keys_.begin() + new_size,
44 suggestion_keys_.end());
45 } else if (old_size < new_size) {
46 suggestion_keys_.insert(suggestion_keys_.end(),
47 suggestion_keys.begin() + old_size,
48 suggestion_keys.end());
49 }
50 }
51
52 void Predictor::SetUpdateCallback(base::Callback<void()> on_update_callback) {
53 on_update_callback_ = on_update_callback;
54 }
55
56 void Predictor::StoreCurWord(std::string new_word) {
57 if (new_word == " ") {
58 previous_words_.push_back(current_word_);
59 current_word_ = "";
60 Predictor::ShowEmptySuggestion();
61 } else {
62 current_word_ += new_word;
63 Predictor::GetSuggestion();
64 }
65 }
66
67 int Predictor::ChooseSuggestedWord(std::string suggested) {
68 int old_size = static_cast<int>(current_word_.size());
69 // split suggested by space into a vector
70 std::istringstream sug(suggested);
71 std::istream_iterator<std::string> beg(sug), end;
72 std::vector<std::string> sugs(beg, end);
73 previous_words_.insert(previous_words_.end(), sugs.begin(), sugs.end());
74 current_word_ = "";
75 Predictor::ShowEmptySuggestion();
76 return old_size;
77 }
78
79 void Predictor::DeleteCharInCurWord() {
80 if (!current_word_.empty()) {
81 current_word_.erase(current_word_.end() - 1);
82 if (current_word_.empty()) {
83 Predictor::ShowEmptySuggestion();
84 } else {
85 Predictor::GetSuggestion();
86 }
87 } else if (!previous_words_.empty()) {
88 current_word_ = previous_words_.back();
89 previous_words_.pop_back();
90 if (!current_word_.empty())
91 Predictor::GetSuggestion();
92 }
93 }
94
95 void Predictor::ShowEmptySuggestion() {
96 for (size_t i = 0; i < suggestion_keys_.size(); i++) {
97 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText("");
98 }
99 on_update_callback_.Run();
100 }
101
102 void Predictor::GetSuggestion() {
103 prediction::PredictionInfoPtr prediction_info =
104 prediction::PredictionInfo::New();
105 // we are not using bigram atm
106 prediction_info->previous_words =
107 mojo::Array<prediction::PrevWordInfoPtr>::New(0).Pass();
108 prediction_info->current_word = mojo::String(current_word_);
109
110 prediction_service_impl_->GetPredictionList(
111 prediction_info.Pass(),
112 base::Bind(&Predictor::GetPredictionListAndEnd, base::Unretained(this)));
113 }
114
115 void Predictor::GetPredictionListAndEnd(
116 const mojo::Array<mojo::String>& input_list) {
117 for (size_t i = 0; i < suggestion_keys_.size(); i++) {
118 std::string change_text;
119 if (i < input_list.size()) {
120 change_text = std::string(input_list[i].data());
121 } else {
122 change_text = "";
123 }
124 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText(change_text);
125 }
126 on_update_callback_.Run();
127 }
128
129 } // namespace keyboard
OLDNEW
« no previous file with comments | « services/keyboard_native/predictor.h ('k') | services/keyboard_native/text_update_key.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698