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