Class: Daumdic
- Inherits:
-
Object
- Object
- Daumdic
- Defined in:
- lib/daumdic.rb
Class Method Summary collapse
-
.one_liner(input) ⇒ Object
다음사전에 단어를 검색한 후, 한줄짜리 결과를 출력한다.
Class Method Details
.one_liner(input) ⇒ Object
다음사전에 단어를 검색한 후, 한줄짜리 결과를 출력한다.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/daumdic.rb', line 17 def self.one_liner(input) return if input.nil? return if (input = input.strip).empty? uri = "https://dic.daum.net/search.do?#{URI.encode_www_form(q: input)}" doc = Nokogiri::HTML(URI.open(uri)) # Look for alternatives rel = doc.css('.link_speller').map(&:text).join(', ') return rel unless rel.empty? # Got some results box = doc.css('.search_box')[0] return if box.nil? word = box.css('.txt_cleansch').text word = box.css('.txt_searchword')[0]&.text if word.empty? meaning = box.css('.txt_search').map(&:text).join(', ') pronounce = box.css('.txt_pronounce').first&.text lang = box.parent.css('.tit_word').text if /^(.*)어사전$/.match(lang); lang = $1 end # Failed to parse daumdic return if meaning.empty? # Make a result message result = '' unless ['한국', '영', '일본', '한자사전'].include? lang result += "(#{lang}) " end if input != word result += "#{word} " end unless pronounce.nil? result += "#{pronounce} " end result += meaning end |