2014-04-17 22:55:53 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
2016-10-02 16:13:14 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2016-09-30 17:18:39 +02:00
|
|
|
package stop
|
2014-04-17 22:55:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/analysis"
|
2016-09-30 17:18:39 +02:00
|
|
|
"github.com/blevesearch/bleve/analysis/tokenmap"
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/registry"
|
2014-04-17 22:55:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStopWordsFilter(t *testing.T) {
|
|
|
|
|
|
|
|
inputTokenStream := analysis.TokenStream{
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("a"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("walk"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("in"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("the"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("park"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedTokenStream := analysis.TokenStream{
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("walk"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("park"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-08-14 03:14:47 +02:00
|
|
|
cache := registry.NewCache()
|
|
|
|
stopListConfig := map[string]interface{}{
|
2016-09-30 17:18:39 +02:00
|
|
|
"type": tokenmap.Name,
|
2014-08-14 03:14:47 +02:00
|
|
|
"tokens": []interface{}{"a", "in", "the"},
|
|
|
|
}
|
2014-09-01 19:55:23 +02:00
|
|
|
_, err := cache.DefineTokenMap("stop_test", stopListConfig)
|
2014-07-31 01:29:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-14 03:14:47 +02:00
|
|
|
|
|
|
|
stopConfig := map[string]interface{}{
|
2014-09-01 19:55:23 +02:00
|
|
|
"type": "stop_tokens",
|
2014-08-14 03:14:47 +02:00
|
|
|
"stop_token_map": "stop_test",
|
|
|
|
}
|
2014-09-01 19:55:23 +02:00
|
|
|
stopFilter, err := cache.DefineTokenFilter("stop_test", stopConfig)
|
2014-08-14 03:14:47 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ouputTokenStream := stopFilter.Filter(inputTokenStream)
|
2014-04-17 22:55:53 +02:00
|
|
|
if !reflect.DeepEqual(ouputTokenStream, expectedTokenStream) {
|
|
|
|
t.Errorf("expected %#v got %#v", expectedTokenStream, ouputTokenStream)
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 18:29:33 +02:00
|
|
|
|
|
|
|
func BenchmarkStopWordsFilter(b *testing.B) {
|
|
|
|
|
|
|
|
inputTokenStream := analysis.TokenStream{
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("a"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("walk"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("in"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("the"),
|
|
|
|
},
|
|
|
|
&analysis.Token{
|
|
|
|
Term: []byte("park"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := registry.NewCache()
|
|
|
|
stopListConfig := map[string]interface{}{
|
2016-09-30 17:18:39 +02:00
|
|
|
"type": tokenmap.Name,
|
2016-09-11 18:29:33 +02:00
|
|
|
"tokens": []interface{}{"a", "in", "the"},
|
|
|
|
}
|
|
|
|
_, err := cache.DefineTokenMap("stop_test", stopListConfig)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stopConfig := map[string]interface{}{
|
|
|
|
"type": "stop_tokens",
|
|
|
|
"stop_token_map": "stop_test",
|
|
|
|
}
|
|
|
|
stopFilter, err := cache.DefineTokenFilter("stop_test", stopConfig)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stopFilter.Filter(inputTokenStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|