Back to godoc.org
code.sajari.com/word2vec

Package word2vec

v1.0.0
Latest Go to latest

The latest major version is .

Published: Mar 23, 2018 | License: MIT | Module: code.sajari.com/word2vec

Overview

Package word2vec provides functionality for reading binary word2vec models and performing cosine similarity queries (see https://code.google.com/p/word2vec/).

Index

func Add

func Add(e Expr, weight float32, words []string)

Add is a convenience method for adding multiple words to an Expr.

func AddWeight

func AddWeight(e Expr, weights []float32, words []string)

AddWeight is a convenience method for adding multiple weighted words to an Expr.

func MultiCosN

func MultiCosN(m *Model, exprs []Expr, n int) ([][]Match, error)

MultiCosN takes a list of expressions and computes the n most similar words for each.

func NewServer

func NewServer(c Coser) http.Handler

NewServer creates a new word2vec server which exports endpoints for performing similarity queries on a word2vec Model.

type Client

type Client struct {
	Addr string
}

Client is type which implements Coser and evaluates Expr similarity queries using a word2vec Server (see above).

func (Client) Cos

func (c Client) Cos(x, y Expr) (float32, error)

Cos implements Coser.

func (Client) CosN

func (c Client) CosN(e Expr, n int) ([]Match, error)

CosN implements Coser.

func (Client) Coses

func (c Client) Coses(pairs [][2]Expr) ([]float32, error)

Coses implements Coser.

type Coser

type Coser interface {
	// Cos computes the cosine similarity of the expressions.
	Cos(e, f Expr) (float32, error)

	// Coses computes the cosine similarity of pairs of expressions.
	Coses(pairs [][2]Expr) ([]float32, error)

	// CosN computes the N most similar words to the expression.
	CosN(e Expr, n int) ([]Match, error)
}

Coser is an interface which defines methods which can evaluate cosine similarity between Exprs.

func NewCache

func NewCache(c Coser) Coser

NewCache returns a Coser which will cache repeated calls to the Cos method, particularly useful when using Client.

type Expr

type Expr map[string]float32

Expr is a type which represents a linear expresssion of (weight, word) pairs which can be evaluated to a vector by a word2vec Model.

func (Expr) Add

func (e Expr) Add(weight float32, word string)

Add appends the given word with specified weight to the expression. If the word already exists in the expression, then the weights are added.

func (Expr) Eval

func (e Expr) Eval(m *Model) (Vector, error)

Eval evaluates the Expr to a Vector using a Model.

type Mapper

type Mapper interface {
	Map(words []string) map[string]Vector
}

Mapper is an interface which defines a method which can return a mapping of word -> Vector for each word in words.

type Match

type Match struct {
	Word  string  `json:"word"`
	Score float32 `json:"score"`
}

Match is a type which represents a pairing of a word and score indicating the similarity of this word against a search word.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is a type which represents a word2vec Model and implements the Coser and Mapper interfaces.

func FromReader

func FromReader(r io.Reader) (*Model, error)

FromReader creates a Model using the binary model data provided by the io.Reader.

func (*Model) Cos

func (m *Model) Cos(a, b Expr) (float32, error)

Cos returns the cosine similarity of the given expressions.

func (*Model) CosN

func (m *Model) CosN(e Expr, n int) ([]Match, error)

CosN computes the n most similar words to the expression. Returns an error if the expression could not be evaluated.

func (*Model) Coses

func (m *Model) Coses(pairs [][2]Expr) ([]float32, error)

Coses returns the cosine similarity of each pair of expressions in the list. Returns immediately if an error occurs.

func (*Model) Dim

func (m *Model) Dim() int

Dim returns the dimention of the vectors in the model.

func (*Model) Eval

func (m *Model) Eval(expr Expr) (Vector, error)

Eval constructs a vector by evaluating the expression vector. Returns an error if a word is not in the model.

func (*Model) Map

func (m *Model) Map(words []string) map[string]Vector

Map returns a mapping word -> Vector for each word in `words`. Unknown words are ignored.

func (*Model) Size

func (m *Model) Size() int

Size returns the number of words in the model.

type NotFoundError

type NotFoundError struct {
	Word string
}

NotFoundError is an error returned from Model functions when an input word is not in the model.

func (NotFoundError) Error

func (e NotFoundError) Error() string

type Vector

type Vector []float32

Vector is a type which represents a word vector.

func (Vector) Add

func (v Vector) Add(a float32, u Vector)

Add performs v += a * u (in-place).

func (Vector) Dot

func (v Vector) Dot(u Vector) float32

Dot computes the dot product with u.

func (Vector) Norm

func (v Vector) Norm() float32

Norm computes the Euclidean norm of the vector.

func (Vector) Normalise

func (v Vector) Normalise()

Normalise normalises the vector in-place.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier