1
restaurant-search/build/converter.go

237 lines
5.0 KiB
Go
Raw Permalink 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"
2015-09-22 02:06:20 +00:00
"net/url"
2015-09-20 11:02:03 +00:00
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/naoina/toml"
)
//
// semantics
//
type semantics struct {
2015-11-15 08:26:24 +00:00
Accommodating float64
Affordable float64
Atmospheric float64
Delicious float64
2015-09-20 11:02:03 +00:00
}
func (s semantics) combine(other semantics, weight float64) semantics {
return semantics{
2015-11-15 08:26:24 +00:00
s.Accommodating + other.Accommodating*weight,
2015-09-20 11:02:03 +00:00
s.Affordable + other.Affordable*weight,
s.Atmospheric + other.Atmospheric*weight,
s.Delicious + other.Delicious*weight,
}
}
func (s semantics) reduce(weight float64) semantics {
return semantics{
2015-11-15 08:26:24 +00:00
s.Accommodating / weight,
2015-09-20 11:02:03 +00:00
s.Affordable / weight,
s.Atmospheric / weight,
s.Delicious / weight,
}
}
//
2015-09-22 02:06:20 +00:00
// selector
2015-09-20 11:02:03 +00:00
//
2015-09-22 02:06:20 +00:00
type selector struct {
2015-09-20 11:02:03 +00:00
Path string
Attr string
RegEx string
regExComp *regexp.Regexp
}
2015-09-22 02:06:20 +00:00
func (l *selector) 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 = ""
}
}
2015-09-22 02:57:07 +00:00
strs = append(strs, strings.TrimSpace(str))
2015-09-20 11:02:03 +00:00
})
return strs, err
}
2015-09-22 02:06:20 +00:00
func (l *selector) locateString(doc *goquery.Document) (string, error) {
2015-09-20 13:30:38 +00:00
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-22 02:06:20 +00:00
func (l *selector) locateInt(doc *goquery.Document) (int64, error) {
2015-09-20 13:30:38 +00:00
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-22 02:06:20 +00:00
func (l *selector) locateFloat(doc *goquery.Document) (float64, error) {
2015-09-20 13:30:38 +00:00
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-21 04:07:38 +00:00
// converter
2015-09-20 11:02:03 +00:00
//
2015-09-21 04:07:38 +00:00
type converter struct {
2015-09-22 02:57:07 +00:00
Name string
2015-09-22 02:06:20 +00:00
Domains []string
2015-09-20 11:02:03 +00:00
Index struct {
2015-09-22 02:06:20 +00:00
Items selector
Next selector
2015-09-20 11:02:03 +00:00
}
Item struct {
2015-09-22 02:06:20 +00:00
Name selector
Address selector
Count selector
2015-09-20 13:30:38 +00:00
Props map[string]struct {
semantics
2015-09-22 02:06:20 +00:00
selector
2015-09-20 13:30:38 +00:00
Scale float64
}
2015-09-20 11:02:03 +00:00
}
}
2015-09-21 04:07:38 +00:00
func newConverter(filename string) (*converter, error) {
2015-09-20 11:02:03 +00:00
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
2015-09-21 04:07:38 +00:00
var desc converter
2015-09-20 13:30:38 +00:00
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
}
2015-09-21 04:07:38 +00:00
func (c converter) define(keyword string) semantics {
return c.Item.Props[keyword].semantics
2015-09-20 11:02:03 +00:00
}
2015-09-22 02:06:20 +00:00
func (c converter) compatible(address string) bool {
parsed, err := url.Parse(address)
if err != nil {
return false
}
for _, d := range c.Domains {
if d == parsed.Host {
return true
}
}
return false
}
2015-09-21 04:07:38 +00:00
func (c converter) index(doc *goquery.Document) (next string, items []string, err error) {
if items, err = c.Index.Items.locateStrings(doc); err != nil {
2015-09-20 11:02:03 +00:00
return
}
2015-09-21 04:07:38 +00:00
if next, err = c.Index.Next.locateString(doc); err != nil {
2015-09-20 11:02:03 +00:00
return
}
return
}
2015-09-21 04:07:38 +00:00
func (c converter) review(doc *goquery.Document) (name, address string, features map[string]float64, count int64, err error) {
if name, err = c.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-21 04:07:38 +00:00
if address, err = c.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-21 04:07:38 +00:00
if count, err = c.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-21 04:07:38 +00:00
for n, p := range c.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
}
2015-09-22 03:20:19 +00:00
features[n] = value*2.0 - 1.0
2015-09-20 11:02:03 +00:00
}
return
}