Skip to content

Instantly share code, notes, and snippets.

@bontusss
Created December 28, 2022 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bontusss/96f8785f4e2c4ec55c455872fac89cac to your computer and use it in GitHub Desktop.
Save bontusss/96f8785f4e2c4ec55c455872fac89cac to your computer and use it in GitHub Desktop.
thesaurus
package main
import (
"encoding/json"
"errors"
"net/http"
)
type BigHuge struct {
APIKEY string
}
type synonyms struct {
Noun *words `json:"noun"`
Verb *words `json:"verb"`
}
type words struct {
Syn []string `json:"syn"`
}
func (b *BigHuge) Synonymns(term string) ([]string, error) {
var syns []string
response, err := http.Get("https://words.bighugelabs.com/api/2/" + b.APIKEY + "/" + term + "/json")
if err != nil {
return syns, errors.New("bighuge: Failed when looking for synonyms for \"" + term + "\"" + err.Error())
}
var data synonyms
defer response.Body.Close()
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
return syns, err
}
if data.Noun != nil {
syns = append(syns, data.Noun.Syn...)
}
if data.Verb != nil {
syns = append(syns, data.Verb.Syn...)
}
return syns, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment