dict 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +1 -1
  2. data/.rspec +2 -2
  3. data/.travis.yml +14 -14
  4. data/Gemfile +4 -4
  5. data/Gemfile.lock +40 -40
  6. data/README.md +34 -34
  7. data/Rakefile +8 -8
  8. data/bin/dict +7 -7
  9. data/dict.gemspec +28 -28
  10. data/lib/dict/cli/runner.rb +137 -115
  11. data/lib/dict/dict.rb +40 -53
  12. data/lib/dict/dictionary.rb +45 -62
  13. data/lib/dict/glosbe.rb +68 -66
  14. data/lib/dict/result.rb +38 -38
  15. data/lib/dict/version.rb +3 -3
  16. data/lib/dict/wiktionary.rb +91 -91
  17. data/lib/dict.rb +5 -5
  18. data/spec/dict/lib_dict_cli_runner_spec.rb +120 -120
  19. data/spec/dict/lib_dict_spec.rb +39 -39
  20. data/spec/dict/lib_glosbe_spec.rb +48 -34
  21. data/spec/dict/lib_wiktionary_spec.rb +68 -62
  22. data/spec/dict/spec_helper.rb +17 -17
  23. data/spec/dict/vcr_cassettes/glosbe_translations_asdfff_cassette.yml +1518 -0
  24. data/spec/dict/vcr_cassettes/glosbe_translations_atomic_cassette.yml +2794 -2794
  25. data/spec/dict/vcr_cassettes/glosbe_translations_usage_cassette.yml +2530 -0
  26. data/spec/dict/vcr_cassettes/glosbe_translations_woda_cassette.yml +1949 -1949
  27. data/spec/dict/vcr_cassettes/slowik_runner_cassette.yml +4178 -4178
  28. data/spec/dict/vcr_cassettes/translations_dragon_cassette.yml +8659 -8659
  29. data/spec/dict/vcr_cassettes/translations_slownik_cassette.yml +4177 -4177
  30. data/spec/dict/vcr_cassettes/wiktionary_no_usage_examples.yml +8634 -0
  31. data/spec/dict/vcr_cassettes/wiktionary_translate_result_uppercase.yml +6120 -0
  32. data/spec/dict/vcr_cassettes/wiktionary_translations_field_cassette.yml +8600 -8600
  33. data/spec/dict/vcr_cassettes/wiktionary_translations_samochod_cassette.yml +6140 -6140
  34. data/spec/dict/vcr_cassettes/wiktionary_usage_examples_kot.yml +6444 -0
  35. data/spec/dict/vcr_setup.rb +19 -19
  36. metadata +9 -4
data/.gitignore CHANGED
@@ -1,2 +1,2 @@
1
- *~
1
+ *~
2
2
  *.gem
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --color
2
- --format progress
1
+ --color
2
+ --format progress
data/.travis.yml CHANGED
@@ -1,14 +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
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 CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in dict.gemspec
4
- gemspec
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dict.gemspec
4
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,40 +1,40 @@
1
- PATH
2
- remote: .
3
- specs:
4
- dict (0.3.3)
5
- nokogiri (~> 1.5.5)
6
- slop (~> 3.3.2)
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- activesupport (3.2.6)
12
- i18n (~> 0.6)
13
- multi_json (~> 1.0)
14
- diff-lcs (1.1.3)
15
- fakeweb (1.3.0)
16
- i18n (0.6.0)
17
- multi_json (1.3.6)
18
- nokogiri (1.5.5)
19
- rake (0.9.2.2)
20
- rspec (2.11.0)
21
- rspec-core (~> 2.11.0)
22
- rspec-expectations (~> 2.11.0)
23
- rspec-mocks (~> 2.11.0)
24
- rspec-core (2.11.0)
25
- rspec-expectations (2.11.1)
26
- diff-lcs (~> 1.1.3)
27
- rspec-mocks (2.11.1)
28
- slop (3.3.2)
29
- vcr (2.2.3)
30
-
31
- PLATFORMS
32
- ruby
33
-
34
- DEPENDENCIES
35
- activesupport
36
- dict!
37
- fakeweb
38
- rake
39
- rspec (~> 2.11)
40
- vcr
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dict (0.3.4)
5
+ nokogiri (~> 1.5.5)
6
+ slop (~> 3.3.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.2.6)
12
+ i18n (~> 0.6)
13
+ multi_json (~> 1.0)
14
+ diff-lcs (1.1.3)
15
+ fakeweb (1.3.0)
16
+ i18n (0.6.0)
17
+ multi_json (1.3.6)
18
+ nokogiri (1.5.5)
19
+ rake (0.9.2.2)
20
+ rspec (2.11.0)
21
+ rspec-core (~> 2.11.0)
22
+ rspec-expectations (~> 2.11.0)
23
+ rspec-mocks (~> 2.11.0)
24
+ rspec-core (2.11.0)
25
+ rspec-expectations (2.11.1)
26
+ diff-lcs (~> 1.1.3)
27
+ rspec-mocks (2.11.1)
28
+ slop (3.3.2)
29
+ vcr (2.2.3)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ activesupport
36
+ dict!
37
+ fakeweb
38
+ rake
39
+ rspec (~> 2.11)
40
+ vcr
data/README.md CHANGED
@@ -1,35 +1,35 @@
1
- # dict [![Build Status](https://secure.travis-ci.org/Ragnarson/dict-gem.png?branch=master)](http://travis-ci.org/Ragnarson/dict-gem)
2
- CLI and backend for dict - an open source dictionary aggregator.
3
-
4
- ## Usage ##
5
- To search for `WORD`:
6
-
7
- dict WORD [OPTIONS]
8
-
9
- ### Available options ###
10
-
11
- - `-h` or `--help` - displays help message
12
- - `-t` or `--time` - used to set timeout in seconds. Default value: 300
13
- - `-d` or `--dict` - used to select desired dictionary
14
- - `-v` or `--version` - used to show version, authors, license
15
- - `-c` or `--clean` - used to delete examples from translation
16
-
17
-
18
-
19
- ## People
20
-
21
- ### Trainees
22
- - [Jan Borwin](https://github.com/janiuuuu)
23
- - [Mateusz Czerwiński](https://github.com/mrfustrr)
24
- - [Kosma Dunikowski](https://github.com/kdunee)
25
- - [Aleksander Gozdek](https://github.com/alexgozdek)
26
- - [Rafał Ośko](https://github.com/rekosko)
27
- - [Michał Podlecki](https://github.com/michalpodlecki)
28
-
29
- ### Mentors
30
- - [Grzegorz Kołodziejczyk](https://github.com/grk)
31
- - [Michał Kwiatkowski](https://github.com/mkwiatkowski)
32
-
33
- ## Made during internship at [Ragnarson](http://ragnarson.com/)
34
-
1
+ # dict [![Build Status](https://secure.travis-ci.org/Ragnarson/dict-gem.png?branch=master)](http://travis-ci.org/Ragnarson/dict-gem)
2
+ CLI and backend for dict - an open source dictionary aggregator.
3
+
4
+ ## Usage ##
5
+ To search for `WORD`:
6
+
7
+ dict WORD [OPTIONS]
8
+
9
+ ### Available options ###
10
+
11
+ - `-h` or `--help` - displays help message
12
+ - `-t` or `--time` - used to set timeout in seconds. Default value: 300
13
+ - `-d` or `--dict` - used to select desired dictionary
14
+ - `-v` or `--version` - used to show version, authors, license
15
+ - `-c` or `--clean` - used to delete examples from translation
16
+
17
+
18
+
19
+ ## People
20
+
21
+ ### Trainees
22
+ - [Jan Borwin](https://github.com/janiuuuu)
23
+ - [Mateusz Czerwiński](https://github.com/mrfustrr)
24
+ - [Kosma Dunikowski](https://github.com/kdunee)
25
+ - [Aleksander Gozdek](https://github.com/alexgozdek)
26
+ - [Rafał Ośko](https://github.com/rekosko)
27
+ - [Michał Podlecki](https://github.com/michalpodlecki)
28
+
29
+ ### Mentors
30
+ - [Grzegorz Kołodziejczyk](https://github.com/grk)
31
+ - [Michał Kwiatkowski](https://github.com/mkwiatkowski)
32
+
33
+ ## Made during internship at [Ragnarson](http://ragnarson.com/)
34
+
35
35
  ## Hosted by [Shelly Cloud](https://shellycloud.com/)
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- task :default => :spec
5
- desc "Run specs"
6
- RSpec::Core::RakeTask.new do |t|
7
- t.rspec_opts = %w(-fp --color)
8
- end
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = %w(-fp --color)
8
+ end
data/bin/dict CHANGED
@@ -1,7 +1,7 @@
1
- #!/usr/bin/ruby
2
- # -*- coding: utf-8 -*-
3
-
4
- require 'dict/cli/runner'
5
-
6
- runner = Dict::CLI::Runner.new
7
- runner.run
1
+ #!/usr/bin/ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'dict/cli/runner'
5
+
6
+ runner = Dict::CLI::Runner.new
7
+ runner.run
data/dict.gemspec CHANGED
@@ -1,28 +1,28 @@
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
- s.add_development_dependency "vcr"
10
- s.add_development_dependency "activesupport"
11
- s.add_development_dependency "fakeweb"
12
-
13
- s.name = %q{dict}
14
- s.version = Dict::VERSION
15
- s.authors = ['Aleksander Gozdek', 'Mateusz Czerwinski', 'Michał Podlecki', 'Rafał Ośko', 'Kosma Dunikowski', 'Jan Borwin']
16
- s.email = ['mtczerwinski@gmail.com']
17
- s.date = Time.now.strftime('%Y-%m-%d')
18
- s.summary = %q{Gem made for dictionary application}
19
- s.description = <<-END
20
- Dict is an open source dictionary aggregator.
21
- END
22
- s.homepage = 'https://github.com/Ragnarson/dict-gem'
23
-
24
- s.files = `git ls-files`.split("\n")
25
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
- s.require_paths = ["lib"]
28
- end
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
+ s.add_development_dependency "vcr"
10
+ s.add_development_dependency "activesupport"
11
+ s.add_development_dependency "fakeweb"
12
+
13
+ s.name = %q{dict}
14
+ s.version = Dict::VERSION
15
+ s.authors = ['Aleksander Gozdek', 'Mateusz Czerwinski', 'Michał Podlecki', 'Rafał Ośko', 'Kosma Dunikowski', 'Jan Borwin']
16
+ s.email = ['mtczerwinski@gmail.com']
17
+ s.date = Time.now.strftime('%Y-%m-%d')
18
+ s.summary = %q{Gem stworzony dla aplikacji słownikowej}
19
+ s.description = <<-END
20
+ Dict to open-source'owy agregator słowników.
21
+ END
22
+ s.homepage = 'https://github.com/Ragnarson/dict-gem'
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
@@ -1,115 +1,137 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Class, which provides using application in Command Line Interface.
4
-
5
- require 'dict/dict'
6
- require 'dict/version'
7
- require 'slop'
8
- require 'timeout'
9
-
10
- module Dict
11
- module CLI
12
- class Runner
13
- def parameters_valid?
14
- not ARGV.empty?
15
- end
16
-
17
- def parse_parameters
18
- opts = Slop.parse! do
19
- banner <<-END
20
- Usage: dict WORD [OPTIONS]
21
- Search WORD in dict, an open source dictionary aggregator.
22
- END
23
-
24
- on '-h', :help=, 'Display this help message', :argument => :optional
25
- on '-t', :time=, 'Set timeout in seconds. Default: 300', :as => :int
26
- on '-d', :dict=, "Select desired dictionary. Available options: #{Dict.available_dictionaries.join(', ')}"
27
- on '-v', :version=, "Information about gem, authors, license", :argument => :optional
28
- on '-c', :clean=, "Remove examples from translation", :argument => :optional
29
-
30
- end
31
- end
32
-
33
- def get_translations(opts, word)
34
- Timeout::timeout(opts[:time].to_i || 300) do
35
- if opts.dict?
36
- Dict.get_single_dictionary_translations(word, opts[:dict])
37
- else
38
- Dict.get_all_dictionaries_translations(word)
39
- end
40
- end
41
- rescue Timeout::Error
42
- "Timeout for the query."
43
- end
44
-
45
- def expected_argument_description(option)
46
- case option
47
- when "dict"
48
- Dict.available_dictionaries.join(', ')
49
- when "time"
50
- "number of seconds"
51
- else
52
- "?"
53
- end
54
- end
55
-
56
- MSG = "Usage: dict WORD [OPTIONS]\nTry `dict --help for more information.\n"
57
- VERSION = "dict version #{Dict::VERSION}\nSearch WORD in dict, an open source dictionary aggregator.\nCopyright (C) 2012 by\nTrainees:\n Jan Borwin\n Mateusz Czerwiński\n Kosma Dunikowski\n Aleksander Gozdek\n Rafał Ośko\n Michał Podlecki\nMentors:\n Grzegorz Kołodziejski\n Michał Kwiatkowski\nLicense: MIT\nMade during intership at Ragnarson : http://ragnarson.com/\nHosted by Shelly Cloud : http://shellycloud.com/\nHomepage: http://github.com/Ragnarson/dict-gem/\nSources of dictionaries: http://wiktionary.org/\n http://glosbe.com/\n"
58
-
59
- # Returns only translations of the given word, without example sentences.
60
- def clean_translation(opts, word)
61
- translation = get_translations(opts, word)
62
- string = String.new
63
- string += word + " : "
64
- if opts.dict?
65
- translation[word].each { |x| string << x << ", " }
66
- else
67
- begin
68
- Dict.available_dictionaries.each do |x|
69
- translation[x][word].each { |y| string << y << ", " }
70
- end
71
- rescue NoMethodError
72
- return "No translation found."
73
- end
74
-
75
- end
76
- 2.times { string.chop! }
77
- string
78
- end
79
-
80
- def run
81
- begin
82
- opts = parse_parameters
83
- rescue Slop::MissingArgumentError => e
84
- incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1]
85
- description = expected_argument_description(incomplete_option)
86
- abort("Missing argument. Expected: #{description}")
87
-
88
- end
89
-
90
- if opts.help?
91
- abort(opts.to_s)
92
- elsif opts.version?
93
- abort(VERSION)
94
- elsif opts.time?
95
- if (opts[:time].to_i) == 0
96
- abort("Wrong time value.")
97
- else
98
- puts get_translations(opts, ARGV[0])
99
- end
100
- else
101
- if !parameters_valid?
102
- abort(MSG)
103
- else
104
- if opts.clean?
105
- puts clean_translation(opts, ARGV[0])
106
- else
107
- puts get_translations(opts, ARGV[0])
108
- end
109
- end
110
- end
111
-
112
- end
113
- end
114
- end
115
- end
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Class, which provides using application in Command Line Interface.
4
+
5
+ require 'dict/dict'
6
+ require 'dict/version'
7
+ require 'slop'
8
+ require 'timeout'
9
+
10
+ module Dict
11
+ module CLI
12
+ class Runner
13
+
14
+ def parameters_valid?
15
+ not ARGV.empty?
16
+ end
17
+
18
+ def parse_parameters
19
+ opts = Slop.parse! do
20
+ banner <<-END
21
+ Przykład użycia: dict SŁOWO [OPCJE]
22
+ Wyszukaj SŁOWO w dict, open-source'owym agregatorze słowników.
23
+ END
24
+
25
+ on '-h', :help=, 'Wyświetl pomoc', :argument => :optional
26
+ on '-t', :time=, 'Ustaw limit czasu żądania w sekundach. Domyślnie: 300', :as => :int
27
+ on '-d', :dict=, "Wybierz słownik. Dostępne : #{Dict.available_dictionaries.join(', ')}"
28
+ on '-v', :version=, "Informacje o gemie, autorach, licencji", :argument => :optional
29
+ on '-c', :clean=, "Nie wyświetlaj przykładów użycia", :argument => :optional
30
+
31
+ end
32
+ end
33
+
34
+ def get_translations(opts, word)
35
+ Timeout::timeout(opts[:time].to_i || 300) do
36
+ if opts.dict?
37
+ Dict.get_single_dictionary_translations(word, opts[:dict])
38
+ else
39
+ Dict.get_all_dictionaries_translations(word)
40
+ end
41
+ end
42
+ rescue Timeout::Error
43
+ "Upłynął limit czasu żądania."
44
+ end
45
+
46
+ def expected_argument_description(option)
47
+ case option
48
+ when "dict"
49
+ Dict.available_dictionaries.join(', ')
50
+ when "time"
51
+ "liczba sekund"
52
+ else
53
+ "?"
54
+ end
55
+ end
56
+
57
+ MSG = "Przykład użycia: dict SŁOWO [OPCJE]\n `dict --help, aby uzyskać więcej informacji.\n"
58
+ VERSION = "dict wersja #{Dict::VERSION}\nWyszukaj SŁOWO w dict, open-source'owym agregatorze słowników. \nCopyright (C) 2012 by\nZespół:\n Jan Borwin\n Mateusz Czerwiński\n Kosma Dunikowski\n Aleksander Gozdek\n Rafał Ośko\n Michał Podlecki\nMentorzy:\n Grzegorz Kołodziejski\n Michał Kwiatkowski\nLicencja: MIT\nStworzono na praktykach w : http://ragnarson.com/\nHosting: Shelly Cloud :\t http://shellycloud.com/\nStrona domowa:\t\t http://github.com/Ragnarson/dict-gem/\nSłowniki:\t\t http://wiktionary.org/\n\t\t\t http://glosbe.com/\n"
59
+
60
+ # Returns array without duplicates
61
+ def clean_translation(results)
62
+ clean = []
63
+ results.each do |_, translations_hash|
64
+ translations_hash.each do |_, translations|
65
+ clean.concat(translations)
66
+ end
67
+ end
68
+ clean.uniq
69
+ end
70
+
71
+ # Prints translations from all dictionaries
72
+ def print_all_dictionaries_translations(results)
73
+ results.each do |dictionary, translations_hash|
74
+ puts "Nazwa słownika - #{dictionary.upcase}"
75
+ print_translations(translations_hash)
76
+ end
77
+ end
78
+
79
+ # Prints translations for given Hash or Array
80
+ def print_translations(results)
81
+ if results.empty?
82
+ puts 'Przepraszamy, ale w wybranym słowniku nie znaleziono tłumaczenia.'
83
+ else
84
+ if results.instance_of?(Hash)
85
+ results.each do |_, translations|
86
+ print_array(translations)
87
+ end
88
+ elsif results.instance_of?(Array)
89
+ print_array(results)
90
+ end
91
+ end
92
+ end
93
+
94
+ # Prints array elements one by one vertically
95
+ def print_array(arr)
96
+ puts "Znaleziono: #{arr.size} tłumaczeń"
97
+ arr.each { |el| puts "- #{el}" }
98
+ end
99
+
100
+ def run
101
+ begin
102
+ opts = parse_parameters
103
+ rescue Slop::MissingArgumentError => e
104
+ incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1]
105
+ description = expected_argument_description(incomplete_option)
106
+ abort("Brakujący argument. Spodziewano: #{description}")
107
+ end
108
+
109
+ if opts.help?
110
+ abort(opts.to_s)
111
+ elsif opts.version?
112
+ abort(VERSION)
113
+ elsif opts.time?
114
+ if (opts[:time].to_i) == 0
115
+ abort("Nieprawidłowa wartość czasu.")
116
+ else
117
+ print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
118
+ end
119
+ else
120
+ if !parameters_valid?
121
+ abort(MSG)
122
+ else
123
+ if opts.clean? && !opts.dict?
124
+ print_translations(clean_translation(get_translations(opts, ARGV[0])))
125
+ else
126
+ if opts.dict?
127
+ print_translations(get_translations(opts, ARGV[0]))
128
+ else
129
+ print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
data/lib/dict/dict.rb CHANGED
@@ -1,53 +1,40 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require 'dict/wiktionary'
4
- require 'dict/glosbe'
5
-
6
- require "yaml"
7
-
8
- module Dict
9
- class << self
10
- # Returns hash with structure as showed below
11
- # { 'DICTIONARY_NAME' => { 'TRANSLATION' => ['EXAMPLE', ...], ... }, ... }
12
- def get_all_dictionaries_translations(word)
13
- dictionaries = Hash.new
14
-
15
- available_dictionaries.each do |dictionary|
16
- dictionaries[dictionary] = get_single_dictionary_translations(word, dictionary)
17
- end
18
- dictionaries
19
- end
20
-
21
- # Prints translations from all dictionaries.
22
- def print_all_dictionaries_translations(word)
23
- available_dictionaries.each do |dictionary|
24
- print_single_dictionary_translations(word, dictionary)
25
- end
26
- end
27
-
28
- # Returns hash with structure as showed below
29
- # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
30
- def get_single_dictionary_translations(word, dictionary)
31
- case dictionary
32
- when 'wiktionary'
33
- Wiktionary.new(word).translate.translations
34
- when 'glosbe'
35
- Glosbe.new(word).translate.translations
36
- else Dictionary.message
37
- end
38
- rescue Dictionary::ConnectError
39
- "Couldn't connect to the dictionary."
40
- end
41
-
42
- # Prints translations from single dictionary.
43
- def print_single_dictionary_translations(word, dictionary)
44
- puts "Word '#{word.upcase}' translations from #{dictionary.upcase} dictionary."
45
- puts get_single_dictionary_translations(word, dictionary).to_yaml
46
- end
47
-
48
- # Returns array of currently available dictionaries.
49
- def available_dictionaries
50
- ['wiktionary', 'glosbe']
51
- end
52
- end
53
- end
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'dict/wiktionary'
4
+ require 'dict/glosbe'
5
+
6
+ require "yaml"
7
+
8
+ module Dict
9
+ class << self
10
+ # Returns hash with structure as showed below
11
+ # { 'DICTIONARY_NAME' => { 'TRANSLATION' => ['EXAMPLE', ...], ... }, ... }
12
+ def get_all_dictionaries_translations(word)
13
+ dictionaries = Hash.new
14
+
15
+ available_dictionaries.each do |dictionary|
16
+ dictionaries[dictionary] = get_single_dictionary_translations(word, dictionary)
17
+ end
18
+ dictionaries
19
+ end
20
+
21
+ # Returns hash with structure as showed below
22
+ # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
23
+ def get_single_dictionary_translations(word, dictionary)
24
+ case dictionary
25
+ when 'wiktionary'
26
+ Wiktionary.new(word).translate.translations
27
+ when 'glosbe'
28
+ Glosbe.new(word).translate.translations
29
+ else Dictionary.message
30
+ end
31
+ rescue Dictionary::ConnectError
32
+ "Couldn't connect to the dictionary."
33
+ end
34
+
35
+ # Returns array of currently available dictionaries.
36
+ def available_dictionaries
37
+ ['wiktionary', 'glosbe']
38
+ end
39
+ end
40
+ end