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

Side by Side Diff: services/prediction/dictionary_service.cc

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fixed build problem 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 <algorithm>
6 #include <deque>
7 #include <fstream>
8 #include <string>
9
10 #include "base/base_paths.h"
11 #include "base/files/file_path.h"
12 #include "base/path_service.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "mojo/services/prediction/public/interfaces/prediction.mojom.h"
16 #include "mojo/tools/embed/data.h"
17 #include "services/prediction/dictionary_service.h"
18 #include "services/prediction/input_info.h"
19 #include "services/prediction/kDictFile.h"
20 #include "services/prediction/key_set.h"
21 #include "services/prediction/proximity_info_factory.h"
22 #include "third_party/prediction/suggest/core/dictionary/dictionary.h"
23 #include "third_party/prediction/suggest/core/result/suggestion_results.h"
24 #include "third_party/prediction/suggest/core/session/dic_traverse_session.h"
25 #include "third_party/prediction/suggest/core/session/prev_words_info.h"
26 #include "third_party/prediction/suggest/core/suggest_options.h"
27 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/diction ary_structure_with_buffer_policy_factory.h"
28
29 namespace prediction {
30
31 DictionaryService::DictionaryService() : max_suggestion_size_(50) {
32 }
33
34 DictionaryService::~DictionaryService() {
35 }
36
37 void DictionaryService::CreatDictFromEmbeddedDataIfNotExist(
38 const std::string path) {
39 if (std::ifstream(path.c_str()))
40 return;
41 std::ofstream dic_file(path.c_str(),
42 std::ofstream::out | std::ofstream::binary);
43 dic_file.write(prediction::kDictFile.data, prediction::kDictFile.size);
44 dic_file.close();
45 }
46
47 latinime::Dictionary* const DictionaryService::OpenDictionary(
48 const std::string path,
49 const int start_offset,
50 const int size,
51 const bool is_updatable) {
52 latinime::DictionaryStructureWithBufferPolicy::StructurePolicyPtr
53 dictionary_structure_with_buffer_policy(
54 latinime::DictionaryStructureWithBufferPolicyFactory::
55 newPolicyForExistingDictFile(path.c_str(), start_offset, size,
56 is_updatable));
57 if (!dictionary_structure_with_buffer_policy) {
58 return nullptr;
59 }
60
61 latinime::Dictionary* const dictionary = new latinime::Dictionary(
62 std::move(dictionary_structure_with_buffer_policy));
63 return dictionary;
64 }
65
66 mojo::Array<mojo::String> DictionaryService::GetDictionarySuggestion(
67 PredictionInfoPtr prediction_info,
68 latinime::ProximityInfo* proximity_info) {
69 mojo::Array<mojo::String> suggestion_words =
70 mojo::Array<mojo::String>::New(0);
71
72 // dictionary
73 base::FilePath dir_temp;
74 PathService::Get(base::DIR_TEMP, &dir_temp);
75 std::string path = dir_temp.value() + "/main_en.dict";
76 if (!default_dictionary_) {
77 CreatDictFromEmbeddedDataIfNotExist(path);
78 default_dictionary_ = scoped_ptr<latinime::Dictionary>(
79 OpenDictionary(path, 0, prediction::kDictFile.size, false));
80 if (!default_dictionary_) {
81 return suggestion_words.Clone().Pass();
82 }
83 }
84
85 // dic_traverse_session
86 if (!default_session_) {
87 default_session_ = scoped_ptr<latinime::DicTraverseSession>(
88 reinterpret_cast<latinime::DicTraverseSession*>(
89 latinime::DicTraverseSession::getSessionInstance(
90 "en", prediction::kDictFile.size)));
91 latinime::PrevWordsInfo empty_prev_words;
92 default_session_->init(default_dictionary_.get(), &empty_prev_words, 0);
93 }
94
95 // current word
96 InputInfo input_info;
97 input_info.ProcessInput(prediction_info->current_word);
98 int input_size = input_info.GetRealSize();
99
100 // previous words
101 latinime::PrevWordsInfo prev_words_info =
102 ProcessPrevWord(prediction_info->previous_words);
103
104 // suggestion options
105 // is_gesture, use_full_edit_distance,
106 // block_offensive_words, space_aware gesture_enabled
107 int options[] = {0, 0, 0, 0};
108 latinime::SuggestOptions suggest_options(options, arraysize(options));
109
110 latinime::SuggestionResults suggestion_results(max_suggestion_size_);
111 if (input_size > 0) {
112 default_dictionary_->getSuggestions(
113 proximity_info, default_session_.get(), input_info.GetXCoordinates(),
114 input_info.GetYCoordinates(), input_info.GetTimes(),
115 input_info.GetPointerIds(), input_info.GetCodepoints(), input_size,
116 &prev_words_info, &suggest_options, -1.0f, &suggestion_results);
117 } else {
118 default_dictionary_->getPredictions(&prev_words_info, &suggestion_results);
119 }
120
121 // process suggestion results
122 std::deque<std::string> suggestion_words_reverse;
123 char cur_beginning;
124 std::string lo_cur;
125 std::string up_cur;
126 if (input_size > 0) {
127 cur_beginning = prediction_info->current_word[0];
128 std::string cur_rest =
129 std::string(prediction_info->current_word.data())
130 .substr(1, prediction_info->current_word.size() - 1);
131 lo_cur = std::string(1, (char)tolower(cur_beginning)) + cur_rest;
132 up_cur = std::string(1, (char)toupper(cur_beginning)) + cur_rest;
133 }
134 while (!suggestion_results.mSuggestedWords.empty()) {
135 const latinime::SuggestedWord& suggested_word =
136 suggestion_results.mSuggestedWords.top();
137 base::string16 word;
138 for (int i = 0; i < suggested_word.getCodePointCount(); i++) {
139 base::char16 code_point = suggested_word.getCodePoint()[i];
140 word.push_back(code_point);
141 }
142 std::string word_string = base::UTF16ToUTF8(word);
143 if (word_string.compare(lo_cur) != 0 && word_string.compare(up_cur) != 0) {
144 if (isupper(cur_beginning))
145 word_string[0] = toupper(word_string[0]);
146 suggestion_words_reverse.push_front(word_string);
147 }
148 suggestion_results.mSuggestedWords.pop();
149 }
150
151 // remove dups within suggestion words
152 for (size_t i = 0; i < suggestion_words_reverse.size(); i++) {
153 for (size_t j = i + 1; j < suggestion_words_reverse.size(); j++) {
154 if (suggestion_words_reverse[i].compare(suggestion_words_reverse[j]) ==
155 0) {
156 suggestion_words_reverse.erase(suggestion_words_reverse.begin() + j);
157 j--;
158 }
159 }
160 }
161
162 for (std::deque<std::string>::iterator it = suggestion_words_reverse.begin();
163 it != suggestion_words_reverse.end(); ++it) {
164 suggestion_words.push_back(mojo::String(*it));
165 }
166
167 return suggestion_words.Clone().Pass();
168 }
169
170 // modified from Android JniDataUtils::constructPrevWordsInfo
171 latinime::PrevWordsInfo DictionaryService::ProcessPrevWord(
172 mojo::Array<PrevWordInfoPtr>& prev_words) {
173 int prev_word_codepoints[MAX_PREV_WORD_COUNT_FOR_N_GRAM][MAX_WORD_LENGTH];
174 int prev_word_codepoint_count[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
175 bool are_beginning_of_sentence[MAX_PREV_WORD_COUNT_FOR_N_GRAM];
176 int prevwords_count = std::min(
177 prev_words.size(), static_cast<size_t>(MAX_PREV_WORD_COUNT_FOR_N_GRAM));
178 for (int i = 0; i < prevwords_count; ++i) {
179 prev_word_codepoint_count[i] = 0;
180 are_beginning_of_sentence[i] = false;
181 int prev_word_size = prev_words[i]->word.size();
182 if (prev_word_size > MAX_WORD_LENGTH) {
183 continue;
184 }
185 for (int j = 0; j < prev_word_size; j++) {
186 prev_word_codepoints[i][j] = (int)((prev_words[i])->word)[j];
187 }
188 prev_word_codepoint_count[i] = prev_word_size;
189 are_beginning_of_sentence[i] = prev_words[i]->is_beginning_of_sentence;
190 }
191 latinime::PrevWordsInfo prev_words_info =
192 latinime::PrevWordsInfo(prev_word_codepoints, prev_word_codepoint_count,
193 are_beginning_of_sentence, prevwords_count);
194 return prev_words_info;
195 }
196
197 } // namespace prediction
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698