/
knowledgeGraph.go
88 lines (72 loc) · 1.99 KB
/
knowledgeGraph.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
package googlenl
import (
"encoding/json"
)
type SearchResult struct {
Type string `json:"@type"`
Result SearchResultEntity `json:"result"`
ResultScore float64 `json:"resultScore"`
}
type SearchResultEntity struct {
Id string `json:"@id"`
Type []string `json:"@type"`
Name string `json:"name"`
Image EntityImage `json:"image"`
DetailedDescription DetailedDescription `json:"detailedDescription"`
}
type EntityImage struct {
ContentUrl string `json:"contentUrl"`
Url string `json:"url"`
}
type DetailedDescription struct {
ArticleBody string `json:"articleBody"`
License string `json:"license"`
Url string `json:"url"`
}
type KnowledgeGraphSearchResponse struct {
ItemListElement []*SearchResult `json:"itemListElement,omitempty"`
}
func SearchKnowledgeGraph(query string) (*KnowledgeGraphSearchResponse, error) {
req := KnowledgeGraphClient.Entities.Search()
req.Query(query)
r, err := req.Do()
if err != nil {
return nil, err
}
resp := &KnowledgeGraphSearchResponse{}
for i := 0; i < len(r.ItemListElement); i++ {
b, err := json.Marshal(r.ItemListElement[i])
if err != nil {
return nil, err
}
sr := &SearchResult{}
err = json.Unmarshal(b, sr)
if err != nil {
return nil, err
}
resp.ItemListElement = append(resp.ItemListElement, sr)
}
return resp, nil
}
func SearchKnowledgeGraphByIds(ids ...string) (*KnowledgeGraphSearchResponse, error) {
req := KnowledgeGraphClient.Entities.Search()
req.Ids(ids...)
r, err := req.Do()
if err != nil {
return nil, err
}
resp := &KnowledgeGraphSearchResponse{}
for i := 0; i < len(r.ItemListElement); i++ {
b, err := json.Marshal(r.ItemListElement[i])
if err != nil {
return nil, err
}
sr := &SearchResult{}
err = json.Unmarshal(b, sr)
if err != nil {
return nil, err
}
resp.ItemListElement = append(resp.ItemListElement, sr)
}
return resp, nil
}