bing_dictionary 0.1.3 → 0.1.7

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: 8c815fd924860c6926cec410939ee4c1012a2698
4
- data.tar.gz: 2a74d6e5d1bc2776ed51629feaf7f741d03a1e8c
2
+ SHA256:
3
+ metadata.gz: 1cad16aa4d9efe86a2c0aadbdbf90132184b6ba5d63ba6f38f637ae2189591b2
4
+ data.tar.gz: 56b07543646bfd3535f50c3d25fd25377c66e46919ec6f4889c82e87ba4fe72c
5
5
  SHA512:
6
- metadata.gz: c80f47b478f95e684a23cf71a25ce9fea3d2aa2d44cc41dd327dd6f815f0c2e9f50acb0333eb6b2d4ea12ba258e89c9f444f6bb64ac43f16f9c626852023ce3b
7
- data.tar.gz: c07bcf66d85f0d60ae7487cae295df5e0dde29083550a21fe515fd685309519ee234014cb5b9d2f24b97aefd4054c9eb0f6d2b4b128e9476480a8d45b2cd1cff
6
+ metadata.gz: b96c361ef65d1d323b9a71ffd13d6eb22f5c7a5cd2c3f498f0ebcfce6ce9ca0ef2fe35957612602a47b53f8218ff3aa10b1acffbb690140fb7702d96dc7584a6
7
+ data.tar.gz: 41e6c58cf8de625c69c17fa1ac5dc69254606c37ee273c5e6db84b2943dd0923298ae853734cc20440d9ca8a810ba12b0ec0472e5049ffcc406ca1856eb33871
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,51 @@ 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:
56
+
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
61
67
 
62
- nnoremap <leader>dd :!open dic://<cword><CR>
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.
76
+ bundle exec ./bin/dict --no-cache hello
70
77
 
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).
78
+ // Install gem in local
79
+ rake install
80
+ // Build new version
81
+ rake build
82
+ // Build new gem and publish to https://rubygems.org
83
+ rake release
72
84
 
73
85
  ## Contributing
74
86
 
data/bin/dict CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bing_dictionary"
4
3
  require 'optparse'
5
4
 
6
5
  options = {}
@@ -9,7 +8,10 @@ option_parser = OptionParser.new do |opts|
9
8
  opts.on("-p", "--[no-]pronounce", "Pronounce the word") do |v|
10
9
  options[:pronounce] = v
11
10
  end
12
- opts.on("-j", "--[no-]jump", "Jump to web page") do |v|
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|
13
15
  options[:jump] = v
14
16
  end
15
17
  opts.on("-v", "--version", "Show the version") do |v|
@@ -21,11 +23,32 @@ if ARGV.empty?
21
23
  option_parser.parse('--help')
22
24
  else
23
25
  option_parser.parse!
26
+ word = ARGV.join(' ')
24
27
  if options[:version]
28
+ require "bing_dictionary"
25
29
  puts BingDictionary::VERSION
26
30
  elsif options[:jump]
27
- `open http://cn.bing.com/dict/?q=#{CGI::escape(ARGV.join(' '))}`
31
+ `open http://cn.bing.com/dict/?q=#{CGI::escape(word)}`
28
32
  else
29
- BingDictionary::Base.translate(ARGV.join(' '), options)
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
30
52
  end
31
53
  end
54
+
@@ -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"
@@ -2,10 +2,12 @@ require 'nokogiri'
2
2
  require 'open-uri'
3
3
  require 'cgi'
4
4
  require "bing_dictionary/version"
5
+ require "uri"
6
+ require "net/http"
5
7
 
6
8
  module BingDictionary
7
9
  class Base
8
- attr_reader :doc
10
+ attr_accessor :doc, :options, :word
9
11
 
10
12
  def self.translate(word, options = {})
11
13
  self.new(word, options).translate
@@ -14,10 +16,23 @@ module BingDictionary
14
16
  exit 1
15
17
  end
16
18
 
19
+ def request(word)
20
+ url = URI("https://cn.bing.com/dict/search?q=#{CGI::escape(word)}")
21
+
22
+ https = Net::HTTP.new(url.host, url.port)
23
+ https.use_ssl = true
24
+ request = Net::HTTP::Get.new(url)
25
+ request["Cookie"] = "_EDGE_S=mkt=zh-cn;"
26
+
27
+ response = https.request(request)
28
+ return response.read_body
29
+ end
30
+
17
31
  def initialize(word, options = {})
18
- file = open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}")
19
- @doc = Nokogiri::HTML(file)
20
- @options = options
32
+ body = self.request(word)
33
+ self.word = word
34
+ self.doc = Nokogiri::HTML(body)
35
+ self.options = options
21
36
  end
22
37
 
23
38
  def translate
@@ -25,15 +40,15 @@ module BingDictionary
25
40
  machine if doc.at_css('.smt_hw')
26
41
  sentence if doc.at_css('#sentenceSeg .se_li')
27
42
  guess if doc.at_css('.dym_area')
28
- pronounce if doc.at_css('#headword') && @options[:pronounce]
43
+ pronounce if doc.at_css('#headword') && options[:pronounce]
29
44
  end
30
45
 
31
46
  def head
32
47
  puts doc.at_css('#headword').text
33
- puts doc.at_css('.hd_tf_lh').text.green
48
+ puts with_color(doc.at_css('.hd_tf_lh').text, :green)
34
49
  puts
35
50
  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
51
+ puts with_color(with_fixed(li.at_css('.pos').text, 5), :blue) + li.at_css('.def').text
37
52
  end
38
53
  end
39
54
 
@@ -46,13 +61,13 @@ module BingDictionary
46
61
  def machine
47
62
  puts doc.at_css('.smt_hw').text
48
63
  puts doc.at_css('.p1-10').text
49
- puts doc.at_css('.p1-11').text.green
64
+ puts with_color(doc.at_css('.p1-11').text, :green)
50
65
  end
51
66
 
52
67
  def sentence
53
68
  puts
54
69
  doc.css('#sentenceSeg .se_li').first(4).map do |li|
55
- puts li.css('.sen_en').text
70
+ puts with_highlight(li.css('.sen_en').text, word)
56
71
  puts li.css('.sen_cn').text
57
72
  puts
58
73
  end
@@ -71,24 +86,18 @@ module BingDictionary
71
86
  end
72
87
  end
73
88
 
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"
89
+ COLORS = %i[ black red green yellow blue magenta cyan white ]
90
+ def with_color(str, color)
91
+ "\e[3#{COLORS.index(color)}m" << str.to_s << "\e[0m"
83
92
  end
84
93
 
85
- define_method "#{color}_bg" do
86
- "\e[4#{idx}m" << self.to_s << "\e[0m"
94
+ def with_fixed(str, width)
95
+ width = width - str.each_char.count { |c| c =~ /\p{Han}/ }
96
+ width > 0 ? ("%-#{width}s" % str) : str
87
97
  end
88
- end
89
98
 
90
- def fixed(width)
91
- width = width - self.each_char.count { |c| c =~ /\p{Han}/ }
92
- width > 0 ? ("%-#{width}s" % self) : self
99
+ def with_highlight(str, keyword)
100
+ str.gsub(/#{keyword}/i) { |s| with_color(s, :yellow) }
101
+ end
93
102
  end
94
103
  end
@@ -1,3 +1,3 @@
1
1
  module BingDictionary
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.7"
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.3
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - lingceng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-14 00:00:00.000000000 Z
11
+ date: 2021-08-22 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/