• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils/common_utils.h"
17 
18 #include <fstream>
19 
20 #include "absl/strings/str_split.h"
21 
22 namespace tflite {
23 namespace support {
24 namespace utils {
25 namespace {
26 struct membuf : std::streambuf {
membuftflite::support::utils::__anon00627f860111::membuf27   membuf(char* begin, char* end) { this->setg(begin, begin, end); }
28 };
29 
ReadIStreamLineByLine(std::istream * istream,const std::function<void (std::string)> & line_processor)30 void ReadIStreamLineByLine(
31     std::istream* istream,
32     const std::function<void(std::string)>& line_processor) {
33   std::string str;
34   while (std::getline(*istream, str)) {
35     if (!str.empty()) {
36       line_processor(str);
37     }
38   }
39 }
40 
ReadIStreamLineSplits(std::istream * istream)41 absl::node_hash_map<std::string, int> ReadIStreamLineSplits(
42     std::istream* istream) {
43   absl::node_hash_map<std::string, int> vocab_index_map;
44   std::string str;
45   ReadIStreamLineByLine(istream, [&vocab_index_map](const std::string& str) {
46     std::vector<std::string> v = absl::StrSplit(str, ' ');
47     vocab_index_map[v[0]] = std::stoi(v[1]);
48   });
49   return vocab_index_map;
50 }
51 
ReadIStreamByLine(std::istream * istream)52 std::vector<std::string> ReadIStreamByLine(std::istream* istream) {
53   std::vector<std::string> vocab_from_file;
54   std::string str;
55 
56   ReadIStreamLineByLine(istream, [&vocab_from_file](const std::string& str) {
57     vocab_from_file.push_back(str);
58   });
59   return vocab_from_file;
60 }
61 
62 }  // namespace
63 
LoadVocabFromFile(const std::string & path_to_vocab)64 std::vector<std::string> LoadVocabFromFile(const std::string& path_to_vocab) {
65   std::vector<std::string> vocab_from_file;
66   std::ifstream in(path_to_vocab.c_str());
67   return ReadIStreamByLine(&in);
68 }
69 
LoadVocabFromBuffer(const char * vocab_buffer_data,const size_t vocab_buffer_size)70 std::vector<std::string> LoadVocabFromBuffer(const char* vocab_buffer_data,
71                                              const size_t vocab_buffer_size) {
72   membuf sbuf(const_cast<char*>(vocab_buffer_data),
73               const_cast<char*>(vocab_buffer_data + vocab_buffer_size));
74   std::istream in(&sbuf);
75   return ReadIStreamByLine(&in);
76 }
77 
LoadVocabAndIndexFromFile(const std::string & path_to_vocab)78 absl::node_hash_map<std::string, int> LoadVocabAndIndexFromFile(
79     const std::string& path_to_vocab) {
80   absl::node_hash_map<std::string, int> vocab_index_map;
81   std::ifstream in(path_to_vocab.c_str());
82   return ReadIStreamLineSplits(&in);
83 }
84 
LoadVocabAndIndexFromBuffer(const char * vocab_buffer_data,const size_t vocab_buffer_size)85 absl::node_hash_map<std::string, int> LoadVocabAndIndexFromBuffer(
86     const char* vocab_buffer_data, const size_t vocab_buffer_size) {
87   membuf sbuf(const_cast<char*>(vocab_buffer_data),
88               const_cast<char*>(vocab_buffer_data + vocab_buffer_size));
89   absl::node_hash_map<std::string, int> vocab_index_map;
90   std::istream in(&sbuf);
91   return ReadIStreamLineSplits(&in);
92 }
93 
94 }  // namespace utils
95 }  // namespace support
96 }  // namespace tflite
97