Sha256: 418f1bade2ebb986e794d4224622b99d12006c57ba074c87a2b1b78deb977b47
Contents?: true
Size: 1.92 KB
Versions: 2
Compression:
Stored size: 1.92 KB
Contents
require 'nokogiri' require 'open-uri' require 'cgi' require "bing_dictionary/version" module BingDictionary class Base attr_reader :doc def self.translate(word) self.new(word).translate rescue SocketError warn 'Connection failed! Please check your network.' exit 1 end def initialize(word) file = open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}") @doc = Nokogiri::HTML(file) end def translate head if doc.at_css('#headword') machine if doc.at_css('.smt_hw') sentence if doc.at_css('#sentenceSeg .se_li') guess if doc.at_css('.dym_area') end def head puts doc.at_css('#headword').text puts doc.at_css('.hd_tf_lh').text.green puts doc.at_css('.hd_area').next_sibling.css('li').each do |li| puts li.at_css('.pos').text.fixed(5).blue + li.at_css('.def').text end end def machine puts doc.at_css('.smt_hw').text puts doc.at_css('.p1-10').text puts doc.at_css('.p1-11').text.green end def sentence puts doc.css('#sentenceSeg .se_li').first(4).map do |li| puts li.css('.sen_en').text puts li.css('.sen_cn').text puts end end def guess puts content = doc.at_css('.content') puts content.at_css('.p2-2').text puts content.css('.dym_area').each do |area| puts area.at_css('.df_wb_a').text puts area.css('.df_wb_c').map(&:text) puts end end end end class String COLORS = %w(black red green yellow blue magenta cyan white) COLORS.each_with_index do |color, idx| define_method color do "\e[3#{idx}m" << self.to_s << "\e[0m" end define_method "#{color}_bg" do "\e[4#{idx}m" << self.to_s << "\e[0m" end end def fixed(width) width = width - self.each_char.count { |c| c =~ /\p{Han}/ } width > 0 ? ("%-#{width}s" % self) : self end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
bing_dictionary-0.1.1 | lib/bing_dictionary.rb |
bing_dictionary-0.1.0 | lib/bing_dictionary.rb |