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 for (size_t i = 0; i < suggestion_keys.size(); i++) { | |
32 if (old_size != 0) { | |
APW
2015/08/03 17:26:21
you've got two issues if the new vector is a diffe
riajiang
2015/08/03 19:39:42
Changed to use min of new_size and old_size
| |
33 static_cast<TextUpdateKey*>(suggestion_keys[i]) | |
34 ->ChangeText(suggestion_keys_[i]->ToText()); | |
35 suggestion_keys_[i] = suggestion_keys[i]; | |
36 } else { | |
37 suggestion_keys_.push_back(suggestion_keys[i]); | |
38 } | |
39 } | |
40 } | |
41 | |
42 void Predictor::SetUpdateCallback(base::Callback<void()> on_update_callback) { | |
43 on_update_callback_ = on_update_callback; | |
44 } | |
45 | |
46 void Predictor::StoreCurWord(std::string new_word) { | |
47 if (new_word == " ") { | |
48 previous_words_.push_back(current_word_); | |
49 current_word_ = ""; | |
50 Predictor::ShowEmptySuggestion(); | |
51 } else { | |
52 current_word_ += new_word; | |
53 Predictor::GetSuggestion(); | |
54 } | |
55 } | |
56 | |
57 int Predictor::ChooseSuggestedWord(std::string suggested) { | |
58 int old_size = static_cast<int>(current_word_.size()); | |
59 // split suggested by space into a vector | |
60 std::istringstream sug(suggested); | |
61 std::istream_iterator<std::string> beg(sug), end; | |
62 std::vector<std::string> sugs(beg, end); | |
63 previous_words_.insert(previous_words_.end(), sugs.begin(), sugs.end()); | |
64 current_word_ = ""; | |
65 Predictor::ShowEmptySuggestion(); | |
66 return old_size; | |
67 } | |
68 | |
69 void Predictor::DeleteCharInCurWord() { | |
70 if (!current_word_.empty()) { | |
71 current_word_.pop_back(); | |
72 if (current_word_.length() == 0) { | |
APW
2015/08/03 17:26:21
empty
riajiang
2015/08/03 19:39:42
Done.
| |
73 Predictor::ShowEmptySuggestion(); | |
74 } else { | |
75 Predictor::GetSuggestion(); | |
76 } | |
77 } else if (!previous_words_.empty()) { | |
78 current_word_ = previous_words_.back(); | |
79 previous_words_.pop_back(); | |
80 if (!current_word_.empty()) | |
81 Predictor::GetSuggestion(); | |
82 } | |
83 } | |
84 | |
85 void Predictor::ShowEmptySuggestion() { | |
86 for (size_t i = 0; i < suggestion_keys_.size(); i++) { | |
87 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText(""); | |
88 } | |
89 on_update_callback_.Run(); | |
90 } | |
91 | |
92 void Predictor::GetSuggestion() { | |
93 prediction::PredictionInfoPtr prediction_info = | |
94 prediction::PredictionInfo::New(); | |
95 // we are not using bigram atm | |
96 prediction_info->previous_words = | |
97 mojo::Array<prediction::PrevWordInfoPtr>::New(0).Pass(); | |
98 prediction_info->current_word = mojo::String(current_word_); | |
99 | |
100 prediction_service_impl_->GetPredictionList( | |
101 prediction_info.Pass(), | |
102 base::Bind(&Predictor::GetPredictionListAndEnd, base::Unretained(this))); | |
103 } | |
104 | |
105 void Predictor::GetPredictionListAndEnd( | |
106 const mojo::Array<mojo::String>& input_list) { | |
107 for (size_t i = 0; i < suggestion_keys_.size(); i++) { | |
108 std::string change_text; | |
109 if (i < input_list.size()) { | |
110 change_text = std::string(input_list[i].data()); | |
111 } else { | |
112 change_text = ""; | |
113 } | |
114 static_cast<TextUpdateKey*>(suggestion_keys_[i])->ChangeText(change_text); | |
115 } | |
116 on_update_callback_.Run(); | |
117 } | |
118 | |
119 } // namespace keyboard | |
OLD | NEW |