xref: /aosp_15_r20/external/tflite-support/tensorflow_lite_support/cc/text/tokenizers/tokenizer.h (revision b16991f985baa50654c05c5adbb3c8bbcfb40082)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_SUPPORT_CC_TEXT_TOKENIZERS_TOKENIZER_H_
17 #define TENSORFLOW_LITE_SUPPORT_CC_TEXT_TOKENIZERS_TOKENIZER_H_
18 
19 #include <fstream>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/strings/string_view.h"
24 
25 namespace tflite {
26 namespace support {
27 namespace text {
28 namespace tokenizer {
29 
30 struct TokenizerResult {
31   std::vector<std::string> subwords;
32 };
33 
34 // Interface of general tokenizer.
35 class Tokenizer {
36  public:
37   // Perform tokenization to get tokenized results.
38   virtual TokenizerResult Tokenize(const std::string& input) = 0;
39 
40   // Find the id of a string token.
41   virtual bool LookupId(absl::string_view key, int* result) const = 0;
42 
43   // Find the string token from an id.
44   virtual bool LookupWord(int vocab_id, absl::string_view* result) const = 0;
45 
46   // Destructor.
47   virtual ~Tokenizer() = default;
48 };
49 
50 }  // namespace tokenizer
51 }  // namespace text
52 }  // namespace support
53 }  // namespace tflite
54 
55 #endif  // TENSORFLOW_LITE_SUPPORT_CC_TEXT_TOKENIZERS_TOKENIZER_H_
56