Debian Codegraph @

vertexai/internal/sentencepiece/normalize.go

raw
1 // Copyright 2024 Google LLC
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 package sentencepiece
16
17 import "strings"
18
19 // normalize performs unicode normalization.
20 //
21 // SentencePiece has a feature to perform configurable unicode normalization on
22 // the input text and has some options for adding dummy whitespace prefixes or
23 // trimming whitespace. However, the model we're working with has a very simple
24 // normalizer that does none of this. These options can be added in the future
25 // if needed.
26 func normalize(text string) string {
27 return replaceSpacesBySeparator(text)
28 }
29
30 const whitespaceSeparator = "▁"
31
32 // replaceSpacesBySeparator replaces spaces by the whitespace separator used by
33 // the model.
34 func replaceSpacesBySeparator(text string) string {
35 return strings.ReplaceAll(text, " ", whitespaceSeparator)
36 }
37
38 // replaceSeparatorsBySpace replaces the whitespace separator used by
39 // the model back with spaces.
40 func replaceSeparatorsBySpace(text string) string {
41 return strings.ReplaceAll(text, whitespaceSeparator, " ")
42 }