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/bert_tokenizer.h"
17 
18 #include "tensorflow_lite_support/cc/port/integral_types.h"
19 
20 namespace tflite {
21 namespace support {
22 namespace text {
23 namespace tokenizer {
24 
FlatHashMapBackedWordpiece(const std::vector<std::string> & vocab)25 FlatHashMapBackedWordpiece::FlatHashMapBackedWordpiece(
26     const std::vector<std::string>& vocab)
27     : vocab_{vocab} {
28   for (int i = 0; i < vocab_.size(); ++i) {
29     index_map_[vocab_[i]] = i;
30   }
31 }
32 
Contains(absl::string_view key,bool * value) const33 tensorflow::text::LookupStatus FlatHashMapBackedWordpiece::Contains(
34     absl::string_view key, bool* value) const {
35   *value = index_map_.contains(key);
36   return tensorflow::text::LookupStatus();
37 }
38 
LookupId(const absl::string_view key,int * result) const39 bool FlatHashMapBackedWordpiece::LookupId(const absl::string_view key,
40                                           int* result) const {
41   auto it = index_map_.find(key);
42   if (it == index_map_.end()) {
43     return false;
44   }
45   *result = it->second;
46   return true;
47 }
48 
LookupWord(int vocab_id,absl::string_view * result) const49 bool FlatHashMapBackedWordpiece::LookupWord(int vocab_id,
50                                             absl::string_view* result) const {
51   if (vocab_id >= vocab_.size() || vocab_id < 0) {
52     return false;
53   }
54   *result = vocab_[vocab_id];
55   return true;
56 }
57 
Tokenize(const std::string & input)58 TokenizerResult BertTokenizer::Tokenize(const std::string& input) {
59   return TokenizeWordpiece(input);
60 }
61 
TokenizeWordpiece(const std::string & input)62 WordpieceTokenizerResult BertTokenizer::TokenizeWordpiece(
63     const std::string& input) {
64   WordpieceTokenizerResult result;
65   std::vector<std::string>& subwords = result.subwords;
66   std::vector<int>& wp_absolute_begin_offset = result.wp_begin_offset;
67   std::vector<int>& wp_absolute_end_offset = result.wp_end_offset;
68 
69   std::vector<absl::string_view> tokens;
70   std::vector<int64> begin_offsets;
71   std::vector<int64> end_offsets;
72 
73   // Run through tokenize function
74   tensorflow::text::RegexSplit(input, delim_re_, true, include_delim_re_,
75                                &tokens, &begin_offsets, &end_offsets);
76 
77   for (int token_index = 0; token_index < tokens.size(); token_index++) {
78     auto& token = tokens[token_index];
79     int num_word_pieces = 0;
80     tensorflow::text::LookupStatus status = WordpieceTokenize(
81         token, options_.max_bytes_per_token, options_.max_chars_per_subtoken,
82         options_.suffix_indicator, options_.use_unknown_token,
83         options_.unknown_token, options_.split_unknown_chars, &vocab_,
84         &subwords, &wp_absolute_begin_offset, &wp_absolute_end_offset,
85         &num_word_pieces);
86 
87     result.row_lengths.emplace_back(num_word_pieces);
88     // for the last num_word_pieces added into wp_absolute_begin_offset and
89     // wp_absolute_end_offset, offset them with begin_offsets[token_index]
90     int absolute_offset_size = wp_absolute_begin_offset.size();
91     for (int i = num_word_pieces; i > 0; i--) {
92       wp_absolute_begin_offset[absolute_offset_size - i] +=
93           begin_offsets[token_index];
94       wp_absolute_end_offset[absolute_offset_size - i] +=
95           begin_offsets[token_index];
96     }
97     if (!status.success) {
98       return result;
99     }
100   }
101 
102   return result;
103 }
104 
105 }  // namespace tokenizer
106 }  // namespace text
107 }  // namespace support
108 }  // namespace tflite
109