1
This commit is contained in:
Alex Yatskov 2015-09-20 22:30:38 +09:00
parent c4791a87e9
commit 5c29c7dbb4
2 changed files with 47 additions and 64 deletions

View File

@ -18,8 +18,8 @@ name = "tripadvisor"
path = "h3.reviews_header" path = "h3.reviews_header"
regex = "^(\d+)" regex = "^(\d+)"
[item.semantics] [item.props]
[item.semantics.service] [item.props.service]
accomodating = 1.0 accomodating = 1.0
affordable = 0.0 affordable = 0.0
atmospheric = 0.0 atmospheric = 0.0
@ -30,7 +30,7 @@ name = "tripadvisor"
regex = "^([0-9]*\.?[0-9]+)" regex = "^([0-9]*\.?[0-9]+)"
attr = "alt" attr = "alt"
[item.semantics.food] [item.props.food]
accomodating = 0.0 accomodating = 0.0
affordable = 0.0 affordable = 0.0
atmospheric = 0.0 atmospheric = 0.0
@ -41,7 +41,7 @@ name = "tripadvisor"
regex = "^([0-9]*\.?[0-9]+)" regex = "^([0-9]*\.?[0-9]+)"
attr = "alt" attr = "alt"
[item.semantics.value] [item.props.value]
accomodating = 0.0 accomodating = 0.0
affordable = 1.0 affordable = 1.0
atmospheric = 0.0 atmospheric = 0.0
@ -52,7 +52,7 @@ name = "tripadvisor"
regex = "^([0-9]*\.?[0-9]+)" regex = "^([0-9]*\.?[0-9]+)"
attr = "alt" attr = "alt"
[item.semantics.atmosphere] [item.props.atmosphere]
accomodating = 0.0 accomodating = 0.0
affordable = 0.0 affordable = 0.0
atmospheric = 1.0 atmospheric = 1.0

View File

@ -34,16 +34,6 @@ import (
"github.com/naoina/toml" "github.com/naoina/toml"
) )
//
// descriptor
//
type descriptor struct {
gc *geoCache
wc *webCache
conf descConf
}
// //
// semantics // semantics
// //
@ -73,9 +63,9 @@ func (s semantics) reduce(weight float64) semantics {
} }
// //
// pathConf // locator
// //
type pathConf struct { type locator struct {
Path string Path string
Attr string Attr string
RegEx string RegEx string
@ -83,25 +73,25 @@ type pathConf struct {
regExComp *regexp.Regexp regExComp *regexp.Regexp
} }
func (c *pathConf) locateStrings(doc *goquery.Document) ([]string, error) { func (l *locator) locateStrings(doc *goquery.Document) ([]string, error) {
var err error var err error
if len(c.RegEx) > 0 && c.regExComp == nil { if len(l.RegEx) > 0 && l.regExComp == nil {
if c.regExComp, err = regexp.Compile(c.RegEx); err != nil { if l.regExComp, err = regexp.Compile(l.RegEx); err != nil {
return nil, err return nil, err
} }
} }
var strs []string var strs []string
doc.Find(c.Path).Each(func(index int, sel *goquery.Selection) { doc.Find(l.Path).Each(func(index int, sel *goquery.Selection) {
var str string var str string
if len(c.Attr) > 0 { if len(l.Attr) > 0 {
str, _ = sel.Attr(c.Attr) str, _ = sel.Attr(l.Attr)
} else { } else {
str = sel.Text() str = sel.Text()
} }
if c.regExComp != nil { if l.regExComp != nil {
if matches := c.regExComp.FindStringSubmatch(str); len(matches) > 1 { if matches := l.regExComp.FindStringSubmatch(str); len(matches) > 1 {
str = matches[1] str = matches[1]
} else { } else {
str = "" str = ""
@ -115,8 +105,8 @@ func (c *pathConf) locateStrings(doc *goquery.Document) ([]string, error) {
} }
func (c *pathConf) locateString(doc *goquery.Document) (string, error) { func (l *locator) locateString(doc *goquery.Document) (string, error) {
strs, err := c.locateStrings(doc) strs, err := l.locateStrings(doc)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -124,8 +114,8 @@ func (c *pathConf) locateString(doc *goquery.Document) (string, error) {
return strings.Join(strs, " "), nil return strings.Join(strs, " "), nil
} }
func (c *pathConf) locateInt(doc *goquery.Document) (int64, error) { func (l *locator) locateInt(doc *goquery.Document) (int64, error) {
str, err := c.locateString(doc) str, err := l.locateString(doc)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -133,8 +123,8 @@ func (c *pathConf) locateInt(doc *goquery.Document) (int64, error) {
return strconv.ParseInt(str, 10, 8) return strconv.ParseInt(str, 10, 8)
} }
func (c *pathConf) locateFloat(doc *goquery.Document) (float64, error) { func (l *locator) locateFloat(doc *goquery.Document) (float64, error) {
str, err := c.locateString(doc) str, err := l.locateString(doc)
if err != nil { if err != nil {
return 0.0, err return 0.0, err
} }
@ -143,57 +133,50 @@ func (c *pathConf) locateFloat(doc *goquery.Document) (float64, error) {
} }
// //
// semConf // descriptor
// //
type semConf struct { type descriptor struct {
semantics
pathConf
Scale float64
}
//
// descConf
//
type descConf struct {
Index struct { Index struct {
Items pathConf Items locator
Next pathConf Next locator
} }
Item struct { Item struct {
Name pathConf Name locator
Address pathConf Address locator
Count pathConf Count locator
Sem map[string]semConf Props map[string]struct {
semantics
locator
Scale float64
}
} }
} }
func newDescriptor(filename string, gc *geoCache, wc *webCache) (*descriptor, error) { func newDescriptor(filename string) (*descriptor, error) {
desc := &descriptor{gc: gc, wc: wc}
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := toml.Unmarshal(bytes, &desc.conf); err != nil { var desc descriptor
if err := toml.Unmarshal(bytes, &desc); err != nil {
return nil, err return nil, err
} }
return desc, nil return &desc, nil
} }
func (d descriptor) define(keyword string) semantics { func (d descriptor) define(keyword string) semantics {
return d.conf.Item.Sem[keyword].semantics return d.Item.Props[keyword].semantics
} }
func (d descriptor) index(doc *goquery.Document) (next string, items []string, err error) { func (d descriptor) index(doc *goquery.Document) (next string, items []string, err error) {
if items, err = d.conf.Index.Items.locateStrings(doc); err != nil { if items, err = d.Index.Items.locateStrings(doc); err != nil {
return return
} }
if next, err = d.conf.Index.Next.locateString(doc); err != nil { if next, err = d.Index.Next.locateString(doc); err != nil {
return return
} }
@ -201,31 +184,31 @@ func (d descriptor) index(doc *goquery.Document) (next string, items []string, e
} }
func (d descriptor) review(doc *goquery.Document) (name, address string, features map[string]float64, count int64, err error) { func (d descriptor) review(doc *goquery.Document) (name, address string, features map[string]float64, count int64, err error) {
if name, err = d.conf.Item.Name.locateString(doc); err != nil || len(name) == 0 { if name, err = d.Item.Name.locateString(doc); err != nil || len(name) == 0 {
err = errors.New("invalid name") err = errors.New("invalid name")
return return
} }
if address, err = d.conf.Item.Address.locateString(doc); err != nil || len(address) == 0 { if address, err = d.Item.Address.locateString(doc); err != nil || len(address) == 0 {
err = errors.New("invalid address") err = errors.New("invalid address")
return return
} }
if count, err = d.conf.Item.Count.locateInt(doc); err != nil { if count, err = d.Item.Count.locateInt(doc); err != nil {
err = errors.New("invalid review count") err = errors.New("invalid review count")
return return
} }
features = make(map[string]float64) features = make(map[string]float64)
for n, s := range d.conf.Item.Sem { for n, p := range d.Item.Props {
var value float64 var value float64
if value, err = s.pathConf.locateFloat(doc); err != nil { if value, err = p.locateFloat(doc); err != nil {
err = fmt.Errorf("invalid feature value for %s", n) err = fmt.Errorf("invalid feature value for %s", n)
return return
} }
if s.Scale != 0.0 { if p.Scale != 0.0 {
value /= s.Scale value /= p.Scale
} }
features[n] = value features[n] = value