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