Last active
April 15, 2021 16:09
-
-
Save baldwindavid/e4bf6fda3cc5ceab8cb697f9c7316a29 to your computer and use it in GitHub Desktop.
You'll need an API key from words.bighugelabs.com
This file contains hidden or 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
require 'rubygems' | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
require 'cgi' | |
# Get a free API key at https://words.bighugelabs.com/account/getkey | |
API_KEY = '[YOUR API KEY HERE]' | |
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