Skip to content

Commit

Permalink
gh-20 include custom extension in VectorForWord
Browse files Browse the repository at this point in the history
fixes #20
  • Loading branch information
etiennedi committed Jan 9, 2020
1 parent d21e9db commit 1f9dd69
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
10 changes: 7 additions & 3 deletions extensions/storer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

core "github.com/semi-technologies/contextionary/contextionary/core"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -14,7 +15,8 @@ import (
func Test_Storer(t *testing.T) {
t.Run("with invalid inputs", func(t *testing.T) {
repo := &fakeStorerRepo{}
s := NewStorer(&fakeVectorizer{}, repo)
logger, _ := test.NewNullLogger()
s := NewStorer(&fakeVectorizer{}, repo, logger)
inp := ExtensionInput{
Definition: "an electrical device to store energy in the short term",
Weight: 1,
Expand Down Expand Up @@ -69,7 +71,8 @@ func Test_Storer(t *testing.T) {

t.Run("with valid input (single word)", func(t *testing.T) {
repo := &fakeStorerRepo{}
s := NewStorer(&fakeVectorizer{}, repo)
logger, _ := test.NewNullLogger()
s := NewStorer(&fakeVectorizer{}, repo, logger)
concept := "capacitor"
inp := ExtensionInput{
Definition: "an electrical device to store energy in the short term",
Expand All @@ -93,7 +96,8 @@ func Test_Storer(t *testing.T) {
// this is a special case because users will input their words using
// spaces, but we store them using snake_case
repo := &fakeStorerRepo{}
s := NewStorer(&fakeVectorizer{}, repo)
logger, _ := test.NewNullLogger()
s := NewStorer(&fakeVectorizer{}, repo, logger)
concept := "flux capacitor"
inp := ExtensionInput{
Definition: "an energy source for cars to travel through time",
Expand Down
13 changes: 6 additions & 7 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,16 @@ func pbWordsFromStrings(input []string) []*pb.Word {
}

func (s *server) VectorForWord(ctx context.Context, params *pb.Word) (*pb.Vector, error) {
i := s.combinedContextionary.WordToItemIndex(params.Word)
if !i.IsPresent() {
return nil, fmt.Errorf("word %s is not in the contextionary", params.Word)
}

v, err := s.combinedContextionary.GetVectorForItemIndex(i)
wo, err := s.vectorizer.VectorForWord(params.Word)
if err != nil {
return nil, err
}

return vectorToProto(v), nil
if wo == nil {
return nil, fmt.Errorf("word %s is not in the contextionary", params.Word)
}

return vectorToProto(wo.vector), nil
}

func (s *server) VectorForCorpi(ctx context.Context, params *pb.Corpi) (*pb.Vector, error) {
Expand Down
6 changes: 3 additions & 3 deletions server/corpus_vectorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (cv *Vectorizer) vectorForWordOrWords(parts []string) (*vectorWithOccurrenc
return cv.vectorForWords(parts)
}

return cv.vectorForWord(parts[0])
return cv.VectorForWord(parts[0])
}

type vectorWithOccurrence struct {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (cv *Vectorizer) vectorsAndOccurrences(words []string) ([]core.Vector, []ui
// Note that n goes all the way down to zero, so once we didn't find
// any compound words, we're checking the individual word.
compound := cv.compound(cv.nextWords(words, wordPos, additionalWords)...)
vector, err := cv.vectorForWord(compound)
vector, err := cv.VectorForWord(compound)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func (cv *Vectorizer) compound(words ...string) string {
return strings.Join(words, "_")
}

func (cv *Vectorizer) vectorForWord(word string) (*vectorWithOccurrence, error) {
func (cv *Vectorizer) VectorForWord(word string) (*vectorWithOccurrence, error) {
ext, err := cv.extensions.Lookup(word)
if err != nil {
return nil, fmt.Errorf("lookup custom word: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion test/journey/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.12
FROM golang:1.13
WORKDIR /testfiles
COPY go.mod go.sum ./
RUN go mod download
Expand Down
10 changes: 6 additions & 4 deletions test/journey/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/semi-technologies/contextionary/test/journey

go 1.12
go 1.13

require (
github.com/semi-technologies/contextionary v0.0.0-20190529072917-5791db3de20f79fca2dc96e72e18e5e9eb5e7954
github.com/stretchr/testify v1.3.0
google.golang.org/grpc v1.21.0
github.com/semi-technologies/contextionary v0.0.0-20200108103649-8f26e727d5ec
github.com/stretchr/testify v1.4.0
google.golang.org/grpc v1.24.0
)

replace github.com/coreos/go-systemd => github.com/coreos/go-systemd/v22 v22.0.0
Loading

0 comments on commit 1f9dd69

Please sign in to comment.