github.com/wit-ai/wit-go/v2@v2.0.2/entity.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "fmt" 9 "net/http" 10 "net/url" 11 ) 12 13 // Entity represents a wit-ai Entity. 14 // 15 // https://wit.ai/docs/http/20200513/#post__entities_link 16 type Entity struct { 17 ID string `json:"id"` 18 Name string `json:"name"` 19 Lookups []string `json:"lookups,omitempty"` 20 Roles []string `json:"roles,omitempty"` 21 Keywords []EntityKeyword `json:"keywords,omitempty"` 22 } 23 24 // EntityKeyword is a keyword lookup for an Entity. 25 // 26 // https://wit.ai/docs/http/20200513/#post__entities__entity_keywords_link 27 type EntityKeyword struct { 28 Keyword string `json:"keyword"` 29 Synonyms []string `json:"synonyms"` 30 } 31 32 // GetEntities - returns list of entities. 33 // 34 // https://wit.ai/docs/http/20200513/#get__entities_link 35 func (c *Client) GetEntities() ([]Entity, error) { 36 resp, err := c.request(http.MethodGet, "/entities", "application/json", nil) 37 if err != nil { 38 return []Entity{}, err 39 } 40 41 defer resp.Close() 42 43 var entities []Entity 44 decoder := json.NewDecoder(resp) 45 err = decoder.Decode(&entities) 46 return entities, err 47 } 48 49 // CreateEntity - Creates a new entity with the given attributes 50 // 51 // https://wit.ai/docs/http/20200513/#post__entities_link 52 func (c *Client) CreateEntity(entity Entity) (*Entity, error) { 53 entityJSON, err := json.Marshal(entity) 54 if err != nil { 55 return nil, err 56 } 57 58 resp, err := c.request(http.MethodPost, "/entities", "application/json", bytes.NewBuffer(entityJSON)) 59 if err != nil { 60 return nil, err 61 } 62 63 defer resp.Close() 64 65 var entityResp *Entity 66 decoder := json.NewDecoder(resp) 67 err = decoder.Decode(&entityResp) 68 return entityResp, err 69 } 70 71 // GetEntity - returns entity by ID or name. 72 // 73 // https://wit.ai/docs/http/20200513/#get__entities__entity_link 74 func (c *Client) GetEntity(entityID string) (*Entity, error) { 75 resp, err := c.request(http.MethodGet, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", nil) 76 if err != nil { 77 return nil, err 78 } 79 80 defer resp.Close() 81 82 var entity *Entity 83 decoder := json.NewDecoder(resp) 84 err = decoder.Decode(&entity) 85 return entity, err 86 } 87 88 // UpdateEntity - Updates an entity by ID or name. 89 // 90 // https://wit.ai/docs/http/20200513/#put__entities__entity_link 91 func (c *Client) UpdateEntity(entityID string, entity Entity) error { 92 entityJSON, err := json.Marshal(entity) 93 if err != nil { 94 return err 95 } 96 97 resp, err := c.request(http.MethodPut, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", bytes.NewBuffer(entityJSON)) 98 if err != nil { 99 return err 100 } 101 102 defer resp.Close() 103 104 decoder := json.NewDecoder(resp) 105 return decoder.Decode(&entity) 106 } 107 108 // DeleteEntity - deletes entity by ID or name. 109 // 110 // https://wit.ai/docs/http/20200513/#delete__entities__entity_link 111 func (c *Client) DeleteEntity(entityID string) error { 112 resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", nil) 113 if err == nil { 114 resp.Close() 115 } 116 117 return err 118 } 119 120 // DeleteEntityRole - deletes entity role. 121 // 122 // https://wit.ai/docs/http/20200513/#delete__entities__entity_role_link 123 func (c *Client) DeleteEntityRole(entityID string, role string) error { 124 resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s:%s", url.PathEscape(entityID), url.PathEscape(role)), "application/json", nil) 125 if err == nil { 126 resp.Close() 127 } 128 129 return err 130 } 131 132 // AddEntityKeyword - Add a possible value into the list of values for the keyword entity. 133 // 134 // https://wit.ai/docs/http/20200513/#post__entities__entity_keywords_link 135 func (c *Client) AddEntityKeyword(entityID string, keyword EntityKeyword) (*Entity, error) { 136 valueJSON, err := json.Marshal(keyword) 137 if err != nil { 138 return nil, err 139 } 140 141 resp, err := c.request(http.MethodPost, fmt.Sprintf("/entities/%s/keywords", url.PathEscape(entityID)), "application/json", bytes.NewBuffer(valueJSON)) 142 if err != nil { 143 return nil, err 144 } 145 146 defer resp.Close() 147 148 var entityResp *Entity 149 decoder := json.NewDecoder(resp) 150 if err = decoder.Decode(&entityResp); err != nil { 151 return nil, err 152 } 153 154 return entityResp, nil 155 } 156 157 // DeleteEntityKeyword - Delete a keyword from the keywords entity. 158 // 159 // https://wit.ai/docs/http/20200513/#delete__entities__entity_keywords__keyword_link 160 func (c *Client) DeleteEntityKeyword(entityID string, keyword string) error { 161 resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/keywords/%s", url.PathEscape(entityID), url.PathEscape(keyword)), "application/json", nil) 162 if err == nil { 163 resp.Close() 164 } 165 166 return err 167 } 168 169 // AddEntityKeywordSynonym - Create a new synonym of the canonical value of the keywords entity. 170 // 171 // https://wit.ai/docs/http/20200513/#post__entities__entity_keywords__keyword_synonyms_link 172 func (c *Client) AddEntityKeywordSynonym(entityID string, keyword string, synonym string) (*Entity, error) { 173 type syn struct { 174 Synonym string `json:"synonym"` 175 } 176 177 exprJSON, err := json.Marshal(syn{ 178 Synonym: synonym, 179 }) 180 if err != nil { 181 return nil, err 182 } 183 184 resp, err := c.request(http.MethodPost, fmt.Sprintf("/entities/%s/keywords/%s/synonyms", url.PathEscape(entityID), url.PathEscape(keyword)), "application/json", bytes.NewBuffer(exprJSON)) 185 if err != nil { 186 return nil, err 187 } 188 189 defer resp.Close() 190 191 var entityResp *Entity 192 decoder := json.NewDecoder(resp) 193 if err = decoder.Decode(&entityResp); err != nil { 194 return nil, err 195 } 196 197 return entityResp, nil 198 } 199 200 // DeleteEntityKeywordSynonym - Delete a synonym of the keyword of the entity. 201 // 202 // https://wit.ai/docs/http/20200513/#delete__entities__entity_keywords__keyword_synonyms__synonym_link 203 func (c *Client) DeleteEntityKeywordSynonym(entityID string, keyword string, expression string) error { 204 resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/keywords/%s/synonyms/%s", url.PathEscape(entityID), url.PathEscape(keyword), url.PathEscape(expression)), "application/json", nil) 205 if err == nil { 206 resp.Close() 207 } 208 209 return err 210 }