dict 0.3.2 → 0.3.3

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dict (0.3.1)
4
+ dict (0.3.3)
5
5
  nokogiri (~> 1.5.5)
6
6
  slop (~> 3.3.2)
7
7
 
@@ -54,7 +54,7 @@ Search WORD in dict, an open source dictionary aggregator.
54
54
  end
55
55
 
56
56
  MSG = "Usage: dict WORD [OPTIONS]\nTry `dict --help for more information.\n"
57
- VERSION = "dict version #{Dict::VERSION}\nSearch WORD in dict, an open source dictionary aggregator.\nCopyright (C) 2012 by Mateusz Czerwiński, Michał Podlecki,\nAleksander Gozdek, Rafał Ośko, Jan Borwin, Kosma Dunikowski\nMentors: Grzegorz Kołodziejski, Michał Kwiatkowski\nMade during intership at Ragnarson http://ragnarson.com\nHosted by Shelly Cloud https://shellycloud.com\nLicense: MIT\nHomepage: https://github.com/Ragnarson/dict-gem\nSources dictionaries: http://dict.pl, http://pl.wiktionary.org"
57
+ VERSION = "dict version #{Dict::VERSION}\nSearch WORD in dict, an open source dictionary aggregator.\nCopyright (C) 2012 by\nTrainees:\n Jan Borwin\n Mateusz Czerwiński\n Kosma Dunikowski\n Aleksander Gozdek\n Rafał Ośko\n Michał Podlecki\nMentors:\n Grzegorz Kołodziejski\n Michał Kwiatkowski\nLicense: MIT\nMade during intership at Ragnarson : http://ragnarson.com/\nHosted by Shelly Cloud : http://shellycloud.com/\nHomepage: http://github.com/Ragnarson/dict-gem/\nSources of dictionaries: http://wiktionary.org/\n http://glosbe.com/\n"
58
58
 
59
59
  # Returns only translations of the given word, without example sentences.
60
60
  def clean_translation(opts, word)
@@ -13,7 +13,7 @@ module Dict
13
13
  check_arguments(word)
14
14
  @translations = []
15
15
  @examples = []
16
- @word = word
16
+ @word = downcase_word(word)
17
17
  @result = Dict::Result.new(@word)
18
18
  end
19
19
 
@@ -52,5 +52,11 @@ module Dict
52
52
  @original = original
53
53
  end
54
54
  end
55
+
56
+ private
57
+ # Returns a word with all downcased letters, including polish
58
+ def downcase_word(word)
59
+ word.downcase.gsub(/[ĄĆĘŁŃÓŚŹŻ]/, 'Ą' => 'ą', 'Ć' => 'ć', 'Ę' => 'ę', 'Ł' => 'ł', 'Ń' => 'ń', 'Ó' => 'ó', 'Ś' => 'ś', 'Ź' => 'ź', 'Ż' => 'ż')
60
+ end
55
61
  end
56
62
  end
data/lib/dict/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dict
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -10,15 +10,20 @@ module Dict
10
10
 
11
11
  # Returns an Dict::Result object.
12
12
  def translate
13
- translations.each { |item| @result.add_translation(@result.term, item) }
13
+ translations.each do |translation|
14
+ @result.add_translation(@result.term, translation.gsub(/(\s[^|\s]+\|)/,' '))
15
+ examples(translation).each { |example| @result.add_example(translation, example) }
16
+ end
14
17
 
15
18
  @result
16
19
  end
17
20
 
18
21
  def get_html(url)
19
- Nokogiri::HTML(open(URI.encode(url)))
20
- rescue OpenURI::HTTPError
21
- raise Dictionary::ConnectError
22
+ begin
23
+ Nokogiri::HTML(open(URI.encode(url)))
24
+ rescue OpenURI::HTTPError
25
+ raise Dictionary::ConnectError
26
+ end
22
27
  end
23
28
 
24
29
  private
@@ -33,8 +38,10 @@ module Dict
33
38
 
34
39
  content_pl = get_html(url_pl).css('textarea#wpTextbox1').first
35
40
  if polish?(content_pl)
41
+ @is_polish = true
36
42
  extract_polish_translations(content_pl)
37
43
  else
44
+ @is_polish = false
38
45
  extract_english_translations(get_html(url_en).css('textarea#wpTextbox1').first.content)
39
46
  end
40
47
  end
@@ -58,8 +65,27 @@ module Dict
58
65
  translations ||= []
59
66
  end
60
67
 
61
- def get_examples(content, polish = true)
62
- # todo
68
+ def examples(word)
69
+ url_pl = "http://pl.wiktionary.org/w/index.php?title=#{word}&action=edit"
70
+
71
+ if @is_polish
72
+ extract_english_examples(word)
73
+ else
74
+ []
75
+ end
76
+ end
77
+
78
+ # Returns an array containing usage examples of translated polish word to english.
79
+ def extract_english_examples(word)
80
+ word = word.gsub(/\s+\(.+$/,'') || ''
81
+ url_en = "http://en.wiktionary.org/w/index.php?title=#{word}&action=edit"
82
+ examples = /Noun[^\{]+\{\{en\-noun[^=]+/.match(get_html(url_en.gsub('{word}',word)).css('textarea#wpTextbox1').first)
83
+ return [] unless examples.instance_of?(MatchData)
84
+ examples = examples[0].scan(/#: ''([^\n]+)\n/)
85
+ examples.map! do |translation|
86
+ translation[0].gsub(/'{2,}/,'')
87
+ end
88
+ examples
63
89
  end
64
90
  end
65
91
  end
@@ -42,4 +42,21 @@ describe Dict::Wiktionary do
42
42
  Dict::Wiktionary.new("dragon").translate.translations.should eq({'dragon' => ['smok']})
43
43
  end
44
44
  end
45
+
46
+ it "should return translations for word written with uppercase letters" do
47
+ result = Dict::Wiktionary.new('SaMoCHÓd').translate.translations
48
+ result.should eq({"samochód"=>["car", "automobile"]})
49
+ end
50
+
51
+ describe "#examples" do
52
+ it "should return a empty hash of usage examples to 'assdd' word" do
53
+ result = Dict::Wiktionary.new('field').translate.examples
54
+ result.should eq({})
55
+ end
56
+
57
+ it "should return a hash containing usage examples to 'kot' word" do
58
+ result = Dict::Wiktionary.new('kot').translate.examples
59
+ result.should eq({"cat" => ["No room to swing a cat."]})
60
+ end
61
+ end
45
62
  end