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 #include "tensorflow_lite_support/cc/text/tokenizers/regex_tokenizer.h"
17
18 #include <iostream>
19 #include <regex>
20
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/substitute.h"
23 #include "tensorflow_lite_support/cc/utils/common_utils.h"
24 namespace tflite {
25 namespace support {
26 namespace text {
27 namespace tokenizer {
28
29 namespace {
30 constexpr char kStart[] = "<START>";
31 constexpr char kPad[] = "<PAD>";
32 constexpr char kUnknown[] = "<UNKNOWN>";
33
buildIndexTokenMap(const absl::node_hash_map<std::string,int> & token_index_map,absl::node_hash_map<int,absl::string_view> * index_token_map)34 void buildIndexTokenMap(
35 const absl::node_hash_map<std::string, int>& token_index_map,
36 absl::node_hash_map<int, absl::string_view>* index_token_map) {
37 for (const auto& token : token_index_map) {
38 (*index_token_map)[token.second] = token.first;
39 }
40 }
41
42 } // namespace
43
RegexTokenizer(const std::string & regex_pattern,const std::string & path_to_vocab)44 RegexTokenizer::RegexTokenizer(const std::string& regex_pattern,
45 const std::string& path_to_vocab)
46 : delim_re_{absl::Substitute("($0)", regex_pattern)},
47 token_index_map_{utils::LoadVocabAndIndexFromFile(path_to_vocab)} {
48 buildIndexTokenMap(token_index_map_, &index_token_map_);
49 }
50
RegexTokenizer(const std::string & regex_pattern,const char * vocab_buffer_data,size_t vocab_buffer_size)51 RegexTokenizer::RegexTokenizer(const std::string& regex_pattern,
52 const char* vocab_buffer_data,
53 size_t vocab_buffer_size)
54 : delim_re_{absl::Substitute("($0)", regex_pattern)},
55 token_index_map_{utils::LoadVocabAndIndexFromBuffer(vocab_buffer_data,
56 vocab_buffer_size)} {
57 buildIndexTokenMap(token_index_map_, &index_token_map_);
58 }
59
Tokenize(const std::string & input)60 TokenizerResult RegexTokenizer::Tokenize(const std::string& input) {
61 TokenizerResult result;
62
63 // Keep looking for split points until we have reached the end of the input.
64 // TODO (ag/17748161): Using smatch here introduces inefficient string copying; optimize if necessary.
65 std::string leftover = input;
66 std::smatch token;
67 while(std::regex_search(leftover, token, delim_re_)) {
68 if (token.length() > 0) {
69 result.subwords.push_back(token.prefix().str());
70 }
71 leftover = token.suffix().str();
72 }
73
74 // Close the last token.
75 if (!leftover.empty()) {
76 result.subwords.push_back(leftover);
77 }
78
79 return result;
80 }
81
LookupId(absl::string_view key,int * result) const82 bool RegexTokenizer::LookupId(absl::string_view key, int* result) const {
83 auto it = token_index_map_.find(key);
84 if (it == token_index_map_.end()) {
85 return false;
86 }
87 *result = it->second;
88 return true;
89 }
90
LookupWord(int vocab_id,absl::string_view * result) const91 bool RegexTokenizer::LookupWord(int vocab_id, absl::string_view* result) const {
92 auto it = index_token_map_.find(vocab_id);
93 if (it == index_token_map_.end()) {
94 return false;
95 }
96 *result = it->second;
97 return true;
98 }
99
GetStartToken(int * start_token)100 bool RegexTokenizer::GetStartToken(int* start_token) {
101 return LookupId(kStart, start_token);
102 }
103
GetPadToken(int * pad_token)104 bool RegexTokenizer::GetPadToken(int* pad_token) {
105 return LookupId(kPad, pad_token);
106 }
107
GetUnknownToken(int * unknown_token)108 bool RegexTokenizer::GetUnknownToken(int* unknown_token) {
109 return LookupId(kUnknown, unknown_token);
110 }
111
112 } // namespace tokenizer
113 } // namespace text
114 } // namespace support
115 } // namespace tflite
116