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 LIBTEXTCLASSIFIER_LANG_ID_LANG_ID_WRAPPER_H_ 18 #define LIBTEXTCLASSIFIER_LANG_ID_LANG_ID_WRAPPER_H_ 19 20 #include <string> 21 #include <utility> 22 #include <vector> 23 24 #include "lang_id/lang-id.h" 25 26 namespace libtextclassifier3 { 27 28 namespace langid { 29 30 // Loads the LangId model from a given path. 31 std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId> LoadFromPath( 32 const std::string& path); 33 34 // Loads the LangId model from a file descriptor. 35 std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId> LoadFromDescriptor( 36 const int fd); 37 38 // Loads the LangId model from a buffer. The buffer needs to outlive the LangId 39 // instance. 40 std::unique_ptr<libtextclassifier3::mobile::lang_id::LangId> LoadFromUnownedBuffer( 41 const char* buffer, int size); 42 43 // Returns the LangId predictions (locale, confidence) from the given LangId 44 // model. The maximum number of predictions returned will be computed internally 45 // relatively to the noise threshold. 46 std::vector<std::pair<std::string, float>> GetPredictions( 47 const libtextclassifier3::mobile::lang_id::LangId* model, const std::string& text); 48 49 // Same as above but takes a char pointer and byte length. 50 std::vector<std::pair<std::string, float>> GetPredictions( 51 const libtextclassifier3::mobile::lang_id::LangId* model, const char* text, 52 int text_size); 53 54 // Returns the language tags string from the given LangId model. The language 55 // tags will be filtered internally by the LangId threshold. 56 std::string GetLanguageTags(const libtextclassifier3::mobile::lang_id::LangId* model, 57 const std::string& text); 58 59 } // namespace langid 60 61 } // namespace libtextclassifier3 62 63 #endif // LIBTEXTCLASSIFIER_LANG_ID_LANG_ID_WRAPPER_H_ 64