2014-09-13 16:51:56 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the
|
|
|
|
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
|
|
|
|
|
|
|
// +build kagome full
|
|
|
|
|
2014-09-16 17:21:29 +02:00
|
|
|
package ja
|
2014-09-13 16:51:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/blevesearch/bleve/analysis"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
|
|
|
|
2014-10-11 04:59:24 +02:00
|
|
|
"github.com/ikawaha/kagome"
|
2014-09-13 16:51:56 +02:00
|
|
|
)
|
|
|
|
|
2014-09-16 17:21:29 +02:00
|
|
|
const TokenizerName = "kagome"
|
2014-09-13 16:51:56 +02:00
|
|
|
|
|
|
|
type KagomeMorphTokenizer struct {
|
2014-10-11 04:59:24 +02:00
|
|
|
tok *kagome.Tokenizer
|
2014-09-13 16:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewKagomeMorphTokenizer() *KagomeMorphTokenizer {
|
|
|
|
return &KagomeMorphTokenizer{
|
2014-10-11 04:59:24 +02:00
|
|
|
tok: kagome.NewTokenizer(),
|
2014-09-13 16:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 04:59:24 +02:00
|
|
|
func NewKagomeMorphTokenizerWithUserDic(userdic *kagome.UserDic) *KagomeMorphTokenizer {
|
|
|
|
k := kagome.NewTokenizer()
|
|
|
|
k.SetUserDic(userdic)
|
2014-09-13 16:51:56 +02:00
|
|
|
return &KagomeMorphTokenizer{
|
2014-10-11 04:59:24 +02:00
|
|
|
tok: k,
|
2014-09-13 16:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *KagomeMorphTokenizer) Tokenize(input []byte) analysis.TokenStream {
|
2014-09-14 13:19:30 +02:00
|
|
|
var (
|
2014-10-11 04:59:24 +02:00
|
|
|
morphs []kagome.Token
|
2014-09-14 13:19:30 +02:00
|
|
|
prevstart int
|
|
|
|
)
|
2014-09-13 16:51:56 +02:00
|
|
|
|
2014-09-24 00:41:32 +02:00
|
|
|
rv := make(analysis.TokenStream, 0, len(input))
|
2014-09-13 16:51:56 +02:00
|
|
|
if len(input) < 1 {
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
2014-10-11 04:59:24 +02:00
|
|
|
morphs = t.tok.Tokenize(string(input))
|
2014-09-13 16:51:56 +02:00
|
|
|
|
|
|
|
for i, m := range morphs {
|
2014-10-11 04:59:24 +02:00
|
|
|
if m.Surface == "EOS" || m.Surface == "BOS" {
|
2014-09-13 16:51:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-14 13:19:30 +02:00
|
|
|
surfacelen := len(m.Surface)
|
2014-09-13 16:51:56 +02:00
|
|
|
token := &analysis.Token{
|
|
|
|
Term: []byte(m.Surface),
|
2014-10-11 04:59:24 +02:00
|
|
|
Position: i,
|
2014-09-14 13:19:30 +02:00
|
|
|
Start: prevstart,
|
|
|
|
End: prevstart + surfacelen,
|
2014-09-13 16:51:56 +02:00
|
|
|
Type: analysis.Ideographic,
|
|
|
|
}
|
2014-09-14 13:19:30 +02:00
|
|
|
|
|
|
|
prevstart = prevstart + surfacelen
|
2014-09-13 16:51:56 +02:00
|
|
|
rv = append(rv, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func KagomeMorphTokenizerConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.Tokenizer, error) {
|
|
|
|
return NewKagomeMorphTokenizer(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-09-16 17:21:29 +02:00
|
|
|
registry.RegisterTokenizer(TokenizerName, KagomeMorphTokenizerConstructor)
|
2014-09-13 16:51:56 +02:00
|
|
|
}
|