Home | History | Annotate | Download | only in sentencepiece
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "utils/sentencepiece/test_utils.h"
     18 
     19 #include <memory>
     20 
     21 #include "utils/base/integral_types.h"
     22 #include "utils/sentencepiece/double_array_trie.h"
     23 #include "utils/strings/stringpiece.h"
     24 
     25 namespace libtextclassifier3 {
     26 
     27 SentencePieceNormalizer NormalizerFromSpec(StringPiece spec,
     28                                            bool add_dummy_prefix,
     29                                            bool remove_extra_whitespaces,
     30                                            bool escape_whitespaces) {
     31   const uint32 trie_blob_size = reinterpret_cast<const uint32*>(spec.data())[0];
     32   spec.RemovePrefix(sizeof(trie_blob_size));
     33   const TrieNode* trie_blob = reinterpret_cast<const TrieNode*>(spec.data());
     34   spec.RemovePrefix(trie_blob_size);
     35   const int num_nodes = trie_blob_size / sizeof(TrieNode);
     36   return SentencePieceNormalizer(
     37       DoubleArrayTrie(trie_blob, num_nodes),
     38       /*charsmap_normalized=*/StringPiece(spec.data(), spec.size()),
     39       add_dummy_prefix, remove_extra_whitespaces, escape_whitespaces);
     40 }
     41 
     42 }  // namespace libtextclassifier3
     43