engine

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2019 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package engine manages configuration, storage and update.

  • configuration: load configurations from the TOML file.
  • storage: manages data based on BoltDB.
  • update: generate recommendations and save into the database.

Index

Constants

View Source
const (
	BucketNeighbors  = "neighbors"  // Bucket name for neighbors
	BucketRecommends = "recommends" // Bucket name for recommendations

)
View Source
const (
	ListPop    = "pop"    // List name for popular items
	ListLatest = "latest" // List name for latest items
)

Variables

This section is empty.

Functions

func LoadModel

func LoadModel(name string, params base.Params) core.ModelInterface

LoadModel creates model from name and parameters.

func LoadSimilarity

func LoadSimilarity(name string) base.FuncSimilarity

LoadSimilarity creates similarity metric from name.

func Update

func Update(config TomlConfig, metaData toml.MetaData, db *DB) error

Update all kinds recommendations for the database.

func UpdateLatest added in v0.1.3

func UpdateLatest(cacheSize int, db *DB) error

UpdateLatest updates latest items.

func UpdateNeighbors

func UpdateNeighbors(name string, cacheSize int, dataSet core.DataSetInterface, db *DB) error

UpdateNeighbors updates neighbors for the database.

func UpdatePopItem added in v0.1.3

func UpdatePopItem(cacheSize int, db *DB) error

UpdatePopItem updates popular items for the database.

func UpdatePopularity added in v0.1.3

func UpdatePopularity(dataSet core.DataSetInterface, db *DB) error

UpdatePopularity updates popularity for all items.

func UpdateRecommends

func UpdateRecommends(name string, params base.Params, cacheSize int, fitJobs int, dataSet core.DataSetInterface, db *DB) error

UpdateRecommends updates personalized recommendations for the database.

Types

type DB

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

DB manages all data for the engine.

func Open

func Open(path string) (*DB, error)

Open a connection to the database.

func (*DB) Close

func (db *DB) Close() error

Close the connection to the database.

func (*DB) CountFeedback

func (db *DB) CountFeedback() (int, error)

CountFeedback returns the number of feedback in the database.

func (*DB) CountItems

func (db *DB) CountItems() (int, error)

CountItems returns the number of items in the database.

func (*DB) CountUsers

func (db *DB) CountUsers() (int, error)

CountUsers returns the number of users in the database.

func (*DB) GetFeedback

func (db *DB) GetFeedback() (users, items []string, feedback []float64, err error)

GetFeedback returns all feedback in the database.

func (*DB) GetIdentList added in v0.1.3

func (db *DB) GetIdentList(bucketName string, id string, n int) ([]RecommendedItem, error)

GetRecommends gets n recommendations for a user.

func (*DB) GetItem added in v0.1.3

func (db *DB) GetItem(itemId string) (Item, error)

GetItem gets a item from database by item ID.

func (*DB) GetItems

func (db *DB) GetItems() ([]Item, error)

GetItems returns all items in the dataset.

func (*DB) GetItemsByID added in v0.1.3

func (db *DB) GetItemsByID(id []string) ([]Item, error)

GetItem gets items from database by item IDs.

func (*DB) GetList added in v0.1.3

func (db *DB) GetList(name string, n int) ([]RecommendedItem, error)

GetList gets a list from the database.

func (*DB) GetMeta

func (db *DB) GetMeta(name string) (string, error)

GetMeta gets the value of a metadata.

func (*DB) GetRandom

func (db *DB) GetRandom(n int) ([]RecommendedItem, error)

GetRandom returns random items.

func (*DB) GetUserFeedback

func (db *DB) GetUserFeedback(userId string) ([]Feedback, error)

GetUserFeedback get a user's feedback.

func (*DB) GetUsers

func (db *DB) GetUsers() ([]string, error)

GetUsers get all user IDs.

func (*DB) InsertFeedback

func (db *DB) InsertFeedback(userId, itemId string, feedback float64) error

InsertFeedback inserts a feedback into the database.

func (*DB) InsertItem

func (db *DB) InsertItem(itemId string, timestamp *time.Time) error

InsertItem inserts a item into the database.

func (*DB) InsertItems added in v0.1.3

func (db *DB) InsertItems(itemId []string, timestamps []time.Time) error

InsertItems inserts multiple items into the database.

func (*DB) InsertMultiFeedback

func (db *DB) InsertMultiFeedback(userId, itemId []string, feedback []float64) error

InsertMultiFeedback inserts multiple feedback into the database.

func (*DB) InsertMultiUserFeedback

func (db *DB) InsertMultiUserFeedback(userId, itemId []string, feedback []float64) error

InsertMultiUserFeedback inserts multiple feedback into the user feedback bucket of the database.

func (*DB) InsertUserFeedback

func (db *DB) InsertUserFeedback(userId, itemId string, feedback float64) error

InsertUserFeedback inserts a feedback into the user feedback bucket of the database.

func (*DB) LoadFeedbackFromCSV

func (db *DB) LoadFeedbackFromCSV(fileName string, sep string, hasHeader bool) error

LoadFeedbackFromCSV import feedback from a CSV file into the database.

func (*DB) LoadItemsFromCSV

func (db *DB) LoadItemsFromCSV(fileName string, sep string, hasHeader bool, dateColumn int) error

LoadItemsFromCSV imports items from a CSV file into the database.

func (*DB) PutIdentList added in v0.1.3

func (db *DB) PutIdentList(bucketName string, id string, items []RecommendedItem) error

SetRecommends sets recommendations for a user.

func (*DB) PutList added in v0.1.3

func (db *DB) PutList(name string, items []RecommendedItem) error

PutList saves a list into the database.

func (*DB) SaveFeedbackToCSV

func (db *DB) SaveFeedbackToCSV(fileName string, sep string, header bool) error

SaveFeedbackToCSV exports feedback from the database into a CSV file.

func (*DB) SaveItemsToCSV

func (db *DB) SaveItemsToCSV(fileName string, sep string, header bool, date bool) error

SaveItemsToCSV exports items from the database into a CSV file.

func (*DB) SetMeta

func (db *DB) SetMeta(name string, val string) error

SetMeta sets the value of a metadata.

func (*DB) ToDataSet

func (db *DB) ToDataSet() (*core.DataSet, error)

ToDataSet creates a dataset from the database.

func (*DB) UpdatePopularity added in v0.1.3

func (db *DB) UpdatePopularity(itemId []string, popularity []float64) error

UpdatePopularity update popularity of items.

type DatabaseConfig

type DatabaseConfig struct {
	File string `toml:"file"`
}

DatabaseConfig is the configuration for the database.

type Feedback added in v0.1.3

type Feedback struct {
	FeedbackKey
	Rating float64
}

Feedback stores feedback.

type FeedbackKey

type FeedbackKey struct {
	UserId string
	ItemId string
}

FeedbackKey identifies feedback.

type Item added in v0.1.3

type Item struct {
	ItemId     string
	Popularity float64
	Timestamp  time.Time
}

Item stores meta data about item.

func TopItems added in v0.1.3

func TopItems(itemId []Item, weight []float64, n int) (topItemId []Item, topWeight []float64)

TopItems finds top items by weights.

type ParamsConfig

type ParamsConfig struct {
	// Hyper-parameters
	Lr            float64 `toml:"lr"`              // learning rate
	Reg           float64 `toml:"reg"`             // regularization strength
	NEpochs       int     `toml:"n_epochs"`        // number of epochs
	NFactors      int     `toml:"n_factors"`       // number of factors
	RandomState   int     `toml:"random_state"`    // random state (seed)
	UseBias       bool    `toml:"use_bias"`        // use bias
	InitMean      float64 `toml:"init_mean"`       // mean of gaussian initial parameter
	InitStdDev    float64 `toml:"init_std"`        // standard deviation of gaussian initial parameter
	InitLow       float64 `toml:"init_low"`        // lower bound of uniform initial parameter
	InitHigh      float64 `toml:"init_high"`       // upper bound of uniform initial parameter
	NUserClusters int     `toml:"n_user_clusters"` // number of user cluster
	NItemClusters int     `toml:"n_item_clusters"` // number of item cluster
	Type          string  `toml:"type"`            // type for KNN
	UserBased     bool    `toml:"user_based"`      // user based if true. otherwise item based.
	Similarity    string  `toml:"similarity"`      // similarity metrics
	K             int     `toml:"k"`               // number of neighbors
	MinK          int     `toml:"min_k"`           // least number of neighbors
	Shrinkage     int     `toml:"shrinkage"`       // shrinkage strength of similarity
	Alpha         float64 `toml:"alpha"`           // alpha value, depend on context
}

ParamsConfig is the configuration for hyper-parameters of the recommendation model.

func (*ParamsConfig) ToParams

func (config *ParamsConfig) ToParams(metaData toml.MetaData) base.Params

ToParams convert a configuration for hyper-parameters into hyper-parameters.

type RecommendConfig

type RecommendConfig struct {
	Model           string `toml:"model"`
	Similarity      string `toml:"similarity"`
	CacheSize       int    `toml:"cache_size"`
	UpdateThreshold int    `toml:"update_threshold"`
	CheckPeriod     int    `toml:"check_period"`
	FitJobs         int    `toml:"fit_jobs"`
}

RecommendConfig is the configuration for recommendation.

type RecommendedItem

type RecommendedItem struct {
	Item
	Score float64 // score
}

RecommendedItem is the structure for a recommended item.

func Ranking added in v0.1.3

func Ranking(items []RecommendedItem, n int, p, t, c float64) []RecommendedItem

Ranking ranks items by popularity, timestamps and collaborative filtering scores..

type RecommendedItems added in v0.1.3

type RecommendedItems []RecommendedItem

func (RecommendedItems) Len added in v0.1.3

func (items RecommendedItems) Len() int

func (RecommendedItems) Less added in v0.1.3

func (items RecommendedItems) Less(i, j int) bool

func (RecommendedItems) Swap added in v0.1.3

func (items RecommendedItems) Swap(i, j int)

type ServerConfig

type ServerConfig struct {
	Host string `toml:"host"`
	Port int    `toml:"port"`
}

ServerConfig is the configuration for the server.

type TomlConfig

type TomlConfig struct {
	Server    ServerConfig    `toml:"server"`
	Database  DatabaseConfig  `toml:"database"`
	Params    ParamsConfig    `toml:"params"`
	Recommend RecommendConfig `toml:"recommend"`
}

TomlConfig is the configuration for the engine.

func LoadConfig

func LoadConfig(path string) (TomlConfig, toml.MetaData)

LoadConfig loads configuration from toml file.

func (*TomlConfig) FillDefault

func (config *TomlConfig) FillDefault(meta toml.MetaData)

FillDefault fill default values for missing values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL