Chromium Code Reviews| Index: services/keyboard_native/predictor.cc |
| diff --git a/services/keyboard_native/predictor.cc b/services/keyboard_native/predictor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f87a136ea0dc1003ee59c84d48caf95c2bccd0fc |
| --- /dev/null |
| +++ b/services/keyboard_native/predictor.cc |
| @@ -0,0 +1,110 @@ |
| +// 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 <iterator> |
| +#include <sstream> |
| + |
| +#include "base/bind.h" |
| +#include "services/keyboard_native/predictor.h" |
| +#include "services/keyboard_native/text_update_key.h" |
| + |
| +namespace keyboard { |
| + |
| +Predictor::Predictor(mojo::Shell* shell) { |
| + mojo::ServiceProviderPtr prediction_service_provider; |
| + shell->ConnectToApplication("mojo:prediction_service", |
| + mojo::GetProxy(&prediction_service_provider), |
| + nullptr); |
| + mojo::ConnectToService(prediction_service_provider.get(), |
| + &prediction_service_impl_); |
| + suggestion_keys_.clear(); |
| +} |
| + |
| +Predictor::~Predictor() { |
| +} |
| + |
| +void Predictor::SetSuggestionKeys( |
| + std::vector<KeyLayout::Key*> suggestion_keys) { |
| + 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
|
| + suggestion_keys_.push_back(suggestion_keys[i]); |
| + } |
| +} |
| + |
| +void Predictor::SetUpdateCallback(base::Callback<void()> on_update_callback) { |
| + on_update_callback_ = on_update_callback; |
| +} |
| + |
| +void Predictor::StoreCurWord(std::string new_word) { |
| + if (new_word == " ") { |
| + previous_words_.push_back(current_word_); |
| + current_word_ = ""; |
| + Predictor::ShowEmptySuggestion(); |
| + } else { |
| + current_word_ += new_word; |
| + Predictor::GetSuggestion(); |
| + } |
| +} |
| + |
| +int Predictor::ChoseSuggestWord(std::string suggested) { |
| + 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.
|
| + std::istringstream sug(suggested); |
| + std::istream_iterator<std::string> beg(sug), end; |
| + 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.
|
| + previous_words_.insert(previous_words_.end(), sugs.begin(), sugs.end()); |
| + current_word_ = ""; |
| + Predictor::ShowEmptySuggestion(); |
| + return old_size; |
| +} |
| + |
| +void Predictor::DeleteCharInCurWord() { |
| + 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.
|
| + 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.
|
| + if (current_word_.length() == 0) { |
| + Predictor::ShowEmptySuggestion(); |
| + } else { |
| + Predictor::GetSuggestion(); |
| + } |
| + } else if (previous_words_.size() != 0) { |
|
APW
2015/07/31 22:36:43
!empty()
riajiang
2015/08/01 01:23:08
Done.
|
| + current_word_ = previous_words_.back(); |
| + previous_words_.pop_back(); |
| + if (current_word_.length() > 0) |
|
APW
2015/07/31 22:36:43
!empty()
riajiang
2015/08/01 01:23:08
Done.
|
| + Predictor::GetSuggestion(); |
| + } |
| +} |
| + |
| +void Predictor::ShowEmptySuggestion() { |
| + for (size_t i = 0; i < suggestion_keys_.size(); i++) { |
| + ((TextUpdateKey*)suggestion_keys_[i])->ChangeText(""); |
|
APW
2015/07/31 22:36:43
static_cast<>
riajiang
2015/08/01 01:23:08
Done.
|
| + } |
| + on_update_callback_.Run(); |
| +} |
| + |
| +void Predictor::GetSuggestion() { |
| + prediction::PredictionInfoPtr prediction_info = |
| + prediction::PredictionInfo::New(); |
| + // we are not using bigram atm |
| + prediction_info->previous_words = |
| + mojo::Array<prediction::PrevWordInfoPtr>::New(0).Pass(); |
| + prediction_info->current_word = mojo::String(current_word_); |
| + |
| + prediction_service_impl_->GetPredictionList( |
| + prediction_info.Pass(), |
| + base::Bind(&Predictor::GetPredictionListAndEnd, base::Unretained(this))); |
| +} |
| + |
| +void Predictor::GetPredictionListAndEnd( |
| + const mojo::Array<mojo::String>& input_list) { |
| + for (size_t i = 0; i < suggestion_keys_.size(); i++) { |
| + std::string change_text; |
| + 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.
|
| + change_text = ""; |
| + } else { |
| + change_text = std::string(input_list[i].data()); |
| + } |
| + ((TextUpdateKey*)suggestion_keys_[i])->ChangeText(change_text); |
| + } |
| + on_update_callback_.Run(); |
| +} |
| + |
| +} // namespace keyboard |