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_REGEX_TOKENIZER_H_ 17 #define TENSORFLOW_LITE_SUPPORT_CC_TEXT_TOKENIZERS_REGEX_TOKENIZER_H_ 18 19 #include "absl/container/node_hash_map.h" 20 #include "re2/re2.h" 21 #include "tensorflow_lite_support/cc/text/tokenizers/tokenizer.h" 22 23 namespace tflite { 24 namespace support { 25 namespace text { 26 namespace tokenizer { 27 28 // Tokenizer to load a vocabulary and split text by regular expressions. 29 class RegexTokenizer : public Tokenizer { 30 public: 31 explicit RegexTokenizer(const std::string& regex_pattern, 32 const std::string& path_to_vocab); 33 34 explicit RegexTokenizer(const std::string& regex_pattern, 35 const char* vocab_buffer_data, 36 size_t vocab_buffer_size); 37 38 TokenizerResult Tokenize(const std::string& input) override; 39 40 bool LookupId(absl::string_view key, int* result) const override; 41 42 bool LookupWord(int vocab_id, absl::string_view* result) const override; 43 44 bool GetStartToken(int* start_token); 45 bool GetPadToken(int* pad_token); 46 bool GetUnknownToken(int* unknown_token); 47 48 private: 49 RE2 delim_re_; 50 absl::node_hash_map<std::string, int> token_index_map_; 51 absl::node_hash_map<int, absl::string_view> index_token_map_; 52 }; 53 54 } // namespace tokenizer 55 } // namespace text 56 } // namespace support 57 } // namespace tflite 58 59 #endif // TENSORFLOW_LITE_SUPPORT_CC_TEXT_TOKENIZERS_REGEX_TOKENIZER_H_ 60