~ubuntu-branches/debian/sid/golang-github-blevesearch-bleve/sid

« back to all changes in this revision

Viewing changes to analysis/token/camelcase/camelcase.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-30 16:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20170330160603-0oogmb960l7918jx
Tags: upstream-0.5.0+git20170324.202.4702785f
ImportĀ upstreamĀ versionĀ 0.5.0+git20170324.202.4702785f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (c) 2016 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
 
 
15
package camelcase
 
16
 
 
17
import (
 
18
        "bytes"
 
19
        "unicode/utf8"
 
20
 
 
21
        "github.com/blevesearch/bleve/analysis"
 
22
        "github.com/blevesearch/bleve/registry"
 
23
)
 
24
 
 
25
const Name = "camelCase"
 
26
 
 
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.
 
39
//
 
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.
 
42
//
 
43
// See the test file for examples of classes and their parsings.
 
44
type CamelCaseFilter struct{}
 
45
 
 
46
func NewCamelCaseFilter() *CamelCaseFilter {
 
47
        return &CamelCaseFilter{}
 
48
}
 
49
 
 
50
func (f *CamelCaseFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
 
51
        rv := make(analysis.TokenStream, 0, len(input))
 
52
 
 
53
        for _, token := range input {
 
54
                runeCount := utf8.RuneCount(token.Term)
 
55
                runes := bytes.Runes(token.Term)
 
56
 
 
57
                p := NewParser(runeCount)
 
58
                for i := 0; i < runeCount; i++ {
 
59
                        if i+1 >= runeCount {
 
60
                                p.Push(runes[i], nil)
 
61
                        } else {
 
62
                                p.Push(runes[i], &runes[i+1])
 
63
                        }
 
64
                }
 
65
                rv = append(rv, p.FlushTokens()...)
 
66
        }
 
67
        return rv
 
68
}
 
69
 
 
70
func CamelCaseFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) {
 
71
        return NewCamelCaseFilter(), nil
 
72
}
 
73
 
 
74
func init() {
 
75
        registry.RegisterTokenFilter(Name, CamelCaseFilterConstructor)
 
76
}