dict 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dict (0.3.0)
4
+ dict (0.3.1)
5
5
  nokogiri (~> 1.5.5)
6
6
  slop (~> 3.3.2)
7
7
 
@@ -1,5 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ # Class, which provides using application in Command Line Interface.
4
+
3
5
  require 'dict/dict'
4
6
  require 'dict/version'
5
7
  require 'slop'
@@ -54,6 +56,7 @@ Search WORD in dict, an open source dictionary aggregator.
54
56
  MSG = "Usage: dict WORD [OPTIONS]\nTry `dict --help for more information.\n"
55
57
  VERSION = "dict version #{Dict::VERSION}\nSearch WORD in dict, an open source dictionary aggregator.\nCopyright (C) 2012 by Mateusz Czerwiński, Michał Podlecki,\nAleksander Gozdek, Rafał Ośko, Jan Borwin, Kosma Dunikowski\nMentors: Grzegorz Kołodziejski, Michał Kwiatkowski\nMade during intership at Ragnarson http://ragnarson.com\nHosted by Shelly Cloud https://shellycloud.com\nLicense: MIT\nHomepage: https://github.com/Ragnarson/dict-gem\nSources dictionaries: http://dict.pl, http://pl.wiktionary.org"
56
58
 
59
+ # Returns only translations of the given word, without example sentences.
57
60
  def clean_translation(opts, word)
58
61
  translation = get_translations(opts, word)
59
62
  string = String.new
data/lib/dict/dict.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'dict/wiktionary'
4
+ require 'dict/glosbe'
5
+
4
6
  require "yaml"
5
7
 
6
8
  module Dict
7
9
  class << self
8
- # returns hash with structure as showed below
10
+ # Returns hash with structure as showed below
9
11
  # { 'DICTIONARY_NAME' => { 'TRANSLATION' => ['EXAMPLE', ...], ... }, ... }
10
12
  def get_all_dictionaries_translations(word)
11
13
  dictionaries = Hash.new
@@ -16,34 +18,36 @@ module Dict
16
18
  dictionaries
17
19
  end
18
20
 
19
- # prints translations from all dictionaries
21
+ # Prints translations from all dictionaries.
20
22
  def print_all_dictionaries_translations(word)
21
23
  available_dictionaries.each do |dictionary|
22
24
  print_single_dictionary_translations(word, dictionary)
23
25
  end
24
26
  end
25
27
 
26
- # returns hash with structure as showed below
28
+ # Returns hash with structure as showed below
27
29
  # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
28
30
  def get_single_dictionary_translations(word, dictionary)
29
31
  case dictionary
30
- when 'wiktionary'
31
- Wiktionary.new(word).translate.translations
32
- else Dictionary.message
32
+ when 'wiktionary'
33
+ Wiktionary.new(word).translate.translations
34
+ when 'glosbe'
35
+ Glosbe.new(word).translate.translations
36
+ else Dictionary.message
33
37
  end
34
38
  rescue Dictionary::ConnectError
35
39
  "Couldn't connect to the dictionary."
36
40
  end
37
41
 
38
- # prints translations from single dictionary
42
+ # Prints translations from single dictionary.
39
43
  def print_single_dictionary_translations(word, dictionary)
40
44
  puts "Word '#{word.upcase}' translations from #{dictionary.upcase} dictionary."
41
45
  puts get_single_dictionary_translations(word, dictionary).to_yaml
42
46
  end
43
47
 
44
- # returns array of currently available dictionaries
48
+ # Returns array of currently available dictionaries.
45
49
  def available_dictionaries
46
- ['wiktionary']
50
+ ['wiktionary', 'glosbe']
47
51
  end
48
52
  end
49
53
  end
@@ -1,5 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ # It is a base class for classes fetching results from Web dictionaries.
4
+
3
5
  require 'open-uri'
4
6
  require 'dict/result'
5
7
 
@@ -15,7 +17,7 @@ module Dict
15
17
  @result = Dict::Result.new(@word)
16
18
  end
17
19
 
18
- # returns hash with structure as showed below
20
+ # Returns hash with structure as showed below
19
21
  # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
20
22
  def make_hash_results(arr)
21
23
  hash = arr.each_slice(2).inject({}) do |h, (key, value)|
@@ -29,12 +31,12 @@ module Dict
29
31
  hash
30
32
  end
31
33
 
32
- # returns an instance of URI::HTTP class
34
+ # Returns an instance of URI::HTTP class.
33
35
  def uri(url, word = nil)
34
36
  word == nil ? URI(URI.escape(url)) : URI(URI.escape(url + word.downcase.tr(' ', '_')))
35
37
  end
36
38
 
37
- # checks if word was given correctly
39
+ # Checks if word was given correctly.
38
40
  def check_arguments(word)
39
41
  raise ArgumentError.new("No given word") if word.empty?
40
42
  end
@@ -0,0 +1,66 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'nokogiri'
4
+ require 'dict/dictionary'
5
+
6
+ GLOSBE_PL = 'http://glosbe.com/pl/en/' # polish - english
7
+ GLOSBE_EN = 'http://glosbe.com/en/pl/' # english - polish
8
+
9
+ module Dict
10
+ class Glosbe < Dictionary
11
+ # returns an Dict::Result object
12
+ def translate
13
+ begin
14
+ if is_polish?(doc = get_content(GLOSBE_PL, @word))
15
+ add_translations(get_translations(doc))
16
+ add_examples(get_examples(doc, 'en'))
17
+ else
18
+ doc = get_content(GLOSBE_EN, @word)
19
+ add_translations(get_translations(doc))
20
+ add_examples(get_examples(doc, 'pl'))
21
+ end
22
+
23
+ @result
24
+ rescue OpenURI::HTTPError
25
+ raise Dictionary::ConnectError
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ # checks if given word is polish
32
+ def is_polish?(doc)
33
+ doc.at_css('.content_box_rounded p').nil?
34
+ end
35
+
36
+ # returns a html structure of visited site
37
+ def get_content(url, word)
38
+ Nokogiri::HTML(open(uri(url, word))).css('.wordDetails')
39
+ end
40
+
41
+ # returns array with structure as shown below from the given dictionary link
42
+ # ['TRANSLATION1', 'TRANSLATION2', ...]
43
+ def get_translations(doc)
44
+ doc.css('.phrase-container > .translation').each { |translation| translations.push(translation.text.downcase) }
45
+ translations
46
+ end
47
+
48
+ # add obtained translations to Dict::Result object
49
+ def add_translations(translations)
50
+ translations.each { |translation| @result.add_translation(@result.term, translation) }
51
+ end
52
+
53
+ # returns array with structure as shown below from the given dictionary link
54
+ # ['EXAMPLE1', 'EXAMPLE2', ...]
55
+ # the default length of given example is 60 characters
56
+ def get_examples(doc, lang, length = 60)
57
+ doc.css(".tranlastionMemory td[lang=#{lang}]").each { |example| examples.push(example.text.capitalize) if example.text.length < length }
58
+ examples
59
+ end
60
+
61
+ # add obtained examples to Dict::Result object
62
+ def add_examples(examples)
63
+ examples.each { |example| @result.add_example(@result.term, example) }
64
+ end
65
+ end
66
+ end
data/lib/dict/result.rb CHANGED
@@ -1,3 +1,6 @@
1
+ # Objects of this class are returned by methods retrieving translations
2
+ # from Web dictionaries.
3
+
1
4
  module Dict
2
5
  class Result
3
6
  attr_reader :term, :translations, :examples
data/lib/dict/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dict
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -1,12 +1,14 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ # Class fetching translations of given word from wiktionary.org.
4
+
3
5
  require 'nokogiri'
4
6
  require 'dict/dictionary'
5
7
 
6
8
  module Dict
7
9
  class Wiktionary < Dictionary
8
10
 
9
- # Returns an Dict::Result object
11
+ # Returns an Dict::Result object.
10
12
  def translate
11
13
  translations.each { |item| @result.add_translation(@result.term, item) }
12
14
 
@@ -36,12 +36,12 @@ end
36
36
 
37
37
 
38
38
  describe "get_translations" do
39
- it "should return results from wiktionary and dictpl for word 'słowik'" do
39
+ it "should return results from wiktionary and glosbe for word 'słowik'" do
40
40
  VCR.use_cassette('translations_slownik_cassette') do
41
41
  stub_const("ARGV", ["słowik"])
42
42
  runner = Dict::CLI::Runner.new
43
43
  opts = runner.parse_parameters
44
- runner.get_translations(opts, "słowik").should == {"wiktionary"=>{"słowik"=>["nightingale"]}}
44
+ runner.get_translations(opts, "słowik").should == {"wiktionary" => {"słowik" => ["nightingale"]}, "glosbe" => {"słowik" => ["nightingale", "bulbul"]}}
45
45
  end
46
46
  end
47
47
 
@@ -65,8 +65,8 @@ describe "get_translations" do
65
65
  end
66
66
 
67
67
  describe "CLI::Runner" do
68
- HELP_MSG = "Usage: dict WORD [OPTIONS]\nSearch WORD in dict, an open source dictionary aggregator.\n\n -h, --help Display this help message\n -t, --time Set timeout in seconds. Default: 300\n -d, --dict Select desired dictionary. Available options: wiktionary\n -v, --version Information about gem, authors, license\n -c, --clean Remove examples from translation"
69
- DICT_MSG = "Missing argument. Expected: wiktionary"
68
+ HELP_MSG = "Usage: dict WORD [OPTIONS]\nSearch WORD in dict, an open source dictionary aggregator.\n\n -h, --help Display this help message\n -t, --time Set timeout in seconds. Default: 300\n -d, --dict Select desired dictionary. Available options: wiktionary, glosbe\n -v, --version Information about gem, authors, license\n -c, --clean Remove examples from translation"
69
+ DICT_MSG = "Missing argument. Expected: wiktionary, glosbe"
70
70
  TIME_MSG = "Missing argument. Expected: number of seconds"
71
71
  T_MSG = "Wrong time value."
72
72
 
@@ -108,10 +108,12 @@ describe "CLI::Runner" do
108
108
  end
109
109
 
110
110
  it "should return string when you use --clean parameter" do
111
- stub_const("ARGV",["słowik","--clean"])
112
- runner = Dict::CLI::Runner.new
113
- opts = runner.parse_parameters
114
- runner.clean_translation(opts, ARGV[0]).should == "słowik : nightingale"
111
+ VCR.use_cassette('slowik_runner_cassette') do
112
+ stub_const("ARGV",["słowik","--clean"])
113
+ runner = Dict::CLI::Runner.new
114
+ opts = runner.parse_parameters
115
+ runner.clean_translation(opts, ARGV[0]).should == "słowik : nightingale, nightingale, bulbul"
116
+ end
115
117
  end
116
118
 
117
119
 
@@ -10,16 +10,19 @@ describe Dict do
10
10
  arr.size.should_not == 0
11
11
  end
12
12
 
13
- it "should return array of available services, which contains wiktionary and dictpl" do
14
- Dict.available_dictionaries.should == ['wiktionary']
13
+ it "should return array of available services, which contains wiktionary and glosbe" do
14
+ Dict.available_dictionaries.should == ['wiktionary', 'glosbe']
15
15
  end
16
16
 
17
17
  it "should return hash with translations from all dictionaries" do
18
18
  wiktionary = stub(:translate => stub(:translations => {'WORD' => 'WIKTIONARY_RESULTS'}))
19
19
  Dict::Wiktionary.should_receive(:new).with('WORD').and_return(wiktionary)
20
20
 
21
+ glosbe = stub(:translate => stub(:translations => {'WORD' => 'GLOSBE_RESULTS'}))
22
+ Dict::Glosbe.should_receive(:new).with('WORD').and_return(glosbe)
23
+
21
24
  results = Dict::get_all_dictionaries_translations('WORD')
22
- results.should == {'wiktionary' => {'WORD' => 'WIKTIONARY_RESULTS'}}
25
+ results.should == {'wiktionary' => {'WORD' => 'WIKTIONARY_RESULTS'}, 'glosbe' => {'WORD' => 'GLOSBE_RESULTS'}}
23
26
  end
24
27
 
25
28
  it "should return whatever Wiktionary returns embedded in a hash" do
@@ -27,4 +30,10 @@ describe Dict do
27
30
  Dict::Wiktionary.should_receive(:new).with('WORD').and_return(wiktionary)
28
31
  Dict.get_single_dictionary_translations('WORD', 'wiktionary').should == 'WIKTIONARY_RESULTS'
29
32
  end
33
+
34
+ it "should return whatever Glosbe returns embedded in a hash" do
35
+ glosbe = stub(:translate => stub( :translations => 'GLOSBE_RESULTS'))
36
+ Dict::Glosbe.should_receive(:new).with('WORD').and_return(glosbe)
37
+ Dict.get_single_dictionary_translations('WORD', 'glosbe').should == 'GLOSBE_RESULTS'
38
+ end
30
39
  end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*
2
+
3
+ require_relative './vcr_setup'
4
+ require 'dict/glosbe'
5
+
6
+ describe Dict::Glosbe do
7
+
8
+ it "should raise no given word exception" do
9
+ expect { Dict::Glosbe.new }.to raise_error ArgumentError
10
+ end
11
+
12
+ it "should return a Result object" do
13
+ VCR.use_cassette('glosbe_translations_woda_cassette') do
14
+ g = Dict::Glosbe.new('woda').translate
15
+ g.should be_a(Dict::Result)
16
+ end
17
+ end
18
+
19
+ it "should return translations of polish word 'woda' to english with its examples" do
20
+ VCR.use_cassette('glosbe_translations_woda_cassette') do
21
+ g = Dict::Glosbe.new('woda').translate
22
+ g.translations.should == {"woda"=>["water", "aqua"]}
23
+ g.examples.should == {"woda"=>["Details of food and water quality", "Mineral waters, soft drinks and juices (nd", "Fishing for herring in area iia (ec waters", "Bind him, cast him into the slop- pool at low tide!"]}
24
+ end
25
+ end
26
+
27
+ it "should return translations of english word 'atomic' to polish with its examples" do
28
+ VCR.use_cassette('glosbe_translations_atomic_cassette') do
29
+ g = Dict::Glosbe.new('atomic').translate
30
+ g.translations.should == {"atomic"=>["atomowy", "niepodzielny", "atomistyczny", "jednolity"]}
31
+ g.examples.should == {"atomic"=>["Spektrofotometr absorpcji atomowej", "Atom w lewo", "Pomiary metodą absorpcji atomowej"]}
32
+ end
33
+ end
34
+ end
@@ -38,6 +38,8 @@ describe Dict::Wiktionary do
38
38
  end
39
39
 
40
40
  it "should remove html tags from translations of 'dragon' word" do
41
- Dict::Wiktionary.new("dragon").translate.translations.should eq({'dragon' => ['smok']})
41
+ VCR.use_cassette('translations_dragon_cassette') do
42
+ Dict::Wiktionary.new("dragon").translate.translations.should eq({'dragon' => ['smok']})
43
+ end
42
44
  end
43
45
  end