dict 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/translate +6 -3
  2. data/lib/dictpl.rb +34 -0
  3. data/lib/wiktionary.rb +8 -6
  4. metadata +3 -2
data/bin/translate CHANGED
@@ -7,13 +7,17 @@ require 'slop'
7
7
  if ARGV.empty? then puts "Please enter the word. (-h for help)" ; exit end
8
8
 
9
9
  opts = Slop.parse do
10
- banner "Usage: $translate word [Options] (-h for help)"
10
+ banner "Usage: $translate -w word [Options]"
11
+ on '-w', '--word', 'after this option is a word to translate'
11
12
  on '-h', '--help', 'help'
12
13
  on '-t', '--time', 'time in seconds', :as => :int
13
14
  on '-d', '--dict', 'dictonaries: all, wiki etc.'
14
15
  on '-s', '--status', 'status of API'
15
16
  end
16
17
 
18
+ puts opts.to_hash
19
+
20
+ =begin
17
21
  # problem with situation where first word after
18
22
  # translate is --help or -h ...
19
23
  unless ARGV[0].nil?
@@ -28,10 +32,9 @@ unless opts[:dict].nil?
28
32
  # todo: refactor get_response because it has not a dict
29
33
  # parameter
30
34
  end
31
-
35
+ =end
32
36
  puts opts if opts[:help]
33
37
  puts Dict::Translation.status unless opts[:status].nil?
34
38
 
35
39
 
36
40
 
37
-
data/lib/dictpl.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ class Dictpl
5
+
6
+ DICT_URL = "http://dict.pl/dict?word="
7
+ def initialize(word)
8
+ raise ArgumentError, "No given word" if word.empty?
9
+ @word = word
10
+ @uri = URI(URI.escape(DICT_URL + @word + "&lang=EN"))
11
+
12
+ #puts 'word: ' + @word
13
+ #puts 'uri: ' + @uri.to_s
14
+ end
15
+
16
+ def translate
17
+ res = []
18
+ doc = Nokogiri::HTML(open(@uri))
19
+ doc.xpath('//td[@class="resWordCol"]/a')[0..1].each do |node|
20
+ res = node.text
21
+ #puts res
22
+ end
23
+ res
24
+ end
25
+
26
+ end
27
+
28
+
29
+ # example of usage
30
+ word = ARGV[0]
31
+ word ||= ""
32
+
33
+ translation = Dictpl.new word
34
+ puts translation.translate
data/lib/wiktionary.rb CHANGED
@@ -5,27 +5,29 @@ class Wiktionary
5
5
  WIKI_URL = "http://en.wiktionary.org/wiki/"
6
6
  def initialize(word)
7
7
  if word.empty? then raise ArgumentError, "No word given." end
8
- @uri = URI(URI.escape(WIKI_URL + word.downcase))
8
+ escaped_word = word.downcase.tr(' ', '_')
9
+ @uri = URI(URI.escape(WIKI_URL + escaped_word))
9
10
  end
10
-
11
+
11
12
  def translate
12
13
  req = Net::HTTP::Get.new(@uri.path)
13
14
  response, translations, sentences = nil, [], []
14
15
  Net::HTTP.start(@uri.host, @uri.port) do |http|
15
16
  response = http.request(req).body
16
-
17
+
17
18
  doc = Nokogiri::HTML(response)
18
19
  doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
19
20
  translations.push link.content
20
21
  end
21
-
22
+
22
23
  translations.each do |item|
23
- sentence = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + item)))
24
+ escaped_item = item.tr(' ', '_')
25
+ sentence = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + escaped_item)))
24
26
  sentence.css('div#mw-content-text[lang=en] ol > li dl dd i').each do |s|
25
27
  sentences.push s.content
26
28
  end
27
29
  end
28
-
30
+
29
31
  end
30
32
  [translations, sentences]
31
33
  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.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-03 00:00:00.000000000 Z
13
+ date: 2012-07-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: slop
@@ -56,6 +56,7 @@ files:
56
56
  - lib/dict.rb
57
57
  - lib/google.rb
58
58
  - lib/wiktionary.rb
59
+ - lib/dictpl.rb
59
60
  - bin/translate
60
61
  homepage: https://github.com/Ragnarson/dict-gem
61
62
  licenses: []