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 #include "annotator/flatbuffer-utils.h" 18 19 #include <memory> 20 21 #include "utils/base/logging.h" 22 #include "utils/flatbuffers/flatbuffers.h" 23 #include "utils/flatbuffers/reflection.h" 24 #include "flatbuffers/reflection.h" 25 26 namespace libtextclassifier3 { 27 SwapFieldNamesForOffsetsInPath(ModelT * model)28bool SwapFieldNamesForOffsetsInPath(ModelT* model) { 29 if (model->regex_model == nullptr || model->entity_data_schema.empty()) { 30 // Nothing to do. 31 return true; 32 } 33 const reflection::Schema* schema = 34 LoadAndVerifyFlatbuffer<reflection::Schema>( 35 model->entity_data_schema.data(), model->entity_data_schema.size()); 36 37 for (std::unique_ptr<RegexModel_::PatternT>& pattern : 38 model->regex_model->patterns) { 39 for (std::unique_ptr<CapturingGroupT>& group : pattern->capturing_group) { 40 if (group->entity_field_path == nullptr) { 41 continue; 42 } 43 44 if (!SwapFieldNamesForOffsetsInPath(schema, 45 group->entity_field_path.get())) { 46 return false; 47 } 48 } 49 } 50 51 return true; 52 } 53 SwapFieldNamesForOffsetsInPathInSerializedModel(const std::string & model)54std::string SwapFieldNamesForOffsetsInPathInSerializedModel( 55 const std::string& model) { 56 std::unique_ptr<ModelT> unpacked_model = UnPackModel(model.c_str()); 57 TC3_CHECK(unpacked_model != nullptr); 58 TC3_CHECK(SwapFieldNamesForOffsetsInPath(unpacked_model.get())); 59 flatbuffers::FlatBufferBuilder builder; 60 FinishModelBuffer(builder, Model::Pack(builder, unpacked_model.get())); 61 return std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()), 62 builder.GetSize()); 63 } 64 CreateDatetimeSerializedEntityData(const DatetimeParseResult & parse_result)65std::string CreateDatetimeSerializedEntityData( 66 const DatetimeParseResult& parse_result) { 67 EntityDataT entity_data; 68 entity_data.datetime.reset(new EntityData_::DatetimeT()); 69 entity_data.datetime->time_ms_utc = parse_result.time_ms_utc; 70 entity_data.datetime->granularity = 71 static_cast<EntityData_::Datetime_::Granularity>( 72 parse_result.granularity); 73 74 for (const auto& c : parse_result.datetime_components) { 75 EntityData_::Datetime_::DatetimeComponentT datetime_component; 76 datetime_component.absolute_value = c.value; 77 datetime_component.relative_count = c.relative_count; 78 datetime_component.component_type = 79 static_cast<EntityData_::Datetime_::DatetimeComponent_::ComponentType>( 80 c.component_type); 81 datetime_component.relation_type = 82 EntityData_::Datetime_::DatetimeComponent_::RelationType_ABSOLUTE; 83 if (c.relative_qualifier != 84 DatetimeComponent::RelativeQualifier::UNSPECIFIED) { 85 datetime_component.relation_type = 86 EntityData_::Datetime_::DatetimeComponent_::RelationType_RELATIVE; 87 } 88 entity_data.datetime->datetime_component.emplace_back( 89 new EntityData_::Datetime_::DatetimeComponentT(datetime_component)); 90 } 91 return PackFlatbuffer<EntityData>(&entity_data); 92 } 93 94 } // namespace libtextclassifier3 95