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 <fstream>
23 #include <memory>
24 #include <string>
25
26 #include "annotator/model_generated.h"
27 #include "annotator/types.h"
28 #include "flatbuffers/flatbuffers.h"
29
30 namespace libtextclassifier3 {
31
32 // Loads FlatBuffer model, unpacks it and passes it to the visitor_fn so that it
33 // can modify it. Afterwards the modified unpacked model is serialized back to a
34 // flatbuffer.
35 template <typename Fn>
ModifyAnnotatorModel(const std::string & model_flatbuffer,Fn visitor_fn)36 std::string ModifyAnnotatorModel(const std::string& model_flatbuffer,
37 Fn visitor_fn) {
38 std::unique_ptr<ModelT> unpacked_model =
39 UnPackModel(model_flatbuffer.c_str());
40
41 visitor_fn(unpacked_model.get());
42
43 flatbuffers::FlatBufferBuilder builder;
44 FinishModelBuffer(builder, Model::Pack(builder, unpacked_model.get()));
45
46 return std::string(reinterpret_cast<char*>(builder.GetBufferPointer()),
47 builder.GetSize());
48 }
49
50 std::string FirstResult(const std::vector<ClassificationResult>& results);
51
52 std::string ReadFile(const std::string& file_name);
53
54 std::unique_ptr<RegexModel_::PatternT> MakePattern(
55 const std::string& collection_name, const std::string& pattern,
56 const bool enabled_for_classification, const bool enabled_for_selection,
57 const bool enabled_for_annotation, const float score,
58 const float priority_score);
59
60 // Shortcut function that doesn't need to specify the priority score.
61 std::unique_ptr<RegexModel_::PatternT> MakePattern(
62 const std::string& collection_name, const std::string& pattern,
63 const bool enabled_for_classification, const bool enabled_for_selection,
64 const bool enabled_for_annotation, const float score);
65
66 void AddTestRegexModel(ModelT* unpacked_model);
67
68 // Creates empty serialized Annotator model. Takes optional function that can
69 // modify the unpacked ModelT before the serialization.
70 std::string CreateEmptyModel(
71 const std::function<void(ModelT* model)> model_update_fn =
72 [](ModelT* model) {});
73
74 // Create fake entity data schema meta data.
75 void AddTestEntitySchemaData(ModelT* unpacked_model);
76
77 AnnotatedSpan MakeAnnotatedSpan(
78 CodepointSpan span, const std::string& collection, const float score,
79 AnnotatedSpan::Source source = AnnotatedSpan::Source::OTHER);
80
81 } // namespace libtextclassifier3
82
83 #endif // LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
84