dict 0.1.0 → 0.1.1

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 +4 -2
  2. data/lib/dictpl.rb +19 -8
  3. data/lib/wiktionary.rb +37 -64
  4. metadata +1 -1
data/lib/dict.rb CHANGED
@@ -8,7 +8,7 @@ require 'timeout'
8
8
  module Dict
9
9
  class Translation
10
10
  def initialize(word)
11
- @word = word#ARGV[0]
11
+ @word = word
12
12
  end
13
13
 
14
14
  def self.status(adres = "http://dict-app-staging.shellyapp.com/")
@@ -25,10 +25,12 @@ module Dict
25
25
  "Success" unless res.empty?
26
26
  end
27
27
 
28
- rescue #=> ex
28
+ rescue
29
29
  puts "Timeout for the query."
30
30
  {:code => 408, :describe => "Request Timeout"}
31
31
  end
32
32
  end
33
33
  end
34
+ #t = Translation.new "samochód"
35
+ #puts Translation.status
34
36
  end
data/lib/dictpl.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require 'open-uri'
2
3
  require 'nokogiri'
3
4
  require_relative 'result'
@@ -6,11 +7,9 @@ require_relative 'result'
6
7
  class Dictpl
7
8
 
8
9
  DICT_URL = "http://dict.pl/dict?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)
10
+ def initialize(word)
11
+ check_arguments(word)
12
+ initialize_instance_arguments(word)
14
13
  end
15
14
 
16
15
  #
@@ -31,10 +30,22 @@ class Dictpl
31
30
  @context_words.each_slice(2) do |word|
32
31
  @mapped_words[word.first] = word.last
33
32
  @result.add_translation(word.first, word.last)
34
- end
35
-
33
+ end
36
34
  @result
37
35
  end
38
-
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
39
46
  end
40
47
 
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,81 +1,54 @@
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
-
18
3
  require 'net/http'
19
4
  require 'nokogiri'
20
-
21
5
  require_relative 'result'
22
6
 
23
7
  class Wiktionary
24
8
  WIKI_URL = "http://en.wiktionary.org/wiki/"
25
9
  def initialize(word)
26
- check_arguments(word)
27
- initialize_instance_arguments(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))
28
14
  end
29
-
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
+ #
30
24
  def translate
31
- translations = []
32
-
33
- doc = get_html(@uri)
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
34
42
 
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)
39
- end
40
-
41
-
42
- doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
43
- translations.push(link.content)
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
44
50
  end
45
-
46
- examples_of_translations(@result, translations)
47
51
 
48
52
  @result
49
53
  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
-
81
54
  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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: