bing_dictionary 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b63e6e6d8367c9bb9d0a09377acdecfbcff23afb
4
+ data.tar.gz: db05bfacd1152bce409fcf051f60287d84f7bbb3
5
+ SHA512:
6
+ metadata.gz: 32211a31d1ae14a34a2b8827360ee1667c7e5829577a1f3ab7953a6f27633f4e5bbd2b8ec7c63018d0814ae12b193d8efaeebe58a21634d6af4dbe079825ac47
7
+ data.tar.gz: f303d28a2bd96123fd42c9274dcbaa1f720c1defe4950101c6bd1d7a0503c162d718384a6943d531a56e32dcec54ed2bba8d30c6668d777b9ab5ab92a8dacf27
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at lingceng2008@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bing_dictionary.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # BingDictionary
2
+
3
+ 这是一个ruby脚本,用来在命令行中直接查询单词含义,免去打开软件或浏览器的麻烦。
4
+
5
+ Here is a command line dictionary written with ruby that can
6
+ translate English to Chinese.
7
+
8
+ It uses http://cn.bing.com/dict/ to query.
9
+ ![DEMO ](./dict.png)
10
+
11
+ ## Installation
12
+
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
+ $ gem install bing_dictionary
26
+
27
+ ## Usage
28
+ A `dict` runable bin file provided.
29
+
30
+ DEMO:
31
+
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>
39
+
40
+ ## Support long sentence
41
+ Now long sentence is supported
42
+
43
+ dict 你好吗
44
+ dict How old are you
45
+
46
+ ## Support fuzzy query
47
+
48
+ dict hexxo
49
+
50
+ 您要找的是不是
51
+
52
+ 音近词
53
+ head coach总教练
54
+ hexose己醣
55
+ hexode六极管
56
+ ...
57
+
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:
61
+
62
+ nnoremap <leader>dd :!open dic://<cword><CR>
63
+
64
+ ## Thanks
65
+ Heavily borrowed from [Command-Line-Youdao-Dictionary](https://github.com/qhwa/Command-Line-Youdao-Dictionary)
66
+
67
+ ## Development
68
+
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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lingceng/bing_dictionary. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bing_dictionary"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/dict ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bing_dictionary"
5
+
6
+ BingDictionary::Base.translate(ARGV.join ' ')
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bing_dictionary/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bing_dictionary"
8
+ spec.version = BingDictionary::VERSION
9
+ spec.authors = ["lingceng"]
10
+ spec.email = ["lingceng2008@gmail.com"]
11
+
12
+ 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.homepage = "https://github.com/lingceng/bing_dictionary"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = 'dict'
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+
25
+ spec.add_dependency "nokogiri", "~> 1.6"
26
+ end
data/dict.png ADDED
Binary file
@@ -0,0 +1,86 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'cgi'
4
+ require "bing_dictionary/version"
5
+
6
+ module BingDictionary
7
+ class Base
8
+ attr_reader :doc
9
+
10
+ def self.translate(word)
11
+ self.new(word).translate
12
+ rescue SocketError
13
+ warn 'Connection failed! Please check your network.'
14
+ exit 1
15
+ end
16
+
17
+ def initialize(word)
18
+ file = open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}")
19
+ @doc = Nokogiri::HTML(file)
20
+ end
21
+
22
+ def translate
23
+ head if doc.at_css('#headword')
24
+ machine if doc.at_css('.smt_hw')
25
+ sentence if doc.at_css('#sentenceSeg .se_li')
26
+ guess if doc.at_css('.dym_area')
27
+ end
28
+
29
+ def head
30
+ puts doc.at_css('#headword').text
31
+ puts doc.at_css('.hd_tf_lh').text.green
32
+ puts
33
+ doc.at_css('.hd_area').next_sibling.css('li').each do |li|
34
+ puts li.at_css('.pos').text.fixed(5).blue + li.at_css('.def').text
35
+ end
36
+ end
37
+
38
+ def machine
39
+ puts doc.at_css('.smt_hw').text
40
+ puts doc.at_css('.p1-10').text
41
+ puts doc.at_css('.p1-11').text.green
42
+ end
43
+
44
+ def sentence
45
+ puts
46
+ doc.css('#sentenceSeg .se_li').first(4).map do |li|
47
+ puts li.css('.sen_en').text
48
+ puts li.css('.sen_cn').text
49
+ puts
50
+ end
51
+ end
52
+
53
+ def guess
54
+ puts
55
+ content = doc.at_css('.content')
56
+ puts content.at_css('.p2-2').text
57
+ puts
58
+
59
+ content.css('.dym_area').each do |area|
60
+ puts area.at_css('.df_wb_a').text
61
+ puts area.css('.df_wb_c').map(&:text)
62
+ puts
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ class String
71
+ COLORS = %w(black red green yellow blue magenta cyan white)
72
+ COLORS.each_with_index do |color, idx|
73
+ define_method color do
74
+ "\e[3#{idx}m" << self.to_s << "\e[0m"
75
+ end
76
+
77
+ define_method "#{color}_bg" do
78
+ "\e[4#{idx}m" << self.to_s << "\e[0m"
79
+ end
80
+ end
81
+
82
+ def fixed(width)
83
+ width = width - self.each_char.count { |c| c =~ /\p{Han}/ }
84
+ width > 0 ? ("%-#{width}s" % self) : self
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module BingDictionary
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bing_dictionary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lingceng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ description: Command line dictionary grabing http://cn.bing.com/
70
+ email:
71
+ - lingceng2008@gmail.com
72
+ executables:
73
+ - dict
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/dict
85
+ - bin/setup
86
+ - bing_dictionary.gemspec
87
+ - dict.png
88
+ - lib/bing_dictionary.rb
89
+ - lib/bing_dictionary/version.rb
90
+ homepage: https://github.com/lingceng/bing_dictionary
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Command line dictionary grabing http://cn.bing.com/
113
+ test_files: []