1*993b0882SAndroid Build Coastguard Worker /*
2*993b0882SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*993b0882SAndroid Build Coastguard Worker *
4*993b0882SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*993b0882SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*993b0882SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*993b0882SAndroid Build Coastguard Worker *
8*993b0882SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*993b0882SAndroid Build Coastguard Worker *
10*993b0882SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*993b0882SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*993b0882SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*993b0882SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*993b0882SAndroid Build Coastguard Worker * limitations under the License.
15*993b0882SAndroid Build Coastguard Worker */
16*993b0882SAndroid Build Coastguard Worker
17*993b0882SAndroid Build Coastguard Worker // Helper utilities for testing Annotator.
18*993b0882SAndroid Build Coastguard Worker
19*993b0882SAndroid Build Coastguard Worker #ifndef LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
20*993b0882SAndroid Build Coastguard Worker #define LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
21*993b0882SAndroid Build Coastguard Worker
22*993b0882SAndroid Build Coastguard Worker #include <fstream>
23*993b0882SAndroid Build Coastguard Worker #include <memory>
24*993b0882SAndroid Build Coastguard Worker #include <string>
25*993b0882SAndroid Build Coastguard Worker
26*993b0882SAndroid Build Coastguard Worker #include "annotator/model_generated.h"
27*993b0882SAndroid Build Coastguard Worker #include "annotator/types.h"
28*993b0882SAndroid Build Coastguard Worker #include "flatbuffers/flatbuffers.h"
29*993b0882SAndroid Build Coastguard Worker
30*993b0882SAndroid Build Coastguard Worker namespace libtextclassifier3 {
31*993b0882SAndroid Build Coastguard Worker
32*993b0882SAndroid Build Coastguard Worker // Loads FlatBuffer model, unpacks it and passes it to the visitor_fn so that it
33*993b0882SAndroid Build Coastguard Worker // can modify it. Afterwards the modified unpacked model is serialized back to a
34*993b0882SAndroid Build Coastguard Worker // flatbuffer.
35*993b0882SAndroid Build Coastguard Worker template <typename Fn>
ModifyAnnotatorModel(const std::string & model_flatbuffer,Fn visitor_fn)36*993b0882SAndroid Build Coastguard Worker std::string ModifyAnnotatorModel(const std::string& model_flatbuffer,
37*993b0882SAndroid Build Coastguard Worker Fn visitor_fn) {
38*993b0882SAndroid Build Coastguard Worker std::unique_ptr<ModelT> unpacked_model =
39*993b0882SAndroid Build Coastguard Worker UnPackModel(model_flatbuffer.c_str());
40*993b0882SAndroid Build Coastguard Worker
41*993b0882SAndroid Build Coastguard Worker visitor_fn(unpacked_model.get());
42*993b0882SAndroid Build Coastguard Worker
43*993b0882SAndroid Build Coastguard Worker flatbuffers::FlatBufferBuilder builder;
44*993b0882SAndroid Build Coastguard Worker FinishModelBuffer(builder, Model::Pack(builder, unpacked_model.get()));
45*993b0882SAndroid Build Coastguard Worker
46*993b0882SAndroid Build Coastguard Worker return std::string(reinterpret_cast<char*>(builder.GetBufferPointer()),
47*993b0882SAndroid Build Coastguard Worker builder.GetSize());
48*993b0882SAndroid Build Coastguard Worker }
49*993b0882SAndroid Build Coastguard Worker
50*993b0882SAndroid Build Coastguard Worker std::string FirstResult(const std::vector<ClassificationResult>& results);
51*993b0882SAndroid Build Coastguard Worker
52*993b0882SAndroid Build Coastguard Worker std::string ReadFile(const std::string& file_name);
53*993b0882SAndroid Build Coastguard Worker
54*993b0882SAndroid Build Coastguard Worker std::unique_ptr<RegexModel_::PatternT> MakePattern(
55*993b0882SAndroid Build Coastguard Worker const std::string& collection_name, const std::string& pattern,
56*993b0882SAndroid Build Coastguard Worker const bool enabled_for_classification, const bool enabled_for_selection,
57*993b0882SAndroid Build Coastguard Worker const bool enabled_for_annotation, const float score,
58*993b0882SAndroid Build Coastguard Worker const float priority_score);
59*993b0882SAndroid Build Coastguard Worker
60*993b0882SAndroid Build Coastguard Worker // Shortcut function that doesn't need to specify the priority score.
61*993b0882SAndroid Build Coastguard Worker std::unique_ptr<RegexModel_::PatternT> MakePattern(
62*993b0882SAndroid Build Coastguard Worker const std::string& collection_name, const std::string& pattern,
63*993b0882SAndroid Build Coastguard Worker const bool enabled_for_classification, const bool enabled_for_selection,
64*993b0882SAndroid Build Coastguard Worker const bool enabled_for_annotation, const float score);
65*993b0882SAndroid Build Coastguard Worker
66*993b0882SAndroid Build Coastguard Worker void AddTestRegexModel(ModelT* unpacked_model);
67*993b0882SAndroid Build Coastguard Worker
68*993b0882SAndroid Build Coastguard Worker // Creates empty serialized Annotator model. Takes optional function that can
69*993b0882SAndroid Build Coastguard Worker // modify the unpacked ModelT before the serialization.
70*993b0882SAndroid Build Coastguard Worker std::string CreateEmptyModel(
71*993b0882SAndroid Build Coastguard Worker const std::function<void(ModelT* model)> model_update_fn =
72*993b0882SAndroid Build Coastguard Worker [](ModelT* model) {});
73*993b0882SAndroid Build Coastguard Worker
74*993b0882SAndroid Build Coastguard Worker // Create fake entity data schema meta data.
75*993b0882SAndroid Build Coastguard Worker void AddTestEntitySchemaData(ModelT* unpacked_model);
76*993b0882SAndroid Build Coastguard Worker
77*993b0882SAndroid Build Coastguard Worker AnnotatedSpan MakeAnnotatedSpan(
78*993b0882SAndroid Build Coastguard Worker CodepointSpan span, const std::string& collection, const float score,
79*993b0882SAndroid Build Coastguard Worker AnnotatedSpan::Source source = AnnotatedSpan::Source::OTHER);
80*993b0882SAndroid Build Coastguard Worker
81*993b0882SAndroid Build Coastguard Worker } // namespace libtextclassifier3
82*993b0882SAndroid Build Coastguard Worker
83*993b0882SAndroid Build Coastguard Worker #endif // LIBTEXTCLASSIFIER_UTILS_TESTING_ANNOTATOR_H_
84