1

Adding more util functions

This commit is contained in:
Alex Yatskov 2015-03-24 15:16:58 +09:00
parent 99f00d908e
commit d05c540dd7

95
util.go
View File

@ -22,22 +22,7 @@
package main package main
type Features map[string]float32 import "sort"
type Record struct {
features Features
compatibility float32
}
type RecordStats struct {
compatibility float32
count int
}
type Range struct {
min float32
max float32
}
func innerProduct(features1 Features, features2 Features) float32 { func innerProduct(features1 Features, features2 Features) float32 {
var result float32 var result float32
@ -49,7 +34,7 @@ func innerProduct(features1 Features, features2 Features) float32 {
return result return result
} }
func walkMatches(records []Record, features Features, minScore float32, callback func(Record, float32)) { func walkMatches(records Records, features Features, minScore float32, callback func(Record, float32)) {
for _, record := range records { for _, record := range records {
if score := innerProduct(features, record.features); score >= minScore { if score := innerProduct(features, record.features); score >= minScore {
callback(record, score) callback(record, score)
@ -57,7 +42,7 @@ func walkMatches(records []Record, features Features, minScore float32, callback
} }
} }
func statRecords(records []Record, features Features, minScore float32) RecordStats { func statRecords(records Records, features Features, minScore float32) RecordStats {
var stats RecordStats var stats RecordStats
walkMatches(records, features, minScore, func(record Record, score float32) { walkMatches(records, features, minScore, func(record Record, score float32) {
stats.compatibility += record.compatibility stats.compatibility += record.compatibility
@ -67,7 +52,7 @@ func statRecords(records []Record, features Features, minScore float32) RecordSt
return stats return stats
} }
func step(rng Range, steps int, minScore float32, callback func(float32)) { func stepRange(rng Range, steps int, callback func(float32)) {
stepSize := (rng.max - rng.min) / float32(steps) stepSize := (rng.max - rng.min) / float32(steps)
for i := 0; i < steps; i++ { for i := 0; i < steps; i++ {
@ -79,60 +64,28 @@ func step(rng Range, steps int, minScore float32, callback func(float32)) {
} }
} }
// function findRecords(data, features, minScore) { func findRecords(records Records, features Features, minScore float32) {
// var results = []; var foundRecords Records
// walkMatches(data, features, minScore, function(record, score) { walkMatches(records, features, minScore, func(record Record, score float32) {
// results.push({ foundRecords = append(foundRecords, record)
// name: record.name, })
// score: score,
// distanceToUser: record.distanceToUser / 1000.0,
// distanceToStn: record.distanceToStn / 1000.0,
// closestStn: record.closestStn,
// accessCount: record.accessCount,
// id: record.id
// });
// });
// results.sort(function(a, b) { sort.Sort(foundRecords)
// return b.score - a.score; }
// });
// return results; func project(records Records, features Features, featureName string, minScore float32, rng Range, steps int) []Projection {
// } sampleFeatures := make(Features)
for key, value := range features {
sampleFeatures[key] = value
}
// function project(data, features, feature, minScore, range, steps) { var projection []Projection
// var sample = _.clone(features); stepRange(rng, steps, func(sample float32) {
// var results = []; sampleFeatures[featureName] = sample
stats := statRecords(records, sampleFeatures, minScore)
projection = append(projection, Projection{sample: sample, stats: stats})
})
// step(range, steps, function(position) { return projection
// sample[feature] = position; }
// results.push({
// sample: position,
// stats: statRecords(data, sample, minScore)
// });
// });
// return results;
// }
// function buildHints(data, features, feature, minScore, range, steps) {
// var projection = project(
// data,
// features,
// feature,
// minScore,
// range,
// steps
// );
// var hints = [];
// _.each(projection, function(result) {
// hints.push({
// sample: result.sample,
// stats: result.stats
// });
// });
// return hints;
// }