dict 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +30 -0
- data/README.md +2 -0
- data/Rakefile +8 -0
- data/bin/dict +3 -2
- data/dict.gemspec +25 -0
- data/lib/dict/cli/runner.rb +57 -0
- data/lib/dict/dict.rb +49 -0
- data/lib/dict/dictionary.rb +42 -0
- data/lib/dict/dictpl.rb +21 -0
- data/lib/dict/version.rb +3 -0
- data/lib/dict/wiktionary.rb +28 -0
- data/lib/dict.rb +2 -46
- data/spec/dict/bin_translate_spec.rb +50 -0
- data/spec/dict/lib_dict_spec.rb +21 -0
- data/spec/dict/lib_dictpl_spec.rb +33 -0
- data/spec/dict/lib_wiktionary_spec.rb +34 -0
- data/spec/dict/spec_helper.rb +17 -0
- metadata +21 -6
- data/lib/dictionary.rb +0 -40
- data/lib/dictpl.rb +0 -19
- data/lib/module_main.rb +0 -54
- data/lib/wiktionary.rb +0 -26
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 1.9.2
|
5
|
+
notifications:
|
6
|
+
email:
|
7
|
+
- gozdek.aleksander@gmail.com
|
8
|
+
- grk@ragnarson.com
|
9
|
+
- jan.borwin@gmail.com
|
10
|
+
- kosmadunikowski@gmail.com
|
11
|
+
- mtczerwinski@gmail.com
|
12
|
+
- michalk@ragnarson.com
|
13
|
+
- m.podlecki@op.pl
|
14
|
+
- rafal.osko@gmail.com
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dict (0.2.1)
|
5
|
+
nokogiri (~> 1.5.5)
|
6
|
+
slop (~> 3.3.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.1.3)
|
12
|
+
nokogiri (1.5.5)
|
13
|
+
rake (0.9.2.2)
|
14
|
+
rspec (2.11.0)
|
15
|
+
rspec-core (~> 2.11.0)
|
16
|
+
rspec-expectations (~> 2.11.0)
|
17
|
+
rspec-mocks (~> 2.11.0)
|
18
|
+
rspec-core (2.11.0)
|
19
|
+
rspec-expectations (2.11.1)
|
20
|
+
diff-lcs (~> 1.1.3)
|
21
|
+
rspec-mocks (2.11.1)
|
22
|
+
slop (3.3.2)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
dict!
|
29
|
+
rake
|
30
|
+
rspec (~> 2.11)
|
data/README.md
ADDED
data/Rakefile
ADDED
data/bin/dict
CHANGED
data/dict.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "dict/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.add_dependency 'slop', '~> 3.3.2'
|
6
|
+
s.add_dependency 'nokogiri', '~>1.5.5'
|
7
|
+
s.add_development_dependency "rspec", "~> 2.11"
|
8
|
+
s.add_development_dependency "rake"
|
9
|
+
|
10
|
+
s.name = %q{dict}
|
11
|
+
s.version = Dict::VERSION
|
12
|
+
s.authors = ['Aleksander Gozdek', 'Mateusz Czerwinski', 'Michał Podlecki','Rafał Ośko']
|
13
|
+
s.email = ['mtczerwinski@gmail.com']
|
14
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
15
|
+
s.summary = %q{Gem made for dictionary application}
|
16
|
+
s.description = <<-END
|
17
|
+
Dict is an open source dictionary aggregator.
|
18
|
+
END
|
19
|
+
s.homepage = 'https://github.com/Ragnarson/dict-gem'
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'dict/dict'
|
4
|
+
require 'slop'
|
5
|
+
require 'timeout'
|
6
|
+
|
7
|
+
module Dict
|
8
|
+
module CLI
|
9
|
+
class Runner
|
10
|
+
def parameters_valid?
|
11
|
+
not ARGV.empty?
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_parameters
|
15
|
+
available_dictionaries = Dict.available_services.join(', ')
|
16
|
+
|
17
|
+
opts = Slop.parse! do
|
18
|
+
banner <<-END
|
19
|
+
Usage: dict WORD [OPTIONS]
|
20
|
+
Search WORD in dict, an open source dictionary aggregator.
|
21
|
+
END
|
22
|
+
|
23
|
+
on '-h', :help, 'Display this help message'
|
24
|
+
on '-t', :time=, 'Set timeout in seconds. Default: 300', :as => :int
|
25
|
+
on '-d', :dict=, "Select desired dictionary. Available options: #{available_dictionaries}"
|
26
|
+
end
|
27
|
+
opts
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_translations(opts, word)
|
31
|
+
Timeout::timeout(opts[:time].to_i || 300) do
|
32
|
+
if opts.dict?
|
33
|
+
Dict.get_single_dictionary_translations(word, opts[:dict])
|
34
|
+
else
|
35
|
+
Dict.get_all_dictionaries_translations(word)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
rescue Timeout::Error
|
39
|
+
"Timeout for the query."
|
40
|
+
end
|
41
|
+
|
42
|
+
def run(opts)
|
43
|
+
begin
|
44
|
+
opts = parse_parameters
|
45
|
+
rescue Slop::MissingArgumentError
|
46
|
+
abort("Missing argument")
|
47
|
+
end
|
48
|
+
|
49
|
+
abort(opts.to_s) if opts.help?
|
50
|
+
parameters_valid? or abort("Please enter a word. (-h for help)")
|
51
|
+
|
52
|
+
puts get_translations(opts, ARGV[0])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/lib/dict/dict.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'dict/wiktionary'
|
4
|
+
require 'dict/dictpl'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module Dict
|
8
|
+
class << self
|
9
|
+
def get_all_dictionaries_translations(word)
|
10
|
+
dictionaries = Hash.new
|
11
|
+
|
12
|
+
available_services.each do |service|
|
13
|
+
dictionaries[service] = get_single_dictionary_translations(word, service)
|
14
|
+
end
|
15
|
+
dictionaries
|
16
|
+
end
|
17
|
+
|
18
|
+
def print_all_dictionaries_translations(word)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_single_dictionary_translations(word, service)
|
22
|
+
case service
|
23
|
+
when 'wiktionary'
|
24
|
+
Wiktionary.new(word, WIKI_URL).translate
|
25
|
+
when 'dictpl'
|
26
|
+
Dictpl.new(word, DICT_URL).translate
|
27
|
+
else Dictionary.message
|
28
|
+
end
|
29
|
+
rescue Dictionary::ConnectError
|
30
|
+
"Couldn't connect to the service."
|
31
|
+
end
|
32
|
+
|
33
|
+
def print_single_dictionary_translations(word, service)
|
34
|
+
obj = get_single_dictionary_translations(word, service)
|
35
|
+
hash = obj.translate
|
36
|
+
hash.each do |k, v|
|
37
|
+
puts "#{k} - #{v}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_json(hash)
|
42
|
+
hash.to_json
|
43
|
+
end
|
44
|
+
|
45
|
+
def available_services
|
46
|
+
['wiktionary', 'dictpl']
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
module Dict
|
6
|
+
class Dictionary
|
7
|
+
attr_accessor :translations, :examples
|
8
|
+
def initialize(word, url)
|
9
|
+
check_arguments(word)
|
10
|
+
@translations = []
|
11
|
+
@examples = []
|
12
|
+
@uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_hash_results(arr)
|
16
|
+
hash = arr.each_slice(2).inject({}) do |h, (key, value)|
|
17
|
+
if h.has_key?(key)
|
18
|
+
h[key].push(value) ; h
|
19
|
+
else
|
20
|
+
h[key] = [value] ; h
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@translations, @examples = hash.keys, hash.values
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def check_arguments(word)
|
28
|
+
raise ArgumentError.new("No given word") if word.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.message
|
32
|
+
'There\'s no such dictionary in database.'
|
33
|
+
end
|
34
|
+
|
35
|
+
class ConnectError < Exception
|
36
|
+
attr_reader :original
|
37
|
+
def initialize(original = $!)
|
38
|
+
@original = original
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/dict/dictpl.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'dict/dictionary'
|
5
|
+
|
6
|
+
DICT_URL = 'http://dict.pl/dict?word='
|
7
|
+
|
8
|
+
module Dict
|
9
|
+
class Dictpl < Dictionary
|
10
|
+
# Method returns hash with translations as keys and examples of using words as values
|
11
|
+
def translate
|
12
|
+
context_words = []
|
13
|
+
Nokogiri::HTML(open(@uri)).xpath('//td[@class="resWordCol"]/a').each do |node|
|
14
|
+
context_words << node.text
|
15
|
+
end
|
16
|
+
make_hash_results(context_words)
|
17
|
+
rescue OpenURI::HTTPError
|
18
|
+
raise Dictionary::ConnectError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/dict/version.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'dict/dictionary'
|
5
|
+
|
6
|
+
WIKI_URL = 'http://en.wiktionary.org/wiki/'
|
7
|
+
|
8
|
+
module Dict
|
9
|
+
class Wiktionary < Dictionary
|
10
|
+
# Method returns hash with translations as keys and examples of using words as values
|
11
|
+
def translate
|
12
|
+
context_words = []
|
13
|
+
url = 'http://en.wiktionary.org/wiki/'
|
14
|
+
get_html(@uri).css('p + ol li a').each do |node|
|
15
|
+
get_html(url + node.text.tr(' ', '_')).css('p + ol > li dl dd').each do |example|
|
16
|
+
context_words << node.text << example.text
|
17
|
+
end
|
18
|
+
end
|
19
|
+
make_hash_results(context_words)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_html(url)
|
23
|
+
Nokogiri::HTML(open(url))
|
24
|
+
rescue OpenURI::HTTPError
|
25
|
+
raise Dictionary::ConnectError
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/dict.rb
CHANGED
@@ -1,49 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative 'wiktionary'
|
4
|
-
require_relative 'dictpl'
|
5
|
-
require 'json'
|
1
|
+
require 'dict/dict'
|
6
2
|
|
7
3
|
module Dict
|
8
|
-
|
9
|
-
def get_all_dictionaries_translations(word)
|
10
|
-
dictionaries = Hash.new
|
11
|
-
|
12
|
-
available_services.each do |service|
|
13
|
-
dictionaries[service] = get_single_dictionary_translations(word, service)
|
14
|
-
end
|
15
|
-
dictionaries
|
16
|
-
end
|
17
|
-
|
18
|
-
def print_all_dictionaries_translations(word)
|
19
|
-
end
|
20
|
-
|
21
|
-
def get_single_dictionary_translations(word, service)
|
22
|
-
case service
|
23
|
-
when 'wiktionary'
|
24
|
-
Wiktionary.new(word, WIKI_URL).translate
|
25
|
-
when 'dictpl'
|
26
|
-
Dictpl.new(word, DICT_URL).translate
|
27
|
-
else Dictionary.message
|
28
|
-
end
|
29
|
-
rescue Dictionary::ConnectError
|
30
|
-
"Couldn't connect to the service."
|
31
|
-
end
|
32
|
-
|
33
|
-
def print_single_dictionary_translations(word, service)
|
34
|
-
obj = get_single_dictionary_translations(word, service)
|
35
|
-
hash = obj.translate
|
36
|
-
hash.each do |k, v|
|
37
|
-
puts "#{k} - #{v}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_json(hash)
|
42
|
-
hash.to_json
|
43
|
-
end
|
44
|
-
|
45
|
-
def available_services
|
46
|
-
['wiktionary', 'dictpl']
|
47
|
-
end
|
48
|
-
end
|
4
|
+
autoload :VERSION, "dict/version"
|
49
5
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- coding: utf-8 -*
|
2
|
+
require 'dict/cli/runner'
|
3
|
+
|
4
|
+
describe "parameters_valid?" do
|
5
|
+
it "should return false if ARGV is empty" do
|
6
|
+
stub_const("ARGV", [])
|
7
|
+
runner = Dict::CLI::Runner.new
|
8
|
+
runner.parameters_valid?.should == false
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return true if ARGV is not empty" do
|
12
|
+
stub_const("ARGV", ["słowik", "-t", "36", "-d"])
|
13
|
+
runner = Dict::CLI::Runner.new
|
14
|
+
runner.parameters_valid?.should == true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "parse_parameters" do
|
19
|
+
it "should return Hash for parameters słowik -t 36" do
|
20
|
+
stub_const("ARGV", ["słowik", "-t", "36"])
|
21
|
+
runner = Dict::CLI::Runner.new
|
22
|
+
opts = runner.parse_parameters
|
23
|
+
{:help=>nil, :time=>"36", :dict=>nil}.should == opts.to_hash
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return Hash for parameters słowik" do
|
27
|
+
stub_const("ARGV", ["słowik"])
|
28
|
+
runner = Dict::CLI::Runner.new
|
29
|
+
opts = runner.parse_parameters
|
30
|
+
{:help=>nil, :time=>nil, :dict=>nil}.should == opts.to_hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
describe "get_translations" do
|
36
|
+
# it "should return results from wiktionary and dictpl for word 'słowik'" do
|
37
|
+
# stub_const("ARGV", ["-w", "słowik"])
|
38
|
+
# opts = Main::parse_parameters
|
39
|
+
# Main::get_translations(opts).should == {"wiktionary"=>{}, "dictpl"=>{"słowik"=>["nightingale"], "słowik białobrewy; Luscinia indicus; Tarsiger indicus (gatunek ptaka)"=>["white-browed bush-robin"], "słowik białosterny; Luscinia pectoralis (gatunek ptaka)"=>["Himalayan rubythroat", "white-tailed rubythroat"], "słowik chiński; pekińczyk żółty; Leiothrix lutea"=>["Pekin robin", "red-billed leiothrix"], "słowik chiński; pekińczyk żółty; pekińczyk koralodzioby; Leiothrix lutea"=>["Peking robin"], "słowik czarnogardły; Luscinia obscura"=>["black-throated blue robin"], "słowik himalajski; Luscinia brunnea (gatunek ptaka)"=>["Indian blue chat", "Indian blue robin"], "słowik modry; Luscinia cyane"=>["Siberian blue robin"], "słowik obrożny; Luscinia johnstoniae; Tarsiger johnstoniae (gatunek ptaka)"=>["collared bush-robin"]}}
|
40
|
+
# end
|
41
|
+
|
42
|
+
it "should return timeout message for word słowik and -t 5" do
|
43
|
+
stub_const("ARGV", ["słowik","-t","5"])
|
44
|
+
runner = Dict::CLI::Runner.new
|
45
|
+
opts = runner.parse_parameters
|
46
|
+
Dict.should_receive(:get_all_dictionaries_translations).
|
47
|
+
and_return { sleep 20 }
|
48
|
+
runner.get_translations(opts, "słowik").should == "Timeout for the query."
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'dict/dict'
|
4
|
+
|
5
|
+
describe Dict do
|
6
|
+
|
7
|
+
it "should get single translation from dictionary with two arguments given" do
|
8
|
+
expect{
|
9
|
+
Dict.get_single_dictionary_translations('samochód', 'dictpl')
|
10
|
+
}.to_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return hash with translations from all dictionaries" do
|
14
|
+
Dict.get_all_dictionaries_translations('samochód').should be_a(Hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return array of available services" do
|
18
|
+
Dict.available_services.should be_a(Array)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*
|
2
|
+
|
3
|
+
require 'dict/dictpl'
|
4
|
+
|
5
|
+
describe Dict::Dictpl do
|
6
|
+
|
7
|
+
it "should raise no given word exception" do
|
8
|
+
expect { Dict::Dictpl.new }.to raise_error ArgumentError
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return hash for given word: 'samochód'" do
|
12
|
+
result = Dict::Dictpl.new('samochód', DICT_URL).translate
|
13
|
+
result.should be_a(Hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return array with translations" do
|
17
|
+
d = Dict::Dictpl.new('samochód', DICT_URL)
|
18
|
+
d.translate
|
19
|
+
d.translations.should be_a(Array)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return array with examples of translated words" do
|
23
|
+
d = Dict::Dictpl.new('samochód', DICT_URL)
|
24
|
+
d.translate
|
25
|
+
d.examples.should be_a(Array)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return a hash from array of paired values" do
|
29
|
+
d = Dict::Dictpl.new('samochód', DICT_URL)
|
30
|
+
d.make_hash_results(d.translate).should be_a(Hash)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*
|
2
|
+
|
3
|
+
require 'dict/wiktionary'
|
4
|
+
|
5
|
+
describe Dict::Wiktionary do
|
6
|
+
|
7
|
+
it "should raise no given word exception" do
|
8
|
+
expect { Dict::Wiktionary.new }.to raise_error ArgumentError
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return an two element array of translations of word samochód containing [\"car\",\"automobile\"]" do
|
12
|
+
w = Dict::Wiktionary.new('samochód', WIKI_URL)
|
13
|
+
w.translate
|
14
|
+
w.translations.should == ["car", "automobile"]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return array with translations" do
|
18
|
+
w = Dict::Wiktionary.new('samochód', WIKI_URL)
|
19
|
+
w.translate
|
20
|
+
w.translations.should be_a(Array)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return array with examples of translated words" do
|
24
|
+
w = Dict::Wiktionary.new('samochód', WIKI_URL)
|
25
|
+
w.translate
|
26
|
+
w.examples.should be_a(Array)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return a hash from array of paired values" do
|
30
|
+
w = Dict::Wiktionary.new('samochód', WIKI_URL)
|
31
|
+
w.make_hash_results(w.translate).should be_a(Hash)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -88,12 +88,27 @@ executables:
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
-
|
92
|
-
-
|
93
|
-
-
|
94
|
-
-
|
95
|
-
-
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .travis.yml
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
96
98
|
- bin/dict
|
99
|
+
- dict.gemspec
|
100
|
+
- lib/dict.rb
|
101
|
+
- lib/dict/cli/runner.rb
|
102
|
+
- lib/dict/dict.rb
|
103
|
+
- lib/dict/dictionary.rb
|
104
|
+
- lib/dict/dictpl.rb
|
105
|
+
- lib/dict/version.rb
|
106
|
+
- lib/dict/wiktionary.rb
|
107
|
+
- spec/dict/bin_translate_spec.rb
|
108
|
+
- spec/dict/lib_dict_spec.rb
|
109
|
+
- spec/dict/lib_dictpl_spec.rb
|
110
|
+
- spec/dict/lib_wiktionary_spec.rb
|
111
|
+
- spec/dict/spec_helper.rb
|
97
112
|
homepage: https://github.com/Ragnarson/dict-gem
|
98
113
|
licenses: []
|
99
114
|
post_install_message:
|
data/lib/dictionary.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'open-uri'
|
4
|
-
|
5
|
-
class Dictionary
|
6
|
-
attr_accessor :translations, :examples
|
7
|
-
def initialize(word, url)
|
8
|
-
check_arguments(word)
|
9
|
-
@translations = []
|
10
|
-
@examples = []
|
11
|
-
@uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
|
12
|
-
end
|
13
|
-
|
14
|
-
def make_hash_results(arr)
|
15
|
-
hash = arr.each_slice(2).inject({}) do |h, (key, value)|
|
16
|
-
if h.has_key?(key)
|
17
|
-
h[key].push(value) ; h
|
18
|
-
else
|
19
|
-
h[key] = [value] ; h
|
20
|
-
end
|
21
|
-
end
|
22
|
-
@translations, @examples = hash.keys, hash.values
|
23
|
-
hash
|
24
|
-
end
|
25
|
-
|
26
|
-
def check_arguments(word)
|
27
|
-
raise ArgumentError.new("No given word") if word.empty?
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.message
|
31
|
-
'There\'s no such dictionary in database.'
|
32
|
-
end
|
33
|
-
|
34
|
-
class ConnectError < Exception
|
35
|
-
attr_reader :original
|
36
|
-
def initialize(original = $!)
|
37
|
-
@original = original
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
data/lib/dictpl.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'nokogiri'
|
4
|
-
require_relative 'dictionary'
|
5
|
-
|
6
|
-
DICT_URL = 'http://dict.pl/dict?word='
|
7
|
-
|
8
|
-
class Dictpl < Dictionary
|
9
|
-
# Method returns hash with translations as keys and examples of using words as values
|
10
|
-
def translate
|
11
|
-
context_words = []
|
12
|
-
Nokogiri::HTML(open(@uri)).xpath('//td[@class="resWordCol"]/a').each do |node|
|
13
|
-
context_words << node.text
|
14
|
-
end
|
15
|
-
make_hash_results(context_words)
|
16
|
-
rescue OpenURI::HTTPError
|
17
|
-
raise Dictionary::ConnectError
|
18
|
-
end
|
19
|
-
end
|
data/lib/module_main.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'dict'
|
4
|
-
require 'slop'
|
5
|
-
require 'timeout'
|
6
|
-
|
7
|
-
module Main
|
8
|
-
def self.parameters_valid?
|
9
|
-
not ARGV.empty?
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.parse_parameters
|
13
|
-
available_dictionaries = Dict.available_services.join(', ')
|
14
|
-
|
15
|
-
opts = Slop.parse! do
|
16
|
-
banner <<-END
|
17
|
-
Usage: dict WORD [OPTIONS]
|
18
|
-
Search WORD in dict, an open source dictionary aggregator.
|
19
|
-
END
|
20
|
-
|
21
|
-
on '-h', :help, 'Display this help message'
|
22
|
-
on '-t', :time=, 'Set timeout in seconds. Default: 300', :as => :int
|
23
|
-
on '-d', :dict=, "Select desired dictionary. Available options: #{available_dictionaries}"
|
24
|
-
end
|
25
|
-
opts
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.get_translations(opts, word)
|
29
|
-
Timeout::timeout(opts[:time].to_i || 300) do
|
30
|
-
if opts.dict?
|
31
|
-
Dict.get_single_dictionary_translations(word, opts[:dict])
|
32
|
-
else
|
33
|
-
Dict.get_all_dictionaries_translations(word)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
rescue Timeout::Error
|
37
|
-
"Timeout for the query."
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.main(opts)
|
41
|
-
begin
|
42
|
-
opts = parse_parameters
|
43
|
-
rescue Slop::MissingArgumentError
|
44
|
-
abort("Missing argument")
|
45
|
-
end
|
46
|
-
|
47
|
-
abort(opts.to_s) if opts.help?
|
48
|
-
parameters_valid? or abort("Please enter a word. (-h for help)")
|
49
|
-
|
50
|
-
puts get_translations(opts, ARGV[0])
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
data/lib/wiktionary.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'nokogiri'
|
4
|
-
require_relative 'dictionary'
|
5
|
-
|
6
|
-
WIKI_URL = 'http://en.wiktionary.org/wiki/'
|
7
|
-
|
8
|
-
class Wiktionary < Dictionary
|
9
|
-
# Method returns hash with translations as keys and examples of using words as values
|
10
|
-
def translate
|
11
|
-
context_words = []
|
12
|
-
url = 'http://en.wiktionary.org/wiki/'
|
13
|
-
get_html(@uri).css('p + ol li a').each do |node|
|
14
|
-
get_html(url + node.text.tr(' ', '_')).css('p + ol > li dl dd').each do |example|
|
15
|
-
context_words << node.text << example.text
|
16
|
-
end
|
17
|
-
end
|
18
|
-
make_hash_results(context_words)
|
19
|
-
end
|
20
|
-
|
21
|
-
def get_html(url)
|
22
|
-
Nokogiri::HTML(open(url))
|
23
|
-
rescue OpenURI::HTTPError
|
24
|
-
raise Dictionary::ConnectError
|
25
|
-
end
|
26
|
-
end
|