Created
January 8, 2014 20:49
-
-
Save mcls/8324389 to your computer and use it in GitHub Desktop.
Searches for synonyms online
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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