Skip to content

Instantly share code, notes, and snippets.

@baldwindavid
Created April 10, 2012 19:42
Show Gist options
  • Save baldwindavid/2353958 to your computer and use it in GitHub Desktop.
Save baldwindavid/2353958 to your computer and use it in GitHub Desktop.
Simple script to query thesaurus entries - Get an api key and then point to the file via bash_profile (e.g. alias thes='ruby ~/whateverpath/thes.rb)
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'cgi'
# 1) Get an api key - http://words.bighugelabs.com/api.php
# 2) Point to the file via bash_profile (e.g. alias thes='ruby ~/whateverpath/thes.rb)
# 3) From command line type... thes dog or thes cool
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # get an api key - http://words.bighugelabs.com/api.php
API_VERSION = 2
WORDS_PER_LINE = 4
RELATIONSHIP_TYPES = {
'syn' => 'synonyms',
'ant' => 'antonyms',
'rel' => 'related',
'sim' => 'similar',
'usr' => 'user suggested'
}
# Allow multi-word searches without quotes. Yay.
search_term = ARGV.join(' ')
# URL encode command line string
search_term_url = CGI::escape(search_term)
uri = URI.parse("http://words.bighugelabs.com/api/#{API_VERSION}/#{API_KEY}/#{search_term_url}/json")
response = Net::HTTP.get(uri)
if !response.empty?
puts "\n--------------------------------------------------"
puts ' Thesaurus entries for "' + search_term + "\"\n"
puts "--------------------------------------------------\n\n"
parts = JSON.parse(response) if response
parts.each do |part, relationships|
puts " " + part + 's'
print " "
(part.size + 1).times {print '-'}
puts "\n"
RELATIONSHIP_TYPES.sort.reverse.each do |abbrev, title|
if relationships[abbrev]
words = relationships[abbrev].sort
puts " #{title}:"
i = 1
words.each do |word|
print " " if i == 1
print word
print ', ' unless i == words.size
print "\n " if (i % WORDS_PER_LINE == 0) && (i != words.size)
i+=1
end
puts "\n\n"
end
end
end
puts "\n"
else
puts "\n--------------------------------------------------"
puts ' No entries for "' + search_term + '"'
puts "--------------------------------------------------\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment