dict 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dictpl.rb +6 -20
- data/lib/result.rb +12 -12
- data/lib/wiktionary.rb +61 -42
- metadata +1 -1
data/lib/dictpl.rb
CHANGED
@@ -7,13 +7,10 @@ class Dictpl
|
|
7
7
|
|
8
8
|
DICT_URL = "http://dict.pl/dict?word="
|
9
9
|
def initialize(word)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#puts 'word: ' + @word
|
16
|
-
#puts 'uri: ' + @uri.to_s
|
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)
|
17
14
|
end
|
18
15
|
|
19
16
|
#
|
@@ -26,16 +23,14 @@ class Dictpl
|
|
26
23
|
|
27
24
|
doc = Nokogiri::HTML(open(@uri))
|
28
25
|
doc.xpath('//td[@class="resWordCol"]/a').each do |node|
|
29
|
-
|
30
|
-
#puts node.text
|
26
|
+
@context_words << node.text
|
31
27
|
end
|
32
28
|
|
33
|
-
|
29
|
+
@mapped_words = {} # hash containing words with matched translations
|
34
30
|
|
35
31
|
@context_words.each_slice(2) do |word|
|
36
32
|
@mapped_words[word.first] = word.last
|
37
33
|
@result.add_translation(word.first, word.last)
|
38
|
-
puts word.first + ' : ' + word.last
|
39
34
|
end
|
40
35
|
|
41
36
|
@result
|
@@ -43,12 +38,3 @@ class Dictpl
|
|
43
38
|
|
44
39
|
end
|
45
40
|
|
46
|
-
|
47
|
-
# example of usage
|
48
|
-
|
49
|
-
word = ARGV[0]
|
50
|
-
word ||= ""
|
51
|
-
|
52
|
-
# comment lines below if You want to use rspec
|
53
|
-
translation = Dictpl.new word
|
54
|
-
puts translation.translate.translations
|
data/lib/result.rb
CHANGED
@@ -8,21 +8,11 @@ class Result
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def add_translation(term, translation)
|
11
|
-
|
12
|
-
@translations[term].push(translation)
|
13
|
-
else
|
14
|
-
@translations[term] = [translation]
|
15
|
-
end
|
16
|
-
self
|
11
|
+
add_result(@translations, term, translation)
|
17
12
|
end
|
18
13
|
|
19
14
|
def add_example(term, example)
|
20
|
-
|
21
|
-
@examples[term].push(example)
|
22
|
-
else
|
23
|
-
@examples[term] = [example]
|
24
|
-
end
|
25
|
-
self
|
15
|
+
add_result(@examples, term, example)
|
26
16
|
end
|
27
17
|
|
28
18
|
def each_translation
|
@@ -30,4 +20,14 @@ class Result
|
|
30
20
|
yield term, translation
|
31
21
|
end
|
32
22
|
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def add_result(hash, key, value)
|
26
|
+
if hash.has_key?(key)
|
27
|
+
hash[key].push(value)
|
28
|
+
else
|
29
|
+
hash.merge!({ key => [value] })
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
33
|
end
|
data/lib/wiktionary.rb
CHANGED
@@ -1,62 +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
|
-
|
26
|
+
check_arguments(word)
|
27
|
+
initialize_instance_arguments(word)
|
28
|
+
end
|
29
|
+
|
30
|
+
def translate
|
31
|
+
translations = []
|
32
|
+
|
33
|
+
doc = get_html(@uri)
|
12
34
|
|
13
|
-
|
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)
|
44
|
+
end
|
45
|
+
|
46
|
+
examples_of_translations(@result, translations)
|
47
|
+
|
48
|
+
@result
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
14
52
|
|
15
|
-
|
16
|
-
|
17
|
-
@uri = URI(URI.escape(WIKI_URL + escaped_word))
|
53
|
+
def get_html(uri)
|
54
|
+
Nokogiri::HTML(Net::HTTP.get(uri))
|
18
55
|
end
|
19
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
|
20
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
|
+
|
21
71
|
def check_arguments(word)
|
22
|
-
|
72
|
+
if word.empty? then raise ArgumentError.new("No word given.") end
|
23
73
|
end
|
24
|
-
#
|
25
|
-
# Method returns an Result Object containing given word, translations, word usage examples.
|
26
|
-
# Usage example:
|
27
|
-
# result = Wiktionary.new("samochód").translate
|
28
|
-
# result.term # => "samochód"
|
29
|
-
# result.translations # => {"samochód"=>["car", "automobile"]}
|
30
|
-
# 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.", ...]}
|
31
|
-
#
|
32
|
-
def translate
|
33
|
-
req = Net::HTTP::Get.new(@uri.path)
|
34
|
-
response, translations = nil, []
|
35
|
-
Net::HTTP.start(@uri.host, @uri.port) do |http|
|
36
|
-
response = http.request(req).body
|
37
|
-
|
38
|
-
doc = Nokogiri::HTML(response)
|
39
|
-
polish = false
|
40
|
-
doc.css('div#mw-content-text h2 .mw-headline').each do |lang|
|
41
|
-
#raise "Given word is not polish." if lang.content != 'Polish'
|
42
|
-
(polish = true) if lang.content == 'Polish'
|
43
|
-
end
|
44
|
-
return @result if !polish
|
45
|
-
#raise "Given word is not polish." if !polish
|
46
|
-
doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
|
47
|
-
translations.push(link.content)
|
48
|
-
@result.add_translation(@result.term,link.content)
|
49
|
-
end
|
50
74
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
example.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
|
55
|
-
@result.add_example(@result.term,s.content)
|
56
|
-
end
|
57
|
-
end
|
75
|
+
def is_polish?(doc)
|
76
|
+
doc.css('div#mw-content-text h2 .mw-headline').any? do |lang|
|
77
|
+
lang.content == 'Polish'
|
58
78
|
end
|
59
|
-
|
60
|
-
@result
|
61
79
|
end
|
80
|
+
|
62
81
|
end
|