dict 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/dict.rb CHANGED
@@ -7,8 +7,8 @@ require 'timeout'
7
7
 
8
8
  module Dict
9
9
  class Translation
10
- def initialize
11
- @word = ARGV[0]
10
+ def initialize(word)
11
+ @word = word#ARGV[0]
12
12
  end
13
13
 
14
14
  def self.status(adres = "http://dict-app-staging.shellyapp.com/")
@@ -25,11 +25,10 @@ module Dict
25
25
  "Success" unless res.empty?
26
26
  end
27
27
 
28
- rescue => ex
28
+ rescue #=> ex
29
29
  puts "Timeout for the query."
30
30
  {:code => 408, :describe => "Request Timeout"}
31
- end
32
31
  end
32
+ end
33
33
  end
34
34
  end
35
-
data/lib/dictpl.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'open-uri'
2
2
  require 'nokogiri'
3
+ require_relative 'result'
4
+
3
5
 
4
6
  class Dictpl
5
7
 
@@ -7,28 +9,46 @@ class Dictpl
7
9
  def initialize(word)
8
10
  raise ArgumentError, "No given word" if word.empty?
9
11
  @word = word
10
- @uri = URI(URI.escape(DICT_URL + @word + "&lang=EN"))
12
+ @uri = URI(URI.escape(DICT_URL + @word + "&lang=EN"))
13
+ @result = Result.new(word)
11
14
 
12
15
  #puts 'word: ' + @word
13
16
  #puts 'uri: ' + @uri.to_s
14
17
  end
15
-
18
+
19
+ #
20
+ # Method returns hash with translations in many context for a word.
21
+ # Example: for the word 'krowa' it returns translations in such a form:
22
+ # {"krowa"=>["cow"], "krowa bliska wycielenia"=>["freshen of cow"], ... }
23
+ #
16
24
  def translate
17
- res = []
25
+ @context_words = []
26
+
18
27
  doc = Nokogiri::HTML(open(@uri))
19
- doc.xpath('//td[@class="resWordCol"]/a')[0..1].each do |node|
20
- res = node.text
21
- #puts res
28
+ doc.xpath('//td[@class="resWordCol"]/a').each do |node|
29
+ @context_words << node.text
30
+ #puts node.text
22
31
  end
23
- res
32
+
33
+ @mapped_words = {} # hash containing words with matched translations
34
+
35
+ @context_words.each_slice(2) do |word|
36
+ @mapped_words[word.first] = word.last
37
+ @result.add_translation(word.first, word.last)
38
+ puts word.first + ' : ' + word.last
39
+ end
40
+
41
+ @result
24
42
  end
25
-
43
+
26
44
  end
27
45
 
28
46
 
29
47
  # example of usage
48
+
30
49
  word = ARGV[0]
31
- word ||= ""
50
+ word ||= ""
32
51
 
52
+ # comment lines below if You want to use rspec
33
53
  translation = Dictpl.new word
34
- puts translation.translate
54
+ puts translation.translate.translations
data/lib/result.rb ADDED
@@ -0,0 +1,33 @@
1
+ class Result
2
+ attr_accessor :term, :dictionaries, :translations, :examples
3
+ def initialize(term, dictionaries = nil)
4
+ @term = term
5
+ @dictionaries = dictionaries
6
+ @translations = {}
7
+ @examples = {}
8
+ end
9
+
10
+ def add_translation(term, translation)
11
+ if @translations[term]
12
+ @translations[term].push(translation)
13
+ else
14
+ @translations[term] = [translation]
15
+ end
16
+ self
17
+ end
18
+
19
+ def add_example(term, example)
20
+ if @examples[term]
21
+ @examples[term].push(example)
22
+ else
23
+ @examples[term] = [example]
24
+ end
25
+ self
26
+ end
27
+
28
+ def each_translation
29
+ @translations.each_pair do |term,translation|
30
+ yield term, translation
31
+ end
32
+ end
33
+ end
data/lib/wiktionary.rb CHANGED
@@ -1,42 +1,54 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- coding: utf-8 -*-
1
3
  require 'net/http'
2
4
  require 'nokogiri'
5
+ require_relative 'result'
3
6
 
4
7
  class Wiktionary
5
8
  WIKI_URL = "http://en.wiktionary.org/wiki/"
6
9
  def initialize(word)
7
10
  if word.empty? then raise ArgumentError, "No word given." end
8
11
  escaped_word = word.downcase.tr(' ', '_')
12
+ @result = Result.new(escaped_word)
9
13
  @uri = URI(URI.escape(WIKI_URL + escaped_word))
10
14
  end
11
15
 
12
16
  #
13
- # Method returns an array of translations and word usage examples
14
- # For instance to given word 'samochód' it returns:
15
- # [["car","automobile"],["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"]
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 # => ["car","automobile"]
22
+ # result.examples # => ["She drove her car to the mall.","The conductor linked the cars to the locomotive.", ...]
16
23
  #
17
24
  def translate
18
25
  req = Net::HTTP::Get.new(@uri.path)
19
- response, translations, sentences = nil, [], []
26
+ response, translations = nil, []
20
27
  Net::HTTP.start(@uri.host, @uri.port) do |http|
21
28
  response = http.request(req).body
22
29
 
23
30
  doc = Nokogiri::HTML(response)
24
- doc.css('div#mw-content-text h2:first .mw-headline').each do |lang|
25
- raise "Given word is not polish." if lang.content != 'Polish'
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'
26
35
  end
36
+ return @result if !polish
37
+ #raise "Given word is not polish." if !polish
27
38
  doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
28
- translations.push link.content
39
+ translations.push(link.content)
40
+ @result.add_translation(@result.term,link.content)
29
41
  end
30
42
 
31
- translations.each do |item|
43
+ translations.each do |item|
32
44
  escaped_item = item.tr(' ', '_')
33
- sentence = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + escaped_item)))
34
- sentence.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
35
- sentences.push s.content
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)
36
48
  end
37
49
  end
38
-
39
50
  end
40
- [translations, sentences]
51
+
52
+ @result
41
53
  end
42
54
  end
metadata CHANGED
@@ -1,12 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Aleksander Gozdek
9
9
  - Mateusz Czerwinski
10
+ - Michał Podlecki
11
+ - Rafał Ośko
10
12
  autorequire:
11
13
  bindir: bin
12
14
  cert_chain: []
@@ -54,7 +56,7 @@ extensions: []
54
56
  extra_rdoc_files: []
55
57
  files:
56
58
  - lib/dict.rb
57
- - lib/google.rb
59
+ - lib/result.rb
58
60
  - lib/wiktionary.rb
59
61
  - lib/dictpl.rb
60
62
  - bin/translate
data/lib/google.rb DELETED
@@ -1,19 +0,0 @@
1
- require 'net/https'
2
- require 'uri'
3
- require 'json'
4
-
5
- API_KEY = 'AIzaSyB2EQUnNtZycF_xyoyOSm0QJU0dfMGEr44'
6
-
7
- class GoogleTranslator
8
- def initialize(uri)
9
- @uri = URI(uri)
10
- @req = Net::HTTP::Get.new(@uri.path << "?" << @uri.query)
11
- Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
12
- p JSON.parse(http.request(@req).body)
13
- end
14
- end
15
- end
16
-
17
- word = ARGV[0]
18
-
19
- GoogleTranslator.new("https://www.googleapis.com/language/translate/v2?key=#{API_KEY}&q=#{word}&source=pl&target=en")