Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

unicodenorm.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2014 Couchbase, Inc.
  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. package unicodenorm
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/analysis"
  18. "github.com/blevesearch/bleve/registry"
  19. "golang.org/x/text/unicode/norm"
  20. )
  21. const Name = "normalize_unicode"
  22. const NFC = "nfc"
  23. const NFD = "nfd"
  24. const NFKC = "nfkc"
  25. const NFKD = "nfkd"
  26. var forms = map[string]norm.Form{
  27. NFC: norm.NFC,
  28. NFD: norm.NFD,
  29. NFKC: norm.NFKC,
  30. NFKD: norm.NFKD,
  31. }
  32. type UnicodeNormalizeFilter struct {
  33. form norm.Form
  34. }
  35. func NewUnicodeNormalizeFilter(formName string) (*UnicodeNormalizeFilter, error) {
  36. form, ok := forms[formName]
  37. if !ok {
  38. return nil, fmt.Errorf("no form named %s", formName)
  39. }
  40. return &UnicodeNormalizeFilter{
  41. form: form,
  42. }, nil
  43. }
  44. func MustNewUnicodeNormalizeFilter(formName string) *UnicodeNormalizeFilter {
  45. filter, err := NewUnicodeNormalizeFilter(formName)
  46. if err != nil {
  47. panic(err)
  48. }
  49. return filter
  50. }
  51. func (s *UnicodeNormalizeFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
  52. for _, token := range input {
  53. token.Term = s.form.Bytes(token.Term)
  54. }
  55. return input
  56. }
  57. func UnicodeNormalizeFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) {
  58. formVal, ok := config["form"].(string)
  59. if !ok {
  60. return nil, fmt.Errorf("must specify form")
  61. }
  62. form := formVal
  63. return NewUnicodeNormalizeFilter(form)
  64. }
  65. func init() {
  66. registry.RegisterTokenFilter(Name, UnicodeNormalizeFilterConstructor)
  67. }