1
// Copyright (c) 2016 Couchbase, Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
21
"github.com/blevesearch/bleve/analysis"
22
"github.com/blevesearch/bleve/registry"
25
const WidthName = "cjk_width"
27
type CJKWidthFilter struct{}
29
func NewCJKWidthFilter() *CJKWidthFilter {
30
return &CJKWidthFilter{}
33
func (s *CJKWidthFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
34
for _, token := range input {
35
runeCount := utf8.RuneCount(token.Term)
36
runes := bytes.Runes(token.Term)
37
for i := 0; i < runeCount; i++ {
39
if ch >= 0xFF01 && ch <= 0xFF5E {
40
// fullwidth ASCII variants
42
} else if ch >= 0xFF65 && ch <= 0xFF9F {
43
// halfwidth Katakana variants
44
if (ch == 0xFF9E || ch == 0xFF9F) && i > 0 && combine(runes, i, ch) {
45
runes = analysis.DeleteRune(runes, i)
47
runeCount = len(runes)
49
runes[i] = kanaNorm[ch-0xFF65]
53
token.Term = analysis.BuildTermFromRunes(runes)
59
var kanaNorm = []rune{
60
0x30fb, 0x30f2, 0x30a1, 0x30a3, 0x30a5, 0x30a7, 0x30a9, 0x30e3, 0x30e5,
61
0x30e7, 0x30c3, 0x30fc, 0x30a2, 0x30a4, 0x30a6, 0x30a8, 0x30aa, 0x30ab,
62
0x30ad, 0x30af, 0x30b1, 0x30b3, 0x30b5, 0x30b7, 0x30b9, 0x30bb, 0x30bd,
63
0x30bf, 0x30c1, 0x30c4, 0x30c6, 0x30c8, 0x30ca, 0x30cb, 0x30cc, 0x30cd,
64
0x30ce, 0x30cf, 0x30d2, 0x30d5, 0x30d8, 0x30db, 0x30de, 0x30df, 0x30e0,
65
0x30e1, 0x30e2, 0x30e4, 0x30e6, 0x30e8, 0x30e9, 0x30ea, 0x30eb, 0x30ec,
66
0x30ed, 0x30ef, 0x30f3, 0x3099, 0x309A,
69
var kanaCombineVoiced = []rune{
70
78, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
71
0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
72
0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73
0, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
75
var kanaCombineHalfVoiced = []rune{
76
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2,
78
0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82
func combine(text []rune, pos int, r rune) bool {
84
if prev >= 0x30A6 && prev <= 0x30FD {
86
text[pos-1] += kanaCombineHalfVoiced[prev-0x30A6]
88
text[pos-1] += kanaCombineVoiced[prev-0x30A6]
90
return text[pos-1] != prev
95
func CJKWidthFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) {
96
return NewCJKWidthFilter(), nil
100
registry.RegisterTokenFilter(WidthName, CJKWidthFilterConstructor)