• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Helper utilities for testing Annotator.
18 
19 #ifndef LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
20 #define LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
21 
22 #include <memory>
23 #include <string>
24 
25 #include "annotator/model_generated.h"
26 #include "annotator/types.h"
27 #include "flatbuffers/flatbuffers.h"
28 
29 namespace libtextclassifier3 {
30 
31 // Loads FlatBuffer model, unpacks it and passes it to the visitor_fn so that it
32 // can modify it. Afterwards the modified unpacked model is serialized back to a
33 // flatbuffer.
34 template <typename Fn>
ModifyAnnotatorModel(const std::string & model_flatbuffer,Fn visitor_fn)35 std::string ModifyAnnotatorModel(const std::string& model_flatbuffer,
36                                  Fn visitor_fn) {
37   std::unique_ptr<ModelT> unpacked_model =
38       UnPackModel(model_flatbuffer.c_str());
39 
40   visitor_fn(unpacked_model.get());
41 
42   flatbuffers::FlatBufferBuilder builder;
43   FinishModelBuffer(builder, Model::Pack(builder, unpacked_model.get()));
44 
45   return std::string(reinterpret_cast<char*>(builder.GetBufferPointer()),
46                      builder.GetSize());
47 }
48 
49 }  // namespace libtextclassifier3
50 
51 #endif  // LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
52