forked from dgraph-io/tokenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalizer.go
44 lines (37 loc) · 895 Bytes
/
normalizer.go
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
package tokenizer
import (
"bytes"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var mn = runes.In(unicode.Mn)
// Normalizer is just a byte buffer
type Normalizer struct {
src bytes.Buffer
dst bytes.Buffer
trans transform.Transformer
}
// NewNormalizer creates a new Normalizer
func NewNormalizer() *Normalizer {
return &Normalizer{
trans: transform.Chain(norm.NFD, runes.Remove(mn), norm.NFC),
}
}
func (n *Normalizer) Norm(a string) (string, error) {
n.src.Write([]byte(a))
n.dst.Write([]byte(a)) // allocate at least this much
dst, _, err := n.trans.Transform(n.dst.Bytes(), n.src.Bytes(), true)
retVal := string(n.dst.Bytes()[:dst])
n.src.Reset()
n.dst.Reset()
return retVal, err
}
func (n Normalizer) MustNorm(a string) string {
normed, err := n.Norm(a)
if err != nil {
panic(err)
}
return normed
}