You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alphabet.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2018 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 levenshtein
  15. import (
  16. "fmt"
  17. "sort"
  18. "unicode/utf8"
  19. )
  20. type FullCharacteristicVector []uint32
  21. func (fcv FullCharacteristicVector) shiftAndMask(offset, mask uint32) uint32 {
  22. bucketID := offset / 32
  23. align := offset - bucketID*32
  24. if align == 0 {
  25. return fcv[bucketID] & mask
  26. }
  27. left := fcv[bucketID] >> align
  28. right := fcv[bucketID+1] << (32 - align)
  29. return (left | right) & mask
  30. }
  31. type tuple struct {
  32. char rune
  33. fcv FullCharacteristicVector
  34. }
  35. type sortRunes []rune
  36. func (s sortRunes) Less(i, j int) bool {
  37. return s[i] < s[j]
  38. }
  39. func (s sortRunes) Swap(i, j int) {
  40. s[i], s[j] = s[j], s[i]
  41. }
  42. func (s sortRunes) Len() int {
  43. return len(s)
  44. }
  45. func sortRune(r []rune) []rune {
  46. sort.Sort(sortRunes(r))
  47. return r
  48. }
  49. type Alphabet struct {
  50. charset []tuple
  51. index uint32
  52. }
  53. func (a *Alphabet) resetNext() {
  54. a.index = 0
  55. }
  56. func (a *Alphabet) next() (rune, FullCharacteristicVector, error) {
  57. if int(a.index) >= len(a.charset) {
  58. return 0, nil, fmt.Errorf("eof")
  59. }
  60. rv := a.charset[a.index]
  61. a.index++
  62. return rv.char, rv.fcv, nil
  63. }
  64. func dedupe(in string) string {
  65. lookUp := make(map[rune]struct{}, len(in))
  66. var rv string
  67. for len(in) > 0 {
  68. r, size := utf8.DecodeRuneInString(in)
  69. in = in[size:]
  70. if _, ok := lookUp[r]; !ok {
  71. rv += string(r)
  72. lookUp[r] = struct{}{}
  73. }
  74. }
  75. return rv
  76. }
  77. func queryChars(qChars string) Alphabet {
  78. chars := dedupe(qChars)
  79. inChars := sortRune([]rune(chars))
  80. charsets := make([]tuple, 0, len(inChars))
  81. for _, c := range inChars {
  82. tempChars := qChars
  83. var bits []uint32
  84. for len(tempChars) > 0 {
  85. var chunk string
  86. if len(tempChars) > 32 {
  87. chunk = tempChars[0:32]
  88. tempChars = tempChars[32:]
  89. } else {
  90. chunk = tempChars
  91. tempChars = tempChars[:0]
  92. }
  93. chunkBits := uint32(0)
  94. bit := uint32(1)
  95. for _, chr := range chunk {
  96. if chr == c {
  97. chunkBits |= bit
  98. }
  99. bit <<= 1
  100. }
  101. bits = append(bits, chunkBits)
  102. }
  103. bits = append(bits, 0)
  104. charsets = append(charsets, tuple{char: c, fcv: FullCharacteristicVector(bits)})
  105. }
  106. return Alphabet{charset: charsets}
  107. }