dict 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dictpl.rb +24 -11
- data/lib/result.rb +4 -0
- data/lib/wiktionary.rb +40 -17
- metadata +3 -3
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
|
-
|
11
|
-
|
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
|
#
|
@@ -19,22 +18,36 @@ class Dictpl
|
|
19
18
|
# {"krowa"=>["cow"], "krowa bliska wycielenia"=>["freshen of cow"], ... }
|
20
19
|
#
|
21
20
|
def translate
|
21
|
+
|
22
|
+
# Array contatining all words alternately
|
23
|
+
# Example: ["krowa", "cow", "krowa bliska wycielenia", "freshen of cow", ... ]
|
22
24
|
@context_words = []
|
23
25
|
|
24
|
-
doc =
|
26
|
+
doc = get_html(@uri)
|
25
27
|
doc.xpath('//td[@class="resWordCol"]/a').each do |node|
|
26
28
|
@context_words << node.text
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
@mapped_words = {} # hash containing words with matched translations
|
30
32
|
|
31
33
|
@context_words.each_slice(2) do |word|
|
32
34
|
@mapped_words[word.first] = word.last
|
33
35
|
@result.add_translation(word.first, word.last)
|
34
|
-
end
|
35
|
-
|
36
|
+
end
|
36
37
|
@result
|
37
38
|
end
|
38
|
-
|
39
|
-
end
|
40
39
|
|
40
|
+
def check_arguments(word)
|
41
|
+
raise ArgumentError.new("No given word") if word.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
def initialize_instance_arguments(word)
|
45
|
+
@uri = URI(URI.escape(DICT_URL + word + "&lang=EN"))
|
46
|
+
@result = Result.new(word)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_html(uri)
|
50
|
+
Nokogiri::HTML(open(uri))
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/result.rb
CHANGED
data/lib/wiktionary.rb
CHANGED
@@ -21,31 +21,45 @@ require 'nokogiri'
|
|
21
21
|
require_relative 'result'
|
22
22
|
|
23
23
|
class Wiktionary
|
24
|
-
|
24
|
+
WIKI_EN = "http://en.wiktionary.org/wiki/"
|
25
|
+
WIKI_PL = "http://pl.wiktionary.org/wiki/"
|
25
26
|
def initialize(word)
|
26
27
|
check_arguments(word)
|
27
28
|
initialize_instance_arguments(word)
|
28
29
|
end
|
29
30
|
|
30
31
|
def translate
|
31
|
-
translations = []
|
32
|
-
|
32
|
+
translations = []
|
33
|
+
@uri = URI.parse(URI.escape("#{WIKI_EN}#{@word}"))
|
33
34
|
doc = get_html(@uri)
|
34
|
-
|
35
|
-
return @result unless is_polish?(doc)
|
36
35
|
|
37
|
-
doc
|
38
|
-
|
39
|
-
|
36
|
+
if is_polish?(doc)
|
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
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
41
|
+
doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
|
42
|
+
translations.push(link.content)
|
43
|
+
end
|
45
44
|
|
46
|
-
|
45
|
+
get_examples_of_translations_en(@result, translations, WIKI_EN)
|
47
46
|
|
48
|
-
|
47
|
+
@result
|
48
|
+
else
|
49
|
+
@uri = URI.parse(URI.escape("#{WIKI_PL}#{@word}"))
|
50
|
+
doc = get_html(@uri)
|
51
|
+
|
52
|
+
doc.css('div#mw-content-text dfn a').each do |link|
|
53
|
+
@result.add_translation(@result.term, link.content)
|
54
|
+
end
|
55
|
+
|
56
|
+
doc.css('div#mw-content-text dfn a').each do |link|
|
57
|
+
translations.push(link.content)
|
58
|
+
end
|
59
|
+
|
60
|
+
get_examples_of_translations_pl(@result, translations, WIKI_PL)
|
61
|
+
@result
|
62
|
+
end
|
49
63
|
end
|
50
64
|
|
51
65
|
private
|
@@ -54,18 +68,27 @@ class Wiktionary
|
|
54
68
|
Nokogiri::HTML(Net::HTTP.get(uri))
|
55
69
|
end
|
56
70
|
|
57
|
-
def
|
71
|
+
def get_examples_of_translations_en(result, translations, adres)
|
58
72
|
translations.each do |item|
|
59
|
-
example = Nokogiri::HTML(Net::HTTP.get(URI(
|
73
|
+
example = Nokogiri::HTML(Net::HTTP.get(URI(adres + item.tr(' ', '_'))))
|
60
74
|
example.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
|
61
75
|
result.add_example(result.term, s.content)
|
62
76
|
end
|
63
77
|
end
|
64
78
|
end
|
79
|
+
|
80
|
+
def get_examples_of_translations_pl(result, translations, adres)
|
81
|
+
translations.each do |item|
|
82
|
+
example = Nokogiri::HTML(Net::HTTP.get(adres + item))
|
83
|
+
example.css('div#mw-content-text[lang=pl] dl dd').each do |s|
|
84
|
+
result.add_example(result.term, s.content)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
65
88
|
|
66
89
|
def initialize_instance_arguments(word)
|
67
90
|
@result = Result.new(word.downcase.tr(' ', '_'))
|
68
|
-
@
|
91
|
+
@word = word
|
69
92
|
end
|
70
93
|
|
71
94
|
def check_arguments(word)
|
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.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-07-
|
15
|
+
date: 2012-07-08 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: slop
|
@@ -55,9 +55,9 @@ executables:
|
|
55
55
|
extensions: []
|
56
56
|
extra_rdoc_files: []
|
57
57
|
files:
|
58
|
-
- lib/dict.rb
|
59
58
|
- lib/result.rb
|
60
59
|
- lib/wiktionary.rb
|
60
|
+
- lib/dict.rb
|
61
61
|
- lib/dictpl.rb
|
62
62
|
- bin/translate
|
63
63
|
homepage: https://github.com/Ragnarson/dict-gem
|