bing_dictionary 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8d70a6f8f513205be899a6c608857494b9f6f0f1
4
- data.tar.gz: 64c7891d7be3db78dbb7b03af6e04149bc5766ff
2
+ SHA256:
3
+ metadata.gz: 81839a4a2c0db47ca6748695874d665d5da117275e8008ebb9d30d258e794ca8
4
+ data.tar.gz: a907576387cbf7d109bcc14ff97c9ee97d9e3da695b8ad6427345952c0a3b58e
5
5
  SHA512:
6
- metadata.gz: 4c3f39b00a791203d4a73a2ea610a3de370a8722d47d24fb40cb2b6f73bf7b2d4d8e621fef6187aef8ba1a51778450dada0b0cb5cf0d2c25089e73ebc2b36657
7
- data.tar.gz: 96be2ef7575babb693526e0671054734179512f7dc1322d7ca0237abc0925381b0383fffc2d94f765cfc4c994a58ae30e69079fc1a34c8a95bd8c283bc101220
6
+ metadata.gz: 342c0525ed961f9d6b4c2f4c7aa71a90f1e57ac980d37cabc89d625b2c38211e6026e175ef2df3d9866a92494442084f2b26501c6faff2e8e679c9c141708ff3
7
+ data.tar.gz: cef434ed6cc6b22ce04e0c9b777943e4af2c29819984ead8826e48ac08ac170c6b80691f9f980e815d40d4877c7d814380140e5863de6a6339eaf316a21adaca
data/README.md CHANGED
@@ -1,27 +1,13 @@
1
1
  # BingDictionary
2
2
 
3
- 这是一个ruby脚本,用来在命令行中直接查询单词含义,免去打开软件或浏览器的麻烦。
4
-
5
3
  Here is a command line dictionary written with ruby that can
6
- translate English to Chinese.
4
+ translate English to Chinese or vice versa.
7
5
 
8
6
  It uses http://cn.bing.com/dict/ to query.
9
7
  ![DEMO ](./dict.png)
10
8
 
11
9
  ## Installation
12
10
 
13
- Add this line to your application's Gemfile:
14
-
15
- ```ruby
16
- gem 'bing_dictionary'
17
- ```
18
-
19
- And then execute:
20
-
21
- $ bundle
22
-
23
- Or install it yourself as:
24
-
25
11
  $ gem install bing_dictionary
26
12
 
27
13
  ## Usage
@@ -29,23 +15,18 @@ A `dict` runable bin file provided.
29
15
 
30
16
  DEMO:
31
17
 
32
- dict cake
33
- dict 蛋糕
34
-
35
- You can make it work tegother with Vim by add follows to $HOME/.vimrc
36
-
37
- nnoremap <leader>d :!dict <cword><CR>
38
- vnoremap <leader>d y:!dict <c-r>"<CR>
18
+ $ dict cake
19
+ $ dict 蛋糕
39
20
 
40
21
  ## Support long sentence
41
22
  Now long sentence is supported
42
23
 
43
- dict 你好吗
44
- dict How old are you
24
+ $ dict 你好吗
25
+ $ dict How old are you
45
26
 
46
27
  ## Support fuzzy query
47
28
 
48
- dict hexxo
29
+ $ dict hexxo
49
30
 
50
31
  您要找的是不是
51
32
 
@@ -55,20 +36,44 @@ Now long sentence is supported
55
36
  hexode六极管
56
37
  ...
57
38
 
58
- ## For Mac user
59
- You can open your local dictionary.app with `open dict://someword`. It may be faster.
60
- Add following to your vimrc to make it work in vim:
39
+ ## Support Chinese to English
40
+
41
+ $ dict 单片机
42
+ 单片机
43
+
44
+ 网络 MCU; Single Chip Microcomputer; Microcontroller
45
+
46
+ The monolithic integrated circuit occurs the explanation , did not understand may have a look !
47
+ 单片机发生的讲解,不懂得可以看看!
48
+ ...
49
+
50
+ ## Local cache supported
51
+ All queried word default cached to ~/.bing_dictionary.db.
52
+ So it's much faster when you query the word again.
53
+
54
+ [Here](https://github.com/first20hours/google-10000-english) have the most common English words.
55
+ You can cache them all with following script:
61
56
 
62
- nnoremap <leader>dd :!open dic://<cword><CR>
57
+ curl https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt | while read line; do dict $line; done
58
+
59
+ ## More options
60
+
61
+ $ dict --help
62
+ Example: dict hello
63
+ -p, --[no-]pronounce Pronounce the word
64
+ -c, --[no-]cache Use cache from ~/.bing_dictionary.db (Default on)
65
+ -j, --jump Jump to web page
66
+ -v, --version Show the version
67
+
68
+ ## Work with vim
69
+ You can make it work tegother with Vim with [bing_dictionary.vim](https://github.com/lingceng/bing_dictionary.vim)
63
70
 
64
71
  ## Thanks
65
72
  Heavily borrowed from [Command-Line-Youdao-Dictionary](https://github.com/qhwa/Command-Line-Youdao-Dictionary)
66
73
 
67
74
  ## Development
68
75
 
69
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
-
71
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+ bundle exec ./bin/dict --no-cache hello
72
77
 
73
78
  ## Contributing
74
79
 
data/bin/dict CHANGED
@@ -1,15 +1,54 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bing_dictionary"
4
3
  require 'optparse'
5
4
 
6
5
  options = {}
7
- OptionParser.new do |opts|
8
- opts.banner = "Usage: dict [-p] hello"
9
-
6
+ option_parser = OptionParser.new do |opts|
7
+ opts.banner = "Example: dict hello"
10
8
  opts.on("-p", "--[no-]pronounce", "Pronounce the word") do |v|
11
9
  options[:pronounce] = v
12
10
  end
13
- end.parse!
11
+ opts.on("-c", "--[no-]cache", "Use cache from ~/.bing_dictionary.db (Default on)") do |v|
12
+ options[:cache] = v
13
+ end
14
+ opts.on("-j", "--jump", "Jump to web page") do |v|
15
+ options[:jump] = v
16
+ end
17
+ opts.on("-v", "--version", "Show the version") do |v|
18
+ options[:version] = v
19
+ end
20
+ end
21
+
22
+ if ARGV.empty?
23
+ option_parser.parse('--help')
24
+ else
25
+ option_parser.parse!
26
+ word = ARGV.join(' ')
27
+ if options[:version]
28
+ require "bing_dictionary"
29
+ puts BingDictionary::VERSION
30
+ elsif options[:jump]
31
+ `open http://cn.bing.com/dict/?q=#{CGI::escape(word)}`
32
+ else
33
+ require 'dbm'
34
+ DBM.open(File.join(Dir.home, '.bing_dictionary')) do |cache|
35
+ if cache.key?(word) && options[:cache] != false
36
+ puts cache[word]
37
+ else
38
+ require "bing_dictionary"
39
+
40
+ backup = $stdout
41
+ $stdout = StringIO.new
42
+ BingDictionary::Base.translate(word, options)
43
+ out = $stdout.string
44
+ $stdout = backup
45
+
46
+ if out && out.length > 0
47
+ cache[word] = out
48
+ puts out
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
14
54
 
15
- BingDictionary::Base.translate(ARGV.join(' '), options) unless ARGV.empty?
@@ -8,9 +8,10 @@ Gem::Specification.new do |spec|
8
8
  spec.version = BingDictionary::VERSION
9
9
  spec.authors = ["lingceng"]
10
10
  spec.email = ["lingceng2008@gmail.com"]
11
+ spec.licenses = "MIT"
11
12
 
12
13
  spec.summary = %q{Command line dictionary grabing http://cn.bing.com/}
13
- spec.description = %q{Command line dictionary grabing http://cn.bing.com/}
14
+ spec.description = %q{With local cache and can work with vim}
14
15
  spec.homepage = "https://github.com/lingceng/bing_dictionary"
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -18,8 +19,8 @@ Gem::Specification.new do |spec|
18
19
  spec.executables = 'dict'
19
20
  spec.require_paths = ["lib"]
20
21
 
21
- spec.add_development_dependency "bundler", "~> 1.11"
22
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler", ">= 2.2.10"
23
+ spec.add_development_dependency "rake", ">= 12.3.3"
23
24
  spec.add_development_dependency "minitest", "~> 5.0"
24
25
 
25
26
  spec.add_dependency "nokogiri", "~> 1.6"
@@ -5,7 +5,7 @@ require "bing_dictionary/version"
5
5
 
6
6
  module BingDictionary
7
7
  class Base
8
- attr_reader :doc
8
+ attr_accessor :doc, :options, :word
9
9
 
10
10
  def self.translate(word, options = {})
11
11
  self.new(word, options).translate
@@ -15,9 +15,10 @@ module BingDictionary
15
15
  end
16
16
 
17
17
  def initialize(word, options = {})
18
- file = open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}")
19
- @doc = Nokogiri::HTML(file)
20
- @options = options
18
+ file = URI.open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}")
19
+ self.word = word
20
+ self.doc = Nokogiri::HTML(file)
21
+ self.options = options
21
22
  end
22
23
 
23
24
  def translate
@@ -25,15 +26,15 @@ module BingDictionary
25
26
  machine if doc.at_css('.smt_hw')
26
27
  sentence if doc.at_css('#sentenceSeg .se_li')
27
28
  guess if doc.at_css('.dym_area')
28
- pronounce if doc.at_css('#headword') && @options[:pronounce]
29
+ pronounce if doc.at_css('#headword') && options[:pronounce]
29
30
  end
30
31
 
31
32
  def head
32
33
  puts doc.at_css('#headword').text
33
- puts doc.at_css('.hd_tf_lh').text.green
34
+ puts with_color(doc.at_css('.hd_tf_lh').text, :green)
34
35
  puts
35
36
  doc.at_css('.hd_area').next_sibling.css('li').each do |li|
36
- puts li.at_css('.pos').text.fixed(5).blue + li.at_css('.def').text
37
+ puts with_color(with_fixed(li.at_css('.pos').text, 5), :blue) + li.at_css('.def').text
37
38
  end
38
39
  end
39
40
 
@@ -46,13 +47,13 @@ module BingDictionary
46
47
  def machine
47
48
  puts doc.at_css('.smt_hw').text
48
49
  puts doc.at_css('.p1-10').text
49
- puts doc.at_css('.p1-11').text.green
50
+ puts with_color(doc.at_css('.p1-11').text, :green)
50
51
  end
51
52
 
52
53
  def sentence
53
54
  puts
54
55
  doc.css('#sentenceSeg .se_li').first(4).map do |li|
55
- puts li.css('.sen_en').text
56
+ puts with_highlight(li.css('.sen_en').text, word)
56
57
  puts li.css('.sen_cn').text
57
58
  puts
58
59
  end
@@ -65,30 +66,24 @@ module BingDictionary
65
66
  puts
66
67
 
67
68
  content.css('.dym_area').each do |area|
68
- puts area.at_css('.df_wb_a').text
69
- puts area.css('.df_wb_c').map(&:text)
70
- puts
69
+ puts area.at_css('.df_wb_a').text
70
+ puts area.css('.df_wb_c').map(&:text)
71
+ puts
71
72
  end
72
73
  end
73
74
 
74
- end
75
-
76
- end
77
-
78
- class String
79
- COLORS = %w(black red green yellow blue magenta cyan white)
80
- COLORS.each_with_index do |color, idx|
81
- define_method color do
82
- "\e[3#{idx}m" << self.to_s << "\e[0m"
75
+ COLORS = %i[ black red green yellow blue magenta cyan white ]
76
+ def with_color(str, color)
77
+ "\e[3#{COLORS.index(color)}m" << str.to_s << "\e[0m"
83
78
  end
84
79
 
85
- define_method "#{color}_bg" do
86
- "\e[4#{idx}m" << self.to_s << "\e[0m"
80
+ def with_fixed(str, width)
81
+ width = width - str.each_char.count { |c| c =~ /\p{Han}/ }
82
+ width > 0 ? ("%-#{width}s" % str) : str
87
83
  end
88
- end
89
84
 
90
- def fixed(width)
91
- width = width - self.each_char.count { |c| c =~ /\p{Han}/ }
92
- width > 0 ? ("%-#{width}s" % self) : self
85
+ def with_highlight(str, keyword)
86
+ str.gsub(/#{keyword}/i) { |s| with_color(s, :yellow) }
87
+ end
93
88
  end
94
89
  end
@@ -1,3 +1,3 @@
1
1
  module BingDictionary
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bing_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - lingceng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-17 00:00:00.000000000 Z
11
+ date: 2021-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.11'
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.11'
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.6'
69
- description: Command line dictionary grabing http://cn.bing.com/
69
+ description: With local cache and can work with vim
70
70
  email:
71
71
  - lingceng2008@gmail.com
72
72
  executables:
@@ -88,7 +88,8 @@ files:
88
88
  - lib/bing_dictionary.rb
89
89
  - lib/bing_dictionary/version.rb
90
90
  homepage: https://github.com/lingceng/bing_dictionary
91
- licenses: []
91
+ licenses:
92
+ - MIT
92
93
  metadata: {}
93
94
  post_install_message:
94
95
  rdoc_options: []
@@ -105,8 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  - !ruby/object:Gem::Version
106
107
  version: '0'
107
108
  requirements: []
108
- rubyforge_project:
109
- rubygems_version: 2.5.1
109
+ rubygems_version: 3.0.6
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Command line dictionary grabing http://cn.bing.com/