Skip to content

Instantly share code, notes, and snippets.

@mcls
Created January 8, 2014 20:49
Show Gist options
  • Save mcls/8324389 to your computer and use it in GitHub Desktop.
Save mcls/8324389 to your computer and use it in GitHub Desktop.
Searches for synonyms online
package main
import (
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/codegangsta/cli"
)
func findSynonyms(query string) string {
url := "http://thesaurus.com/browse/" + query
doc, e := goquery.NewDocument(url)
if e != nil {
panic(e.Error())
}
var synonyms string = ""
doc.Find(".synonyms span.text").Each(func(i int, s *goquery.Selection) {
synonyms += s.Text() + "\n"
})
return synonyms
}
func main() {
app := cli.NewApp()
app.Name = "syn"
app.Usage = "search for synonyms online"
app.Action = func(c *cli.Context) {
var query string
if len(c.Args()) > 0 {
query = strings.Join(c.Args(), " ")
println(findSynonyms(query))
} else {
println("Please specify a query to search synonyms for")
}
}
app.Run(os.Args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment