1
restaurant-search/util.go

185 lines
5.2 KiB
Go
Raw Normal View History

2015-03-24 03:45:18 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"github.com/kellydunn/golang-geo"
"log"
"math"
"sort"
)
2015-03-24 11:04:52 +00:00
func innerProduct(features1 featureMap, features2 featureMap) float64 {
var result float64
2015-03-24 03:45:18 +00:00
for key, value1 := range features1 {
value2, _ := features2[key]
result += value1 * value2
}
return result
}
2015-03-24 11:04:52 +00:00
func walkMatches(entries records, features featureMap, minScore float64, callback func(record, float64)) {
for _, record := range entries {
2015-03-24 03:45:18 +00:00
if score := innerProduct(features, record.features); score >= minScore {
callback(record, score)
}
}
}
2015-03-24 04:17:39 +00:00
2015-03-24 11:04:52 +00:00
func statRecords(entries records, features featureMap, minScore float64) recordStats {
var stats recordStats
walkMatches(entries, features, minScore, func(record record, score float64) {
2015-03-24 04:17:39 +00:00
stats.compatibility += record.compatibility
stats.count++
})
return stats
}
2015-03-24 11:04:52 +00:00
func stepRange(bounds queryBounds, steps int, callback func(float64)) {
2015-03-24 08:58:35 +00:00
stepSize := (bounds.max - bounds.min) / float64(steps)
2015-03-24 04:17:39 +00:00
for i := 0; i < steps; i++ {
2015-03-24 08:58:35 +00:00
stepMax := bounds.max - stepSize*float64(i)
2015-03-24 04:17:39 +00:00
stepMin := stepMax - stepSize
stepMid := (stepMin + stepMax) / 2
callback(stepMid)
}
}
2015-03-24 11:04:52 +00:00
func findRecords(entries records, features featureMap, minScore float64) {
var foundRecords records
2015-03-24 06:16:58 +00:00
2015-03-24 11:04:52 +00:00
walkMatches(entries, features, minScore, func(record record, score float64) {
2015-03-24 06:16:58 +00:00
foundRecords = append(foundRecords, record)
})
sort.Sort(foundRecords)
}
2015-03-24 11:04:52 +00:00
func project(entries records, features featureMap, featureName string, minScore float64, bounds queryBounds, steps int) []queryProjection {
sampleFeatures := make(featureMap)
2015-03-24 06:16:58 +00:00
for key, value := range features {
sampleFeatures[key] = value
}
2015-03-24 11:04:52 +00:00
var projection []queryProjection
2015-03-24 08:58:35 +00:00
stepRange(bounds, steps, func(sample float64) {
2015-03-24 06:16:58 +00:00
sampleFeatures[featureName] = sample
2015-03-24 11:04:52 +00:00
stats := statRecords(entries, sampleFeatures, minScore)
projection = append(projection, queryProjection{sample: sample, stats: stats})
2015-03-24 06:16:58 +00:00
})
return projection
}
2015-03-24 11:04:52 +00:00
func computeRecordGeo(entries records, context queryContext) {
distUserMin := math.MaxFloat64
distUserMax := 0.0
2015-03-24 11:04:52 +00:00
for _, record := range entries {
2015-03-24 08:58:35 +00:00
if context.geo.valid {
userPoint := geo.NewPoint(context.geo.latitude, context.geo.longitude)
recordPoint := geo.NewPoint(record.geo.latitude, context.geo.longitude)
record.distanceToUser = userPoint.GreatCircleDistance(recordPoint)
}
if record.distanceToUser < distUserMin {
distUserMin = record.distanceToUser
}
if record.distanceToUser > distUserMax {
distUserMax = record.distanceToUser
}
}
distUserRange := distUserMax - distUserMin
2015-03-24 11:04:52 +00:00
for _, record := range entries {
nearby := -((record.distanceToUser-distUserMin)/distUserRange - 0.5) * 2.0
accessible := 1.0 - (record.distanceToStn / context.walkingDist)
if accessible < -1.0 {
accessible = 1.0
} else if accessible > 1.0 {
accessible = 1.0
}
record.features["nearby"] = nearby
record.features["accessible"] = accessible
}
}
2015-03-24 11:04:52 +00:00
func computeRecordPopularity(entries records, context queryContext) {
for _, record := range entries {
historyRows, err := db.Query("SELECT id FROM history WHERE reviewId = (?)", record.id)
if err != nil {
log.Fatal(err)
}
var groupSum float64
var groupCount int
for historyRows.Next() {
var historyId int
if err := historyRows.Scan(&historyId); err != nil {
log.Fatal(err)
}
groupRows, err := db.Query("SELECT categoryId, categoryValue FROM historyGroups WHERE historyId = (?)", historyId)
if err != nil {
log.Fatal(err)
}
2015-03-24 11:04:52 +00:00
recordProfile := make(featureMap)
for groupRows.Next() {
var categoryId int
var categoryValue float64
if err := groupRows.Scan(&categoryId, &categoryValue); err != nil {
log.Fatal(err)
}
recordProfile[categoryId] = categoryValue
}
if err := groupRows.Err(); err != nil {
log.Fatal(err)
}
groupSum += innerProduct(recordProfile, context.profile)
groupCount++
}
if err := historyRows.Err(); err != nil {
log.Fatal(err)
}
var compatibility float64
if groupCount > 0 {
compatibility = groupSum / float64(groupCount)
}
record.features["compatibility"] = compatibility
}
}