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 Name = "camelCase"
27
// CamelCaseFilter splits a given token into a set of tokens where each resulting token
28
// falls into one the following classes:
29
// 1) Upper case followed by lower case letters.
30
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
31
// 2) Upper case followed by upper case letters.
32
// Terminated by a number, an upper case followed by a lower case letter, and a non alpha-numeric symbol.
33
// 3) Lower case followed by lower case letters.
34
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
35
// 4) Number followed by numbers.
36
// Terminated by a letter, and a non alpha-numeric symbol.
37
// 5) Non alpha-numeric symbol followed by non alpha-numeric symbols.
38
// Terminated by a number, and a letter.
40
// It does a one-time sequential pass over an input token, from left to right.
41
// The scan is greedy and generates the longest substring that fits into one of the classes.
43
// See the test file for examples of classes and their parsings.
44
type CamelCaseFilter struct{}
46
func NewCamelCaseFilter() *CamelCaseFilter {
47
return &CamelCaseFilter{}
50
func (f *CamelCaseFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
51
rv := make(analysis.TokenStream, 0, len(input))
53
for _, token := range input {
54
runeCount := utf8.RuneCount(token.Term)
55
runes := bytes.Runes(token.Term)
57
p := NewParser(runeCount)
58
for i := 0; i < runeCount; i++ {
62
p.Push(runes[i], &runes[i+1])
65
rv = append(rv, p.FlushTokens()...)
70
func CamelCaseFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) {
71
return NewCamelCaseFilter(), nil
75
registry.RegisterTokenFilter(Name, CamelCaseFilterConstructor)