blob: 612b1d5ac9a05b02f5a3b218b48b7ff4b58ee553 [file] [log] [blame]
Honglin Yuf33dce32019-12-05 15:10:39 +11001// 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>
Andrew Moylanb481af72020-07-09 15:22:00 +100015#include <mojo/public/cpp/bindings/pending_receiver.h>
16#include <mojo/public/cpp/bindings/receiver.h>
Honglin Yuf33dce32019-12-05 15:10:39 +110017#include <tensorflow/lite/model.h>
18#include <utils/memory/mmap.h>
19
20#include "ml/graph_executor_impl.h"
21#include "ml/mojom/model.mojom.h"
22#include "ml/mojom/text_classifier.mojom.h"
23
24namespace ml {
25
26class TextClassifierImpl
27 : public chromeos::machine_learning::mojom::TextClassifier {
28 public:
Andrew Moylan79b34a42020-07-08 11:13:11 +100029 // Interface to create new `TextClassifierImpl` object. This function will
Andrew Moylanb481af72020-07-09 15:22:00 +100030 // automatically achieve strong binding.The model object will be deleted when
31 // the corresponding mojo connection is closed.
Honglin Yuf33dce32019-12-05 15:10:39 +110032 // Will return false if it fails to create the annotator object, otherwise
33 // return true.
34 static bool Create(
Andrew Moylanb481af72020-07-09 15:22:00 +100035 mojo::PendingReceiver<chromeos::machine_learning::mojom::TextClassifier>
36 receiver);
Honglin Yuf33dce32019-12-05 15:10:39 +110037
38 private:
Andrew Moylan79b34a42020-07-08 11:13:11 +100039 // A private constructor, call `TextClassifierImpl::Create` to create new
Honglin Yuf33dce32019-12-05 15:10:39 +110040 // objects.
41 explicit TextClassifierImpl(
Andrew Moylanb481af72020-07-09 15:22:00 +100042 std::unique_ptr<libtextclassifier3::ScopedMmap>* mmap,
Honglin Yuc5100022020-07-09 11:54:27 +100043 const std::string& langid_model_path,
Andrew Moylanb481af72020-07-09 15:22:00 +100044 mojo::PendingReceiver<chromeos::machine_learning::mojom::TextClassifier>
45 receiver);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090046 TextClassifierImpl(const TextClassifierImpl&) = delete;
47 TextClassifierImpl& operator=(const TextClassifierImpl&) = delete;
Honglin Yuf33dce32019-12-05 15:10:39 +110048
Andrew Moylanb481af72020-07-09 15:22:00 +100049 void SetDisconnectionHandler(base::Closure disconnect_handler);
Honglin Yuf33dce32019-12-05 15:10:39 +110050
51 // chromeos::machine_learning::mojom::TextClassifier:
52 void Annotate(
53 chromeos::machine_learning::mojom::TextAnnotationRequestPtr request,
54 AnnotateCallback callback) override;
55
56 // chromeos::machine_learning::mojom::TextClassifier:
57 void SuggestSelection(
58 chromeos::machine_learning::mojom::TextSuggestSelectionRequestPtr request,
59 SuggestSelectionCallback callback) override;
60
Honglin Yuc5100022020-07-09 11:54:27 +100061 // chromeos::machine_learning::mojom::TextClassifier:
62 void FindLanguages(const std::string& text,
63 FindLanguagesCallback callback) override;
64
Honglin Yuf33dce32019-12-05 15:10:39 +110065 std::unique_ptr<libtextclassifier3::Annotator> annotator_;
66
Honglin Yuc5100022020-07-09 11:54:27 +100067 std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId>
68 language_identifier_;
69
Andrew Moylanb481af72020-07-09 15:22:00 +100070 mojo::Receiver<chromeos::machine_learning::mojom::TextClassifier> receiver_;
Honglin Yuf33dce32019-12-05 15:10:39 +110071};
72
73} // namespace ml
74
75#endif // ML_TEXT_CLASSIFIER_IMPL_H_