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

« back to all changes in this revision

Viewing changes to analysis/lang/cjk/cjk_width.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 cjk
 
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 WidthName = "cjk_width"
 
26
 
 
27
type CJKWidthFilter struct{}
 
28
 
 
29
func NewCJKWidthFilter() *CJKWidthFilter {
 
30
        return &CJKWidthFilter{}
 
31
}
 
32
 
 
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++ {
 
38
                        ch := runes[i]
 
39
                        if ch >= 0xFF01 && ch <= 0xFF5E {
 
40
                                // fullwidth ASCII variants
 
41
                                runes[i] -= 0xFEE0
 
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)
 
46
                                        i--
 
47
                                        runeCount = len(runes)
 
48
                                } else {
 
49
                                        runes[i] = kanaNorm[ch-0xFF65]
 
50
                                }
 
51
                        }
 
52
                }
 
53
                token.Term = analysis.BuildTermFromRunes(runes)
 
54
        }
 
55
 
 
56
        return input
 
57
}
 
58
 
 
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,
 
67
}
 
68
 
 
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,
 
74
}
 
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,
 
80
}
 
81
 
 
82
func combine(text []rune, pos int, r rune) bool {
 
83
        prev := text[pos-1]
 
84
        if prev >= 0x30A6 && prev <= 0x30FD {
 
85
                if r == 0xFF9F {
 
86
                        text[pos-1] += kanaCombineHalfVoiced[prev-0x30A6]
 
87
                } else {
 
88
                        text[pos-1] += kanaCombineVoiced[prev-0x30A6]
 
89
                }
 
90
                return text[pos-1] != prev
 
91
        }
 
92
        return false
 
93
}
 
94
 
 
95
func CJKWidthFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) {
 
96
        return NewCJKWidthFilter(), nil
 
97
}
 
98
 
 
99
func init() {
 
100
        registry.RegisterTokenFilter(WidthName, CJKWidthFilterConstructor)
 
101
}