-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.go
159 lines (141 loc) · 3.47 KB
/
inference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package pfftdb
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
log "github.com/golang/glog"
)
// Inferences is initialized on load
var Inferences map[string]Inference
// Inference [...]
type Inference interface {
Apply(*Graph)
Triples(map[string]interface{}) ([]*Triple, error)
//Remove()
}
// GoogleGeoResp from the google maps api.
type GoogleGeoResp struct {
Results []struct {
AddrComponents []struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
} `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
LocationType string `json:"location_type"`
ViewPort interface{}
} `json:"geometry"`
} `json:"results"`
Status string
}
// GeoRule provides latitude and logitude triples
// for a given address. It searches for location:address within triples
// and applies lat and lng triples to that address.
type GeoRule struct {
}
func init() {
Inferences = map[string]Inference{
"geo": GeoRule{},
}
}
// googleCall performs a remote request to google.
var googleCall = func(address string) ([]byte, error) {
resp, err := http.Get(fmt.Sprintf(`http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false`, url.QueryEscape(address)))
if err != nil {
log.Error(err)
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return nil, err
}
return body, err
}
// Queries
func (gr GeoRule) Apply(g *Graph) {
type Key struct {
Place string
Address string
}
seen := map[Key]struct{}{}
bindings := []Bindings{}
triples := []*Triple{
&Triple{"?placeid", "location:address", "?address"},
}
for _, loc := range g.Query(triples, nil) {
placeID := fmt.Sprintf("%v", loc["placeid"])
addr := fmt.Sprintf("%v", loc["address"])
if _, ok := seen[Key{placeID, addr}]; ok {
continue
}
bindings = append(bindings, loc)
seen[Key{placeID, addr}] = struct{}{}
}
for _, bind := range bindings {
trps, err := gr.Triples(bind)
if err != nil {
log.Error(err)
if err.Error() == "OVER_QUERY_LIMIT" {
return
}
}
for _, triple := range trps {
sub, pred, err := SubPred(triple[0], triple[1])
if err != nil {
log.Error(err)
continue
}
err = g.Add(sub, pred, triple[2])
if err != nil {
log.Error(err)
if err.Error() == "OVER_QUERY_LIMIT" {
}
}
}
}
}
// Triples
func (m GeoRule) Triples(args map[string]interface{}) ([]*Triple, error) {
triples := []*Triple{}
if _, ok := args["placeid"].(string); ok {
addr, ok := args["address"].(string)
if !ok {
return nil, fmt.Errorf("missing address")
}
time.Sleep(500 * time.Millisecond)
body, err := googleCall(addr)
if err != nil {
log.Error(err, body)
return nil, err
}
ggr := GoogleGeoResp{}
err = json.Unmarshal(body, &ggr)
if err != nil {
log.Error(err)
return nil, err
}
if ggr.Status == "OVER_QUERY_LIMIT" {
log.Errorf("%#v", ggr)
return nil, fmt.Errorf(ggr.Status)
}
if len(ggr.Results) == 0 {
log.Errorf("%#v", ggr)
return nil, fmt.Errorf("no results")
}
lat := ggr.Results[0].Geometry.Location.Lat
lng := ggr.Results[0].Geometry.Location.Lng
triples = append(triples, &Triple{addr, "location:lat", lat})
triples = append(triples, &Triple{addr, "location:lng", lng})
}
return triples, nil
}