-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththesaurus.go
94 lines (75 loc) · 2.36 KB
/
thesaurus.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
package main
// @hipbot thesaurus me <word-query>
// Get all synonyms from bighugelabs.com's API
// return an HTML list of synonyms or else text no-results response
import (
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"strings"
)
const BIG_HUGE_LABS_ENDPOINT = "http://words.bighugelabs.com/api/2/"
var bigHugeLabsApiKey = os.Getenv("BIG_HUGE_LABS_API_KEY")
type SynonymResult struct {
NounList WordList `json:"noun"`
VerbList WordList `json:"verb"`
AdjectiveList WordList `json:"adjective"`
AdverbList WordList `json:"adverb"`
}
type WordList struct {
Synonyms []string `json:"syn"`
}
// Get synonyms for <query> via bighugelabs's API
// Return pretty HTML formatted response of synonyms for all
// versions of the <query> word (ie. noun, adjective, adverb, verb, etc)
func synonyms(query string) string {
// Set query url with params - ask for JSON response
queryUrl := BIG_HUGE_LABS_ENDPOINT + bigHugeLabsApiKey + "/" + url.QueryEscape(query) + "/json"
// Send GET request, collect response
resp, err := http.Get(queryUrl)
if err != nil {
log.Println(err)
return "error"
}
defer resp.Body.Close()
// Decode JSON response
decoder := json.NewDecoder(resp.Body)
results := new(SynonymResult)
decoder.Decode(results)
if err != nil {
log.Println(err)
return "error"
}
// Return nicely formatted HTML synonym results
return formattedSynonyms(*results)
}
func formattedSynonyms(results SynonymResult) string {
html := ""
// We only use the noun, verb, adverb, and adjective synonyms
html += words(results.NounList.Synonyms, "Noun")
html += words(results.VerbList.Synonyms, "Verb")
html += words(results.AdverbList.Synonyms, "Adverb")
html += words(results.AdjectiveList.Synonyms, "Adjective")
if html == "" {
html += "I found nothing! So sorry."
}
// Get rid of the line breaks below the last entry
return strings.TrimSuffix(html, "<br><br>")
}
// Return the word kind in bold, followed by a comma separated
// list of the corresponding synonyms
// Insert two line breaks at the bottom of each section for aesthetics
func words(synonyms []string, kind string) string {
html := ""
if len(synonyms) != 0 {
// Bold title (kind of word)
html += "<strong>" + kind + ":</strong><br>"
// Comma separated list of synonyms
html += strings.Join(synonyms, ", ")
// Double line break after list
html += "<br><br>"
}
return html
}