1
restaurant-search/build/descriptor.go

219 lines
4.7 KiB
Go
Raw Normal View History

2015-09-20 11:02:03 +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 (
"errors"
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/naoina/toml"
)
//
// semantics
//
type semantics struct {
Accomodating float64
Affordable float64
Atmospheric float64
Delicious float64
}
func (s semantics) combine(other semantics, weight float64) semantics {
return semantics{
s.Accomodating + other.Accomodating*weight,
s.Affordable + other.Affordable*weight,
s.Atmospheric + other.Atmospheric*weight,
s.Delicious + other.Delicious*weight,
}
}
func (s semantics) reduce(weight float64) semantics {
return semantics{
s.Accomodating / weight,
s.Affordable / weight,
s.Atmospheric / weight,
s.Delicious / weight,
}
}
//
2015-09-20 13:30:38 +00:00
// locator
2015-09-20 11:02:03 +00:00
//
2015-09-20 13:30:38 +00:00
type locator struct {
2015-09-20 11:02:03 +00:00
Path string
Attr string
RegEx string
regExComp *regexp.Regexp
}
2015-09-20 13:30:38 +00:00
func (l *locator) locateStrings(doc *goquery.Document) ([]string, error) {
2015-09-20 11:02:03 +00:00
var err error
2015-09-20 13:30:38 +00:00
if len(l.RegEx) > 0 && l.regExComp == nil {
if l.regExComp, err = regexp.Compile(l.RegEx); err != nil {
2015-09-20 11:02:03 +00:00
return nil, err
}
}
var strs []string
2015-09-20 13:30:38 +00:00
doc.Find(l.Path).Each(func(index int, sel *goquery.Selection) {
2015-09-20 11:02:03 +00:00
var str string
2015-09-20 13:30:38 +00:00
if len(l.Attr) > 0 {
str, _ = sel.Attr(l.Attr)
2015-09-20 11:02:03 +00:00
} else {
str = sel.Text()
}
2015-09-20 13:30:38 +00:00
if l.regExComp != nil {
if matches := l.regExComp.FindStringSubmatch(str); len(matches) > 1 {
2015-09-20 11:02:03 +00:00
str = matches[1]
} else {
str = ""
}
}
strs = append(strs, str)
})
return strs, err
}
2015-09-20 13:30:38 +00:00
func (l *locator) locateString(doc *goquery.Document) (string, error) {
strs, err := l.locateStrings(doc)
2015-09-20 11:02:03 +00:00
if err != nil {
return "", err
}
return strings.Join(strs, " "), nil
}
2015-09-20 13:30:38 +00:00
func (l *locator) locateInt(doc *goquery.Document) (int64, error) {
str, err := l.locateString(doc)
2015-09-20 11:02:03 +00:00
if err != nil {
return 0, err
}
return strconv.ParseInt(str, 10, 8)
}
2015-09-20 13:30:38 +00:00
func (l *locator) locateFloat(doc *goquery.Document) (float64, error) {
str, err := l.locateString(doc)
2015-09-20 11:02:03 +00:00
if err != nil {
return 0.0, err
}
return strconv.ParseFloat(str, 8)
}
//
2015-09-20 13:30:38 +00:00
// descriptor
2015-09-20 11:02:03 +00:00
//
2015-09-20 13:30:38 +00:00
type descriptor struct {
2015-09-20 11:02:03 +00:00
Index struct {
2015-09-20 13:30:38 +00:00
Items locator
Next locator
2015-09-20 11:02:03 +00:00
}
Item struct {
2015-09-20 13:30:38 +00:00
Name locator
Address locator
Count locator
Props map[string]struct {
semantics
locator
Scale float64
}
2015-09-20 11:02:03 +00:00
}
}
2015-09-20 13:30:38 +00:00
func newDescriptor(filename string) (*descriptor, error) {
2015-09-20 11:02:03 +00:00
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
2015-09-20 13:30:38 +00:00
var desc descriptor
if err := toml.Unmarshal(bytes, &desc); err != nil {
2015-09-20 11:02:03 +00:00
return nil, err
}
2015-09-20 13:30:38 +00:00
return &desc, nil
2015-09-20 11:02:03 +00:00
}
func (d descriptor) define(keyword string) semantics {
2015-09-20 13:30:38 +00:00
return d.Item.Props[keyword].semantics
2015-09-20 11:02:03 +00:00
}
func (d descriptor) index(doc *goquery.Document) (next string, items []string, err error) {
2015-09-20 13:30:38 +00:00
if items, err = d.Index.Items.locateStrings(doc); err != nil {
2015-09-20 11:02:03 +00:00
return
}
2015-09-20 13:30:38 +00:00
if next, err = d.Index.Next.locateString(doc); err != nil {
2015-09-20 11:02:03 +00:00
return
}
return
}
func (d descriptor) review(doc *goquery.Document) (name, address string, features map[string]float64, count int64, err error) {
2015-09-20 13:30:38 +00:00
if name, err = d.Item.Name.locateString(doc); err != nil || len(name) == 0 {
2015-09-20 11:02:03 +00:00
err = errors.New("invalid name")
return
}
2015-09-20 13:30:38 +00:00
if address, err = d.Item.Address.locateString(doc); err != nil || len(address) == 0 {
2015-09-20 11:02:03 +00:00
err = errors.New("invalid address")
return
}
2015-09-20 13:30:38 +00:00
if count, err = d.Item.Count.locateInt(doc); err != nil {
2015-09-20 11:02:03 +00:00
err = errors.New("invalid review count")
return
}
features = make(map[string]float64)
2015-09-20 13:30:38 +00:00
for n, p := range d.Item.Props {
2015-09-20 11:02:03 +00:00
var value float64
2015-09-20 13:30:38 +00:00
if value, err = p.locateFloat(doc); err != nil {
2015-09-20 11:02:03 +00:00
err = fmt.Errorf("invalid feature value for %s", n)
return
}
2015-09-20 13:30:38 +00:00
if p.Scale != 0.0 {
value /= p.Scale
2015-09-20 11:02:03 +00:00
}
features[n] = value
}
return
}