Repository: lucy Updated Branches: refs/heads/master 0d4f3465b -> 123c48ea2
Tune and test Go bindings for HeatMap. Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/a4d63f2e Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/a4d63f2e Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/a4d63f2e Branch: refs/heads/master Commit: a4d63f2eca1cbe0d33e5b719fcbc343e9d8e06f2 Parents: 0d4f346 Author: Marvin Humphrey <[email protected]> Authored: Mon Nov 30 18:56:36 2015 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Thu Dec 3 13:34:09 2015 -0800 ---------------------------------------------------------------------- go/build.go | 8 ++++ go/lucy/highlight.go | 85 ++++++++++++++++++++++++++++++++++++++++++ go/lucy/highlight_test.go | 44 ++++++++++++++++++++++ 3 files changed, 137 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/a4d63f2e/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index e1fef50..692702d 100644 --- a/go/build.go +++ b/go/build.go @@ -165,6 +165,14 @@ func specClasses(parcel *cfc.Parcel) { docBinding.SpecMethod("Field_Names", "FieldNames() []string") docBinding.Register() + heatMapBinding := cfc.NewGoClass(parcel, "Lucy::Highlight::HeatMap") + heatMapBinding.SetSuppressCtor(true) + heatMapBinding.SpecMethod("Flatten_Spans", "flattenSpans([]Span) []Span") + heatMapBinding.SpecMethod("Generate_Proximity_Boosts", + "generateProximityBoosts([]Span) []Span") + heatMapBinding.SpecMethod("Get_Spans", "getSpans() []Span") + heatMapBinding.Register() + indexerBinding := cfc.NewGoClass(parcel, "Lucy::Index::Indexer") indexerBinding.SpecMethod("", "Close() error") indexerBinding.SpecMethod("Add_Doc", "AddDoc(doc interface{}) error") http://git-wip-us.apache.org/repos/asf/lucy/blob/a4d63f2e/go/lucy/highlight.go ---------------------------------------------------------------------- diff --git a/go/lucy/highlight.go b/go/lucy/highlight.go new file mode 100644 index 0000000..b0ecab8 --- /dev/null +++ b/go/lucy/highlight.go @@ -0,0 +1,85 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package lucy + +/* +#include <stdlib.h> + +#include "Lucy/Highlight/HeatMap.h" +#include "Clownfish/Vector.h" +*/ +import "C" +import "unsafe" + +import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish" + +func vecToSpanSlice(v *C.cfish_Vector) []Span { + if v == nil { + return nil + } + length := int(C.CFISH_Vec_Get_Size(v)) + slice := make([]Span, length) + for i := 0; i < length; i++ { + elem := C.CFISH_Vec_Fetch(v, C.size_t(i)) + slice[i] = clownfish.ToGo(unsafe.Pointer(elem)).(Span) + } + return slice +} + +func spanSliceToVec(slice []Span) *C.cfish_Vector { + if slice == nil { + return nil + } + length := len(slice) + vec := C.cfish_Vec_new(C.size_t(length)) + for i := 0; i < length; i++ { + elem := clownfish.Unwrap(slice[i], "slice[i]") + C.CFISH_Vec_Push(vec, C.cfish_incref(elem)) + } + return vec +} + +func NewHeatMap(spans []Span, window uint32) HeatMap { + spansCF := spanSliceToVec(spans) + defer C.cfish_decref(unsafe.Pointer(spansCF)) + retvalCF := C.lucy_HeatMap_new(spansCF, C.uint32_t(window)) + return clownfish.WRAPAny(unsafe.Pointer(retvalCF)).(HeatMap) +} + +func (h *HeatMapIMP) flattenSpans(spans []Span) []Span { + self := (*C.lucy_HeatMap)(clownfish.Unwrap(h, "h")) + spansCF := spanSliceToVec(spans) + defer C.cfish_decref(unsafe.Pointer(spansCF)) + retvalCF := C.LUCY_HeatMap_Flatten_Spans(self, spansCF) + defer C.cfish_decref(unsafe.Pointer(retvalCF)) + return vecToSpanSlice(retvalCF) +} + +func (h *HeatMapIMP) generateProximityBoosts(spans []Span) []Span { + self := (*C.lucy_HeatMap)(clownfish.Unwrap(h, "h")) + spansCF := spanSliceToVec(spans) + defer C.cfish_decref(unsafe.Pointer(spansCF)) + retvalCF := C.LUCY_HeatMap_Generate_Proximity_Boosts(self, spansCF) + defer C.cfish_decref(unsafe.Pointer(retvalCF)) + return vecToSpanSlice(retvalCF) +} + +func (h *HeatMapIMP) getSpans() []Span { + self := (*C.lucy_HeatMap)(clownfish.Unwrap(h, "h")) + retvalCF := C.LUCY_HeatMap_Get_Spans(self) + return vecToSpanSlice(retvalCF) +} http://git-wip-us.apache.org/repos/asf/lucy/blob/a4d63f2e/go/lucy/highlight_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/highlight_test.go b/go/lucy/highlight_test.go new file mode 100644 index 0000000..fa086ed --- /dev/null +++ b/go/lucy/highlight_test.go @@ -0,0 +1,44 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package lucy + +import "testing" + +func TestHeatMapBasics(t *testing.T) { + spans := make([]Span, 2) + spans[0] = NewSpan(0, 3, 10.0) + spans[1] = NewSpan(30, 5, 2.0) + heatMap := NewHeatMap(spans, 133) + if _, ok := heatMap.(HeatMap); !ok { + t.Errorf("NewHeatMap") + } + boost := heatMap.calcProximityBoost(spans[0], spans[1]) + if boost <= 0.0 { + t.Errorf("calcProximityBoost: %f", boost) + } + if length := len(heatMap.getSpans()); length <= 0 { + t.Errorf("getSpans: %d", length) + } + boosts := heatMap.generateProximityBoosts(spans) + if length := len(boosts); length <= 0 { + t.Errorf("generateProximityBoosts: %d", length) + } + flattened := heatMap.flattenSpans(spans) + if length := len(flattened); length <= 0 { + t.Errorf("flattenSpans: %d", length) + } +}
