dict 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/translate CHANGED
@@ -1,33 +1,7 @@
1
- #!/usr/bin/env ruby
2
1
  # -*- coding: utf-8 -*-
3
2
 
4
- require 'dict'
5
- require 'slop'
6
-
7
- if ARGV.empty? then puts "Please enter the word. (-h for help)" ; exit end
8
-
9
- opts = Slop.parse do
10
- banner "Usage: $translate -w word [Options]"
11
- on '-w', :word=, 'after this option is a word to translate'
12
- on '-h', :help=, 'help', :argument => :optional
13
- on '-s', :services=, 'available services', :argument => :optional
14
- #on '-t', :time=, 'time in seconds, default: 300 seconds', :as => :int
15
- on '-d', :dict=, 'dictionaries: wiktionary, dictpl; default is all dictionaries'
16
- end
17
-
18
- # translation
19
- if opts.word?
20
- if opts.dict?
21
- puts Dict.get_single_dictionary_translations(opts[:word],opts[:dict])
22
- else
23
- puts Dict.get_all_dictionaries_translations(opts[:word])
24
- end
25
- end
26
-
27
- # help
28
- puts opts if opts.help?
29
-
30
- # services
31
- puts Dict.available_services if opts.services?
3
+ require_relative '../lib/module_main.rb'
32
4
 
5
+ opts = Hash.new
33
6
 
7
+ Main::main(opts)
data/lib/dict.rb CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/ruby -w
2
1
  # -*- coding: utf-8 -*-
3
2
 
4
3
  require_relative 'wiktionary'
5
4
  require_relative 'dictpl'
5
+ require 'json'
6
6
 
7
7
  module Dict
8
8
  class << self
@@ -12,20 +12,34 @@ module Dict
12
12
  available_services.each do |service|
13
13
  dictionaries[service] = get_single_dictionary_translations(word, service)
14
14
  end
15
-
16
15
  dictionaries
17
16
  end
18
17
 
18
+ def print_all_dictionaries_translations(word)
19
+ end
20
+
19
21
  def get_single_dictionary_translations(word, service)
20
22
  case service
21
23
  when 'wiktionary'
22
- Wiktionary.new(word).translate
24
+ Wiktionary.new(word, WIKI_URL).translate
23
25
  when 'dictpl'
24
- Dictpl.new(word).translate
25
- else 'There\'s no such dictionary.'
26
+ Dictpl.new(word, DICT_URL).translate
27
+ else Dictionary.message
28
+ end
29
+ end
30
+
31
+ def print_single_dictionary_translations(word, service)
32
+ obj = get_single_dictionary_translations(word, service)
33
+ hash = obj.translate
34
+ hash.each do |k, v|
35
+ puts "#{k} - #{v}"
26
36
  end
27
37
  end
28
38
 
39
+ def to_json(hash)
40
+ hash.to_json
41
+ end
42
+
29
43
  def available_services
30
44
  ['wiktionary', 'dictpl']
31
45
  end
data/lib/dictionary.rb ADDED
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'open-uri'
4
+
5
+ class Dictionary
6
+ attr_accessor :translations, :examples
7
+ def initialize(word, url)
8
+ check_arguments(word)
9
+ @translations = []
10
+ @examples = []
11
+ @uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
12
+ end
13
+
14
+ def make_hash_results(arr)
15
+ hash = arr.each_slice(2).inject({}) do |h, (key, value)|
16
+ if h.has_key?(key)
17
+ h[key].push(value) ; h
18
+ else
19
+ h[key] = [value] ; h
20
+ end
21
+ end
22
+ @translations, @examples = hash.keys, hash.values
23
+ hash
24
+ end
25
+
26
+ def check_arguments(word)
27
+ raise ArgumentError.new("No given word") if word.empty?
28
+ end
29
+
30
+ def self.message
31
+ 'There\'s no such dictionary in database.'
32
+ end
33
+ end
data/lib/dictpl.rb CHANGED
@@ -1,53 +1,17 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'open-uri'
3
- require 'nokogiri'
4
- require_relative 'result'
5
2
 
3
+ require 'nokogiri'
4
+ require_relative 'dictionary'
6
5
 
7
- class Dictpl
6
+ DICT_URL = 'http://dict.pl/dict?word='
8
7
 
9
- DICT_URL = "http://dict.pl/dict?word="
10
- def initialize(word)
11
- check_arguments(word)
12
- initialize_instance_arguments(word)
13
- end
14
-
15
- #
16
- # Method returns hash with translations in many context for a word.
17
- # Example: for the word 'krowa' it returns translations in such a form:
18
- # {"krowa"=>["cow"], "krowa bliska wycielenia"=>["freshen of cow"], ... }
19
- #
8
+ class Dictpl < Dictionary
9
+ # Method returns hash with translations as keys and examples of using words as values
20
10
  def translate
21
-
22
- # Array contatining all words alternately
23
- # Example: ["krowa", "cow", "krowa bliska wycielenia", "freshen of cow", ... ]
24
- @context_words = []
25
-
26
- doc = get_html(@uri)
27
- doc.xpath('//td[@class="resWordCol"]/a').each do |node|
28
- @context_words << node.text
11
+ context_words = []
12
+ Nokogiri::HTML(open(@uri)).xpath('//td[@class="resWordCol"]/a').each do |node|
13
+ context_words << node.text
29
14
  end
30
-
31
- @mapped_words = {} # hash containing words with matched translations
32
-
33
- @context_words.each_slice(2) do |word|
34
- @mapped_words[word.first] = word.last
35
- @result.add_translation(word.first, word.last)
36
- end
37
- @result
38
- end
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)
15
+ make_hash_results(context_words)
47
16
  end
48
-
49
- def get_html(uri)
50
- Nokogiri::HTML(open(uri))
51
- end
52
-
53
17
  end
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'dict'
4
+ require 'slop'
5
+ require 'timeout'
6
+
7
+ module Main
8
+ def self.check_parameters?
9
+ ARGV.empty?
10
+ end
11
+
12
+ def self.parse_parameters
13
+ opts = Slop.parse do
14
+ banner "Usage: $translate -w word [Options]"
15
+ on '-w', :word=, 'after this option is a word to translate'
16
+ on '-h', :help=, 'help', :argument => :optional
17
+ on '-t', :time=, 'time in seconds, default: 300', :as => :int
18
+ on '-d', :dict=, 'wiktionary, dictpl', :argument => :optional
19
+ end
20
+ opts
21
+ end
22
+
23
+ def self.get_translations(opts)
24
+
25
+ if opts.word?
26
+ begin
27
+ Timeout::timeout(opts[:time].to_i || 300) do
28
+ if opts.dict? && opts[:dict] != nil
29
+ Dict.get_single_dictionary_translations(opts[:word],opts[:dict])
30
+ else
31
+ Dict.get_all_dictionaries_translations(opts[:word])
32
+ end
33
+ end
34
+
35
+ rescue
36
+ "Timeout for the query."
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ def self.main(opts)
43
+
44
+ if check_parameters? == true
45
+ puts "Please enter the word. (-h for help)"
46
+ exit
47
+ end
48
+
49
+ begin
50
+ opts = parse_parameters
51
+ rescue Slop::MissingArgumentError
52
+ puts "Missing argument"
53
+ exit
54
+ end
55
+
56
+ puts get_translations(opts)
57
+
58
+ begin
59
+ puts opts if opts.help?
60
+ puts Dict.available_services if opts.dict?
61
+ rescue
62
+ nil
63
+ end
64
+ end
65
+
66
+ end
67
+
data/lib/wiktionary.rb CHANGED
@@ -1,104 +1,24 @@
1
- #!/usr/bin/ruby -w
2
1
  # -*- coding: utf-8 -*-
3
2
 
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
- require 'net/http'
19
3
  require 'nokogiri'
4
+ require_relative 'dictionary'
20
5
 
21
- require_relative 'result'
6
+ WIKI_URL = 'http://en.wiktionary.org/wiki/'
22
7
 
23
- class Wiktionary
24
- WIKI_EN = "http://en.wiktionary.org/wiki/"
25
- WIKI_PL = "http://pl.wiktionary.org/wiki/"
26
- def initialize(word)
27
- check_arguments(word)
28
- initialize_instance_arguments(word)
29
- end
30
-
8
+ class Wiktionary < Dictionary
9
+ # Method returns hash with translations as keys and examples of using words as values
31
10
  def translate
32
- translations = []
33
- @uri = URI.parse(URI.escape("#{WIKI_EN}#{@word}"))
34
- doc = get_html(@uri)
35
-
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
-
41
- doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
42
- translations.push(link.content)
11
+ context_words = []
12
+ url = 'http://en.wiktionary.org/wiki/'
13
+ get_html(@uri).css('p + ol li a').each do |node|
14
+ get_html(url + node.text.tr(' ', '_')).css('p + ol > li dl dd').each do |example|
15
+ context_words << node.text << example.text
43
16
  end
44
-
45
- get_examples_of_translations_en(@result, translations, WIKI_EN)
46
-
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
17
  end
63
- end
18
+ make_hash_results(context_words)
19
+ end
64
20
 
65
- private
66
-
67
- def get_html(uri)
68
- Nokogiri::HTML(Net::HTTP.get(uri))
69
- end
70
-
71
- def get_examples_of_translations_en(result, translations, adres)
72
- translations.each do |item|
73
- example = Nokogiri::HTML(Net::HTTP.get(URI(adres + item.tr(' ', '_'))))
74
- example.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
75
- result.add_example(result.term, s.content)
76
- end
77
- end
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
88
-
89
- def initialize_instance_arguments(word)
90
- @result = Result.new(word.downcase.tr(' ', '_'))
91
- @word = word
92
- end
93
-
94
- def check_arguments(word)
95
- if word.empty? then raise ArgumentError.new("No word given.") end
96
- end
97
-
98
- def is_polish?(doc)
99
- doc.css('div#mw-content-text h2 .mw-headline').any? do |lang|
100
- lang.content == 'Polish'
101
- end
21
+ def get_html(url)
22
+ Nokogiri::HTML(open(url))
102
23
  end
103
-
104
24
  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.5
4
+ version: 0.1.6
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-08 00:00:00.000000000 Z
15
+ date: 2012-07-10 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: slop
@@ -56,9 +56,10 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
  files:
58
58
  - lib/dict.rb
59
- - lib/result.rb
59
+ - lib/dictionary.rb
60
60
  - lib/wiktionary.rb
61
61
  - lib/dictpl.rb
62
+ - lib/module_main.rb
62
63
  - bin/translate
63
64
  homepage: https://github.com/Ragnarson/dict-gem
64
65
  licenses: []
data/lib/result.rb DELETED
@@ -1,37 +0,0 @@
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
- add_result(@translations, term, translation)
12
- end
13
-
14
- def add_example(term, example)
15
- add_result(@examples, term, example)
16
- end
17
-
18
- def each_translation
19
- @translations.each_pair do |term,translation|
20
- yield term, translation
21
- end
22
- end
23
-
24
- def to_s
25
- "#{@term}\n#{@dictionaries}\n#{@translations}\n#{@examples}\n"
26
- end
27
-
28
- private
29
- def add_result(hash, key, value)
30
- if hash.has_key?(key)
31
- hash[key].push(value)
32
- else
33
- hash.merge!({ key => [value] })
34
- end
35
- self
36
- end
37
- end