1*993b0882SAndroid Build Coastguard Worker /* 2*993b0882SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project 3*993b0882SAndroid Build Coastguard Worker * 4*993b0882SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*993b0882SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*993b0882SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*993b0882SAndroid Build Coastguard Worker * 8*993b0882SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*993b0882SAndroid Build Coastguard Worker * 10*993b0882SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*993b0882SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*993b0882SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*993b0882SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*993b0882SAndroid Build Coastguard Worker * limitations under the License. 15*993b0882SAndroid Build Coastguard Worker */ 16*993b0882SAndroid Build Coastguard Worker 17*993b0882SAndroid Build Coastguard Worker #ifndef LIBTEXTCLASSIFIER_UTILS_SENTENCEPIECE_NORMALIZER_H_ 18*993b0882SAndroid Build Coastguard Worker #define LIBTEXTCLASSIFIER_UTILS_SENTENCEPIECE_NORMALIZER_H_ 19*993b0882SAndroid Build Coastguard Worker 20*993b0882SAndroid Build Coastguard Worker #include <memory> 21*993b0882SAndroid Build Coastguard Worker #include <string> 22*993b0882SAndroid Build Coastguard Worker 23*993b0882SAndroid Build Coastguard Worker #include "utils/container/double-array-trie.h" 24*993b0882SAndroid Build Coastguard Worker #include "utils/strings/stringpiece.h" 25*993b0882SAndroid Build Coastguard Worker 26*993b0882SAndroid Build Coastguard Worker namespace libtextclassifier3 { 27*993b0882SAndroid Build Coastguard Worker 28*993b0882SAndroid Build Coastguard Worker // Normalizer implements a simple text normalizer with user-defined 29*993b0882SAndroid Build Coastguard Worker // string-to-string rules and leftmost longest matching. 30*993b0882SAndroid Build Coastguard Worker class SentencePieceNormalizer { 31*993b0882SAndroid Build Coastguard Worker public: 32*993b0882SAndroid Build Coastguard Worker // charsmap_trie and charsmap_normalized specify the normalization/replacement 33*993b0882SAndroid Build Coastguard Worker // string-to-string rules in the following way: 34*993b0882SAndroid Build Coastguard Worker // A match in the trie for a string will return the offset in 35*993b0882SAndroid Build Coastguard Worker // charsmap_normalized that contains the replacement string. 36*993b0882SAndroid Build Coastguard Worker // 37*993b0882SAndroid Build Coastguard Worker // add_dummy_prefix: Whether to add dummy whitespace at the beginning of the 38*993b0882SAndroid Build Coastguard Worker // text in order to treat "world" in "world" and "hello world" uniformly. 39*993b0882SAndroid Build Coastguard Worker // 40*993b0882SAndroid Build Coastguard Worker // remove_extra_whitespaces: Whether to remove leading, trailing and duplicate 41*993b0882SAndroid Build Coastguard Worker // internal whitespace. 42*993b0882SAndroid Build Coastguard Worker // 43*993b0882SAndroid Build Coastguard Worker // escape_whitespaces: Whether to replace whitespace with a meta symbol. 44*993b0882SAndroid Build Coastguard Worker SentencePieceNormalizer(const DoubleArrayTrie& charsmap_trie, 45*993b0882SAndroid Build Coastguard Worker StringPiece charsmap_normalized, 46*993b0882SAndroid Build Coastguard Worker bool add_dummy_prefix = true, 47*993b0882SAndroid Build Coastguard Worker bool remove_extra_whitespaces = true, 48*993b0882SAndroid Build Coastguard Worker bool escape_whitespaces = true) charsmap_trie_(charsmap_trie)49*993b0882SAndroid Build Coastguard Worker : charsmap_trie_(charsmap_trie), 50*993b0882SAndroid Build Coastguard Worker charsmap_normalized_(charsmap_normalized), 51*993b0882SAndroid Build Coastguard Worker add_dummy_prefix_(add_dummy_prefix), 52*993b0882SAndroid Build Coastguard Worker remove_extra_whitespaces_(remove_extra_whitespaces), 53*993b0882SAndroid Build Coastguard Worker escape_whitespaces_(escape_whitespaces) {} 54*993b0882SAndroid Build Coastguard Worker 55*993b0882SAndroid Build Coastguard Worker // Normalizes a plain utf8 string into an internal representation for 56*993b0882SAndroid Build Coastguard Worker // Sentencepiece model. 57*993b0882SAndroid Build Coastguard Worker bool Normalize(StringPiece input, std::string* normalized_input) const; 58*993b0882SAndroid Build Coastguard Worker 59*993b0882SAndroid Build Coastguard Worker private: 60*993b0882SAndroid Build Coastguard Worker // Normalizes the prefix of `input` and returns the pair of 61*993b0882SAndroid Build Coastguard Worker // normalized prefix and the length of the prefix of `input` processed in the 62*993b0882SAndroid Build Coastguard Worker // normalization. 63*993b0882SAndroid Build Coastguard Worker bool NormalizePrefix(StringPiece input, 64*993b0882SAndroid Build Coastguard Worker std::pair<StringPiece, int>* prefix) const; 65*993b0882SAndroid Build Coastguard Worker 66*993b0882SAndroid Build Coastguard Worker // Internal trie for efficient longest prefix string matching. 67*993b0882SAndroid Build Coastguard Worker DoubleArrayTrie charsmap_trie_; 68*993b0882SAndroid Build Coastguard Worker 69*993b0882SAndroid Build Coastguard Worker // "\0" delimitered concatenated normalized strings. 70*993b0882SAndroid Build Coastguard Worker // the value of `charsmap_trie_` stores offsets into this string. 71*993b0882SAndroid Build Coastguard Worker StringPiece charsmap_normalized_; 72*993b0882SAndroid Build Coastguard Worker 73*993b0882SAndroid Build Coastguard Worker const bool add_dummy_prefix_; 74*993b0882SAndroid Build Coastguard Worker const bool remove_extra_whitespaces_; 75*993b0882SAndroid Build Coastguard Worker const bool escape_whitespaces_; 76*993b0882SAndroid Build Coastguard Worker }; 77*993b0882SAndroid Build Coastguard Worker 78*993b0882SAndroid Build Coastguard Worker } // namespace libtextclassifier3 79*993b0882SAndroid Build Coastguard Worker 80*993b0882SAndroid Build Coastguard Worker #endif // LIBTEXTCLASSIFIER_UTILS_SENTENCEPIECE_NORMALIZER_H_ 81