dict 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dict.rb +22 -25
- data/lib/dictpl.rb +8 -19
- data/lib/wiktionary.rb +64 -37
- 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
|
-
|
5
|
-
|
6
|
-
require 'timeout'
|
4
|
+
require_relative 'wiktionary'
|
5
|
+
require_relative 'dictpl'
|
7
6
|
|
8
7
|
module Dict
|
9
|
-
class
|
10
|
-
def
|
11
|
-
|
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
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
12
|
-
|
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
|
-
|
11
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|