Skip to content

Commit

Permalink
Implement Unicode normalization filters
Browse files Browse the repository at this point in the history
Not sure how to test
  • Loading branch information
clipperhouse committed Apr 17, 2020
1 parent 71f2a28 commit 3d5393e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions filters/norm/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package norm

import (
"github.com/clipperhouse/jargon"
"github.com/clipperhouse/jargon/filters/mapper"
"golang.org/x/text/unicode/norm"
)

// NFC normalizes tokens into Unicode Normalization Form C
var NFC = newFilter(norm.NFC)

// NFD normalizes tokens into Unicode Normalization Form D
var NFD = newFilter(norm.NFD)

// NFKC normalizes tokens into Unicode Normalization Form KC
var NFKC = newFilter(norm.NFKC)

// NFKD normalizes tokens into Unicode Normalization Form KD
var NFKD = newFilter(norm.NFKD)

func newFilter(form norm.Form) jargon.Filter {
f := func(token *jargon.Token) *jargon.Token {
if form.IsNormalString(token.String()) {
return token
}

s := form.String(token.String())
return jargon.NewToken(s, true)
}

return mapper.NewFilter(f)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/kljensen/snowball v0.6.0
github.com/spf13/afero v1.2.2
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
golang.org/x/text v0.3.2
)

go 1.14

0 comments on commit 3d5393e

Please sign in to comment.