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 #ifndef NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_FB_MODEL_LANG_ID_FROM_FB_H_ 18 #define NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_FB_MODEL_LANG_ID_FROM_FB_H_ 19 20 #include <stddef.h> 21 22 #include <memory> 23 #include <string> 24 25 #include "lang_id/lang-id.h" 26 27 namespace libtextclassifier3 { 28 namespace mobile { 29 namespace lang_id { 30 31 // Returns a LangId built using the SAFT model in flatbuffer format from 32 // |filename|. 33 std::unique_ptr<LangId> GetLangIdFromFlatbufferFile(const string &filename); 34 35 // Returns a LangId built using the SAFT model in flatbuffer format from 36 // given file descriptor. 37 std::unique_ptr<LangId> GetLangIdFromFlatbufferFileDescriptor(int fd); 38 39 // Returns a LangId built using the SAFT model in flatbuffer format from 40 // the |num_bytes| bytes that start at address |data|. 41 // 42 // IMPORTANT: the model bytes must be alive during the lifetime of the returned 43 // LangId. To avoid overhead (e.g., heap allocation), this method does not make 44 // a private copy of the model bytes. Avoiding overhead is the main reason we 45 // use flatbuffers. 46 std::unique_ptr<LangId> GetLangIdFromFlatbufferBytes(const char *data, 47 size_t num_bytes); 48 49 // Convenience string-based version of GetLangIdFromFlatbufferBytes. 50 // 51 // IMPORTANT: |bytes| must be alive during the lifetime of the returned LangId. GetLangIdFromFlatbufferBytes(const string & bytes)52inline std::unique_ptr<LangId> GetLangIdFromFlatbufferBytes( 53 const string &bytes) { 54 return GetLangIdFromFlatbufferBytes(bytes.data(), bytes.size()); 55 } 56 57 } // namespace lang_id 58 } // namespace mobile 59 } // namespace nlp_saft 60 61 #endif // NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_FB_MODEL_LANG_ID_FROM_FB_H_ 62