dict 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/dict.rb +22 -25
  2. data/lib/dictpl.rb +8 -19
  3. data/lib/wiktionary.rb +64 -37
  4. metadata +1 -1
data/lib/dict.rb CHANGED
@@ -1,36 +1,33 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
- require 'uri'
5
- require 'net/http'
6
- require 'timeout'
4
+ require_relative 'wiktionary'
5
+ require_relative 'dictpl'
7
6
 
8
7
  module Dict
9
- class Translation
10
- def initialize(word)
11
- @word = word
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
+
16
+ dictionaries
12
17
  end
13
18
 
14
- def self.status(adres = "http://dict-app-staging.shellyapp.com/")
15
- uri = URI(adres)
16
- res = Net::HTTP.get_response(uri)
17
- res.message
18
- end
19
-
20
- def self.get_response(word, time, dict)
21
- uri = URI.parse(URI.escape("http://dict-app-staging.shellyapp.com/#{dict}/#{word}"))
22
- begin
23
- Timeout::timeout(time.to_i) do
24
- res = Net::HTTP.get(uri)
25
- "Success" unless res.empty?
26
- end
27
-
28
- rescue
29
- puts "Timeout for the query."
30
- {:code => 408, :describe => "Request Timeout"}
19
+ def get_single_dictionary_translations(word, service)
20
+ case service
21
+ when 'wiktionary'
22
+ Wiktionary.new(word).translate
23
+ when 'dictpl'
24
+ Dictpl.new(word).translate
25
+ else 'There\'s no such dictionary.'
31
26
  end
32
27
  end
28
+
29
+ def available_services
30
+ ['wiktionary', 'dictpl']
31
+ end
33
32
  end
34
- #t = Translation.new "samochód"
35
- #puts Translation.status
36
33
  end
data/lib/dictpl.rb CHANGED
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  require 'open-uri'
3
2
  require 'nokogiri'
4
3
  require_relative 'result'
@@ -7,9 +6,11 @@ require_relative 'result'
7
6
  class Dictpl
8
7
 
9
8
  DICT_URL = "http://dict.pl/dict?word="
10
- def initialize(word)
11
- check_arguments(word)
12
- initialize_instance_arguments(word)
9
+ def initialize(word)
10
+ raise ArgumentError, "No given word" if word.empty?
11
+ @word = word
12
+ @uri = URI(URI.escape(DICT_URL + @word + "&lang=EN"))
13
+ @result = Result.new(word)
13
14
  end
14
15
 
15
16
  #
@@ -30,22 +31,10 @@ class Dictpl
30
31
  @context_words.each_slice(2) do |word|
31
32
  @mapped_words[word.first] = word.last
32
33
  @result.add_translation(word.first, word.last)
33
- end
34
+ end
35
+
34
36
  @result
35
37
  end
36
-
37
- def check_arguments(word)
38
- if word.empty? then raise ArgumentError.new("No word given.") end
39
- end
40
-
41
- def initialize_instance_arguments(word)
42
- @word = word
43
- @uri = URI(URI.escape(DICT_URL + @word + "&lang=EN"))
44
- @result = Result.new(word)
45
- end
38
+
46
39
  end
47
40
 
48
- a = Dictpl.new "samochód"
49
- a.translate.translations.each_pair do |key, value|
50
- puts key + value.to_s
51
- end
data/lib/wiktionary.rb CHANGED
@@ -1,54 +1,81 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- coding: utf-8 -*-
3
+
4
+ # Method returns an Result Object containing given word,
5
+ # translations, word usage examples.
6
+ # Usage example:
7
+ # result = Wiktionary.new("samochód").translate
8
+ # result.term # => "samochód"
9
+ # result.translations # => {"samochód"=>
10
+ # ["car", "automobile"]}
11
+ # result.examples # => {"samochód"=>["She drove her
12
+ # car to the mall.", "The conductor linked the cars to
13
+ # the locomotive.", "The 11:10 to London was operated by
14
+ # a 4-car diesel multiple unit", "From the front-most
15
+ # car of the subway, he filmed the progress
16
+ # through the tunnel.", "We ordered five hundred cars of gypsum.", ...]}
17
+
3
18
  require 'net/http'
4
19
  require 'nokogiri'
20
+
5
21
  require_relative 'result'
6
22
 
7
23
  class Wiktionary
8
24
  WIKI_URL = "http://en.wiktionary.org/wiki/"
9
25
  def initialize(word)
10
- if word.empty? then raise ArgumentError, "No word given." end
11
- escaped_word = word.downcase.tr(' ', '_')
12
- @result = Result.new(escaped_word)
13
- @uri = URI(URI.escape(WIKI_URL + escaped_word))
26
+ check_arguments(word)
27
+ initialize_instance_arguments(word)
14
28
  end
15
-
16
- #
17
- # Method returns an Result Object containing given word, translations, word usage examples.
18
- # Usage example:
19
- # result = Wiktionary.new("samochód").translate
20
- # result.term # => "samochód"
21
- # result.translations # => {"samochód"=>["car", "automobile"]}
22
- # result.examples # => {"samochód"=>["She drove her car to the mall.", "The conductor linked the cars to the locomotive.", "The 11:10 to London was operated by a 4-car diesel multiple unit", "From the front-most car of the subway, he filmed the progress through the tunnel.", "We ordered five hundred cars of gypsum.", ...]}
23
- #
29
+
24
30
  def translate
25
- req = Net::HTTP::Get.new(@uri.path)
26
- response, translations = nil, []
27
- Net::HTTP.start(@uri.host, @uri.port) do |http|
28
- response = http.request(req).body
29
-
30
- doc = Nokogiri::HTML(response)
31
- polish = false
32
- doc.css('div#mw-content-text h2 .mw-headline').each do |lang|
33
- #raise "Given word is not polish." if lang.content != 'Polish'
34
- (polish = true) if lang.content == 'Polish'
35
- end
36
- return @result if !polish
37
- #raise "Given word is not polish." if !polish
38
- doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
39
- translations.push(link.content)
40
- @result.add_translation(@result.term,link.content)
41
- end
31
+ translations = []
32
+
33
+ doc = get_html(@uri)
42
34
 
43
- translations.each do |item|
44
- escaped_item = item.tr(' ', '_')
45
- example = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + escaped_item)))
46
- example.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
47
- @result.add_example(@result.term,s.content)
48
- end
49
- end
35
+ return @result unless is_polish?(doc)
36
+
37
+ doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
38
+ @result.add_translation(@result.term, link.content)
50
39
  end
40
+
41
+
42
+ doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
43
+ translations.push(link.content)
44
+ end
45
+
46
+ examples_of_translations(@result, translations)
51
47
 
52
48
  @result
53
49
  end
50
+
51
+ private
52
+
53
+ def get_html(uri)
54
+ Nokogiri::HTML(Net::HTTP.get(uri))
55
+ end
56
+
57
+ def examples_of_translations(result, translations)
58
+ translations.each do |item|
59
+ example = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + item.tr(' ', '_'))))
60
+ example.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
61
+ result.add_example(result.term, s.content)
62
+ end
63
+ end
64
+ end
65
+
66
+ def initialize_instance_arguments(word)
67
+ @result = Result.new(word.downcase.tr(' ', '_'))
68
+ @uri = URI(URI.escape(WIKI_URL + word.downcase.tr(' ', '_')))
69
+ end
70
+
71
+ def check_arguments(word)
72
+ if word.empty? then raise ArgumentError.new("No word given.") end
73
+ end
74
+
75
+ def is_polish?(doc)
76
+ doc.css('div#mw-content-text h2 .mw-headline').any? do |lang|
77
+ lang.content == 'Polish'
78
+ end
79
+ end
80
+
54
81
  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.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: