blob: e12fc831da2a6c6b7957c38ae0641178a628dbfd [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>
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
23namespace ml {
24
25class TextClassifierImpl
26 : public chromeos::machine_learning::mojom::TextClassifier {
27 public:
Andrew Moylan79b34a42020-07-08 11:13:11 +100028 // Interface to create new `TextClassifierImpl` object. This function will
Honglin Yuc5100022020-07-09 11:54:27 +100029 // automatically achieve strong binding. The model object will be deleted when
Honglin Yuf33dce32019-12-05 15:10:39 +110030 // 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 Yuc5100022020-07-09 11:54:27 +100034 std::unique_ptr<libtextclassifier3::ScopedMmap>* annotator_model_mmap,
35 const std::string& langid_model_path,
Honglin Yuf33dce32019-12-05 15:10:39 +110036 chromeos::machine_learning::mojom::TextClassifierRequest request);
37
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(
Honglin Yuc5100022020-07-09 11:54:27 +100042 std::unique_ptr<libtextclassifier3::ScopedMmap>* annotator_model_mmap,
43 const std::string& langid_model_path,
Honglin Yuf33dce32019-12-05 15:10:39 +110044 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 Yuc5100022020-07-09 11:54:27 +100058 // chromeos::machine_learning::mojom::TextClassifier:
59 void FindLanguages(const std::string& text,
60 FindLanguagesCallback callback) override;
61
Honglin Yuf33dce32019-12-05 15:10:39 +110062 std::unique_ptr<libtextclassifier3::Annotator> annotator_;
63
Honglin Yuc5100022020-07-09 11:54:27 +100064 std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId>
65 language_identifier_;
66
Honglin Yuf33dce32019-12-05 15:10:39 +110067 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_