OLD | NEW |
---|---|
(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 for (size_t del_count = 0; del_count < old_size - new_size; del_count++) { | |
APW
2015/08/03 21:38:57
You don't need loops for either of these, I think
riajiang
2015/08/03 22:25:05
Done.
| |
44 suggestion_keys_.erase(suggestion_keys_.end() - 1); | |
45 } | |
46 } else if (old_size < new_size) { | |
47 for (size_t add_count = 0; add_count < new_size - old_size; add_count++) { | |
48 suggestion_keys_.push_back(suggestion_keys[keyloop_size + add_count]); | |
49 } | |
50 } | |
51 } | |
52 | |
53 void Predictor::SetUpdateCallback(base::Callback<void()> on_update_callback) { | |
54 on_update_callback_ = on_update_callback; | |
55 } | |
56 | |
57 void Predictor::StoreCurWord(std::string new_word) { | |
58 if (new_word == " ") { | |
59 previous_words_.push_back(current_word_); | |
60 current_word_ = ""; | |
61 Predictor::ShowEmptySuggestion(); | |
62 } else { | |
63 current_word_ += new_word; | |
64 Predictor::GetSuggestion(); | |
65 } | |
66 } | |
67 | |
68 int Predictor::ChooseSuggestedWord(std::string suggested) { | |
69 int old_size = static_cast<int>(current_word_.size()); | |
70 // split suggested by space into a vector | |
71 std::istringstream sug(suggested); | |
72 std::istream_iterator<std::string> beg(sug), end; | |
73 std::vector<std::string> sugs(beg, end); | |
74 previous_words_.insert(previous_words_.end(), sugs.begin(), sugs.end()); | |
75 current_word_ = ""; | |
76 Predictor::ShowEmptySuggestion(); | |
77 return old_size; | |
78 } | |
79 | |
80 void Predictor::DeleteCharInCurWord() { | |
81 if (!current_word_.empty()) { | |
82 current_word_.erase(current_word_.end() - 1); | |
83 if (current_word_.empty()) { | |
84 Predictor::ShowEmptySuggestion(); | |
85 } else { | |
86 Predictor::GetSuggestion(); | |
87 } | |
88 } else if (!previous_words_.empty()) { | |
89 current_word_ = previous_words_.back(); | |
90 previous_words_.pop_back(); | |
91 if (!current_word_.empty()) | |
92 Predictor::GetSuggestion(); | |
93 } | |
94 } | |
95 | |
96 void Predictor::ShowEmptySuggestion() { | |
97 for (size_t i = 0; i < suggestion_keys_.size(); i++) { | |
98 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText(""); | |
99 } | |
100 on_update_callback_.Run(); | |
101 } | |
102 | |
103 void Predictor::GetSuggestion() { | |
104 prediction::PredictionInfoPtr prediction_info = | |
105 prediction::PredictionInfo::New(); | |
106 // we are not using bigram atm | |
107 prediction_info->previous_words = | |
108 mojo::Array<prediction::PrevWordInfoPtr>::New(0).Pass(); | |
109 prediction_info->current_word = mojo::String(current_word_); | |
110 | |
111 prediction_service_impl_->GetPredictionList( | |
112 prediction_info.Pass(), | |
113 base::Bind(&Predictor::GetPredictionListAndEnd, base::Unretained(this))); | |
114 } | |
115 | |
116 void Predictor::GetPredictionListAndEnd( | |
117 const mojo::Array<mojo::String>& input_list) { | |
118 for (size_t i = 0; i < suggestion_keys_.size(); i++) { | |
119 std::string change_text; | |
120 if (i < input_list.size()) { | |
121 change_text = std::string(input_list[i].data()); | |
122 } else { | |
123 change_text = ""; | |
124 } | |
125 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText(change_text); | |
126 } | |
127 on_update_callback_.Run(); | |
128 } | |
129 | |
130 } // namespace keyboard | |
OLD | NEW |