Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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 | #ifndef ML_TEXT_CLASSIFIER_IMPL_H_ |
| 6 | #define ML_TEXT_CLASSIFIER_IMPL_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <annotator/annotator.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <mojo/public/cpp/bindings/binding.h> |
| 16 | #include <tensorflow/lite/model.h> |
| 17 | #include <utils/memory/mmap.h> |
| 18 | |
| 19 | #include "ml/graph_executor_impl.h" |
| 20 | #include "ml/mojom/model.mojom.h" |
| 21 | #include "ml/mojom/text_classifier.mojom.h" |
| 22 | |
| 23 | namespace ml { |
| 24 | |
| 25 | class TextClassifierImpl |
| 26 | : public chromeos::machine_learning::mojom::TextClassifier { |
| 27 | public: |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 28 | // Interface to create new `TextClassifierImpl` object. This function will |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 29 | // automatically achieve strong binding. The model object will be deleted when |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 30 | // the corresponding mojo connection meets error. |
| 31 | // Will return false if it fails to create the annotator object, otherwise |
| 32 | // return true. |
| 33 | static bool Create( |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 34 | std::unique_ptr<libtextclassifier3::ScopedMmap>* annotator_model_mmap, |
| 35 | const std::string& langid_model_path, |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 36 | chromeos::machine_learning::mojom::TextClassifierRequest request); |
| 37 | |
| 38 | private: |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 39 | // A private constructor, call `TextClassifierImpl::Create` to create new |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 40 | // objects. |
| 41 | explicit TextClassifierImpl( |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 42 | std::unique_ptr<libtextclassifier3::ScopedMmap>* annotator_model_mmap, |
| 43 | const std::string& langid_model_path, |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 44 | chromeos::machine_learning::mojom::TextClassifierRequest request); |
| 45 | |
| 46 | void SetConnectionErrorHandler(base::Closure connection_error_handler); |
| 47 | |
| 48 | // chromeos::machine_learning::mojom::TextClassifier: |
| 49 | void Annotate( |
| 50 | chromeos::machine_learning::mojom::TextAnnotationRequestPtr request, |
| 51 | AnnotateCallback callback) override; |
| 52 | |
| 53 | // chromeos::machine_learning::mojom::TextClassifier: |
| 54 | void SuggestSelection( |
| 55 | chromeos::machine_learning::mojom::TextSuggestSelectionRequestPtr request, |
| 56 | SuggestSelectionCallback callback) override; |
| 57 | |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 58 | // chromeos::machine_learning::mojom::TextClassifier: |
| 59 | void FindLanguages(const std::string& text, |
| 60 | FindLanguagesCallback callback) override; |
| 61 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 62 | std::unique_ptr<libtextclassifier3::Annotator> annotator_; |
| 63 | |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 64 | std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId> |
| 65 | language_identifier_; |
| 66 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 67 | mojo::Binding<chromeos::machine_learning::mojom::TextClassifier> binding_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(TextClassifierImpl); |
| 70 | }; |
| 71 | |
| 72 | } // namespace ml |
| 73 | |
| 74 | #endif // ML_TEXT_CLASSIFIER_IMPL_H_ |