-
Notifications
You must be signed in to change notification settings - Fork 8
/
thesaurus.rb
60 lines (54 loc) · 1.63 KB
/
thesaurus.rb
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
# From the great David Baldwin (github.com/baldwindavid)
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'cgi'
API_KEY = '822c0fc517a5d2219c9e5a7389fe5648'
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