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
|
# File 'lib/definer.rb', line 6
def define word
begin
definition = part_of_speech = example = "not found"
api_url = "http://api.pearson.com/v2/dictionaries/entries?headword="
= URI(api_url.concat(word))
res = Net::HTTP.get();
my_hash = JSON.parse(res)
accepted = ['ldoce5','laad3','wordwise']
my_dict = my_hash['results'].select { |set| (set['datasets'] & accepted).any?}[0]
part_of_speech = my_dict['part_of_speech'] unless part_of_speech.class== NilClass
definition = my_dict['senses'][0]['definition']
example = my_dict['senses'][0]['examples'][0]['text'] unless example.class == NilClass
rescue
end
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: define <word> [options]"
options[:example] = false
opts.on( '-e', '--example', 'Shows an example') do
options[:example] = true
end
options[:part_of_speech] = false
opts.on( '-p', '--part', 'Shows the part of speech') do
options[:part_of_speech] = true
end
opts.on( '-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
puts "#{word.upcase} : #{definition}" if definition
puts "Part of Speech : #{part_of_speech}" if options[:part_of_speech]
puts "Example : #{example}" if options[:example]
end
|