linggle 0.03 → 0.04

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b84fd307ea286cfa52eeb140f31910e346c65c1
4
- data.tar.gz: 78ccfb33e517f27ddf418b34c9d373c29214d63e
3
+ metadata.gz: 7bac3fc9284ee9f67e5a2792391dec79770f0c19
4
+ data.tar.gz: 14e2ab157b3e80ca3ef9ed476ab6130266d27f04
5
5
  SHA512:
6
- metadata.gz: 10426a517462bf91e688dbb510c352783538ad09f5a2eb030b6481c44816e191baf767e2427521eee7a9078f427e9b92815e05daf64ef7ab3893a0de0c2d8075
7
- data.tar.gz: efa5c1709e6f0eab608e93137a4d1e7647a7c769b7419a8ce614f4e84954a80cf62ee75ce1bcd63a0afe9cdf3fd7d766083bd530d7a8f6b65cda82c951b6ce52
6
+ metadata.gz: 4c98cf1957b340f3d984807646b3d1dbccf07304b4e2cbe107699e6f95f47fcfe4ff57766bb3467cb324a04e735a49b03bd7503d624c039420b559c806456466
7
+ data.tar.gz: b7c3a663ee4202a4ea547e45ddd666b31e4030b8627053a6f8d1690ae581c25dcb7009f6d5e5014915858f81a02b4afacb7e6886f0cf8aba2f445dac3975773e
@@ -1,10 +1,11 @@
1
1
  require 'json'
2
- require 'open-uri'
2
+ require 'faraday'
3
3
  require 'optionparser'
4
4
  require 'colorize'
5
5
  require "linggle/version"
6
6
  require 'linggle/api'
7
7
  require 'linggle/string'
8
+ require 'linggle/help_doc'
8
9
 
9
10
  # resource
10
11
  # https://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
@@ -14,19 +15,23 @@ module Linggle
14
15
  def self.execute()
15
16
  args = parse()
16
17
  query_str = args.join(' ')
17
- # puts "parse result #{args}"
18
-
18
+
19
19
  # TODO rescue network exception, and follow redirections..
20
- entries = query(query_str)
21
- # {
22
- # "count"=>13966963,
23
- # "count_str"=>"14,000,000",
24
- # "percent"=>"54.5%",
25
- # "phrase" =>
26
- # ["very", "good"]
27
- # }
28
- entries = entries.take(20).map do |e|
29
- "#{e['phrase'].join(' ').green} 频率: #{e['count_str'].yellow} #{e['percent']} \n #{e['percent'].percent_line} \n \n"
20
+ results = Api.new.query(query_str)
21
+ =begin
22
+ {
23
+ "query": "go ?to schoo",
24
+ "time": 1534167385507,
25
+ "ngrams": [
26
+ ["go to school", 667988],
27
+ ["go school", 6137]
28
+ ],
29
+ "total": 674125
30
+ }
31
+ =end
32
+ entries = results['ngrams'].take(20).map do |x|
33
+ percent = ( (x[1].to_f/results['total']).round(2) * 100 ).to_s
34
+ "#{x[0].green} Percent: #{percent.yellow} #{x[1]} \n #{percent.percent_line} \n \n"
30
35
  end
31
36
  puts entries
32
37
  end
@@ -1,95 +1,54 @@
1
1
  module Linggle
2
- API = 'http://linggle.com/query'.freeze
3
- TIMEOUT = 10
2
+ API_BASE = 'http://linggle.com'.freeze
3
+ TIMEOUT = 3
4
4
  COLORS = {
5
5
  e: :yellow, # example
6
6
  t: :blue # title
7
- }
8
-
9
- ##
10
- # 查询返回结果
11
- #
12
- # [
13
- # {"count"=>13966963, "count_str"=>"14,000,000", "percent"=>"54.5%", "phrase"=>["very", "good"]}
14
- # {"count"=>4436461, "count_str"=>"4,400,000", "percent"=>"17.3%", "phrase"=>["pretty", "good"]}
15
- # {"count"=>562220, "count_str"=>"560,000", "percent"=>"2.2%", "phrase"=>["only", "good"]}
16
- # {"count"=>508737, "count_str"=>"510,000", "percent"=>"2.0%", "phrase"=>["real", "good"]}
17
- # {"count"=>481831, "count_str"=>"480,000", "percent"=>"1.9%", "phrase"=>["public", "good"]}
18
- # {"count"=>480872, "count_str"=>"480,000", "percent"=>"1.9%", "phrase"=>["damn", "good"]}
19
- # {"count"=>429867, "count_str"=>"430,000", "percent"=>"1.7%", "phrase"=>["common", "good"]}
20
- # {"count"=>409578, "count_str"=>"410,000", "percent"=>"1.6%", "phrase"=>["other", "good"]}
21
- # {"count"=>308328, "count_str"=>"310,000", "percent"=>"1.2%", "phrase"=>["good", "good"]}
22
- # {"count"=>303609, "count_str"=>"300,000", "percent"=>"1.2%", "phrase"=>["still", "good"]}
23
- # {"count"=>256591, "count_str"=>"260,000", "percent"=>"1.0%", "phrase"=>["just", "good"]}
24
- # {"count"=>225266, "count_str"=>"230,000", "percent"=>"0.9%", "phrase"=>["sound", "good"]}
25
- # {"count"=>207530, "count_str"=>"210,000", "percent"=>"0.8%", "phrase"=>["like", "good"]}
26
- # {"count"=>191120, "count_str"=>"190,000", "percent"=>"0.7%", "phrase"=>["greater", "good"]}
27
- # ...
28
- # ]
29
-
30
- def self.query(query_str)
31
- content = open(
32
- URI::encode("#{API}/#{query_str}"),
33
- read_timeout: TIMEOUT
34
- ).read
35
-
36
- # puts "conent #{content}"
37
- entries = JSON.parse(content)
7
+ }
8
+ class Api
9
+ attr_reader :conn
10
+
11
+ def initialize
12
+ @conn = Faraday.new(url: API_BASE) do |f|
13
+ f.request :url_encoded
14
+ f.response :logger
15
+ f.adapter Faraday.default_adapter
16
+ end
17
+ end
18
+ ##
19
+ # 查询返回结果示例
20
+ #
21
+ # {"query": "go ?to schoo", "time": 1534167385507, "ngrams": [["go to schoo", 234]], "total": 234}
22
+ #
23
+
24
+ def query(query_str)
25
+
26
+
27
+
28
+ # puts "conent #{content}"
29
+ res = conn.post '/query/', {query: query_str, time: Time.now.to_i}.to_json do |req|
30
+ puts csrf_token
31
+ req.headers['Content-Type'] = 'application/json'
32
+ req.headers['X-Requested-With'] = 'XMLHttpRequest'
33
+ req['Cookie'] = "csrftoken=#{csrf_token}"
34
+ req.headers['X-CSRFToken'] = csrf_token
35
+ end
36
+ puts res
37
+ puts res.body
38
+ JSON.parse(res.body)
39
+ end
40
+
41
+
42
+ private
43
+ def csrf_token
44
+ return @csrf_token if @csrf_token
45
+ cookies = conn.get.env.response_headers['set-cookie']
46
+ # TODO save token with'sdbm'
47
+ # File.open '.linggle_cli'
48
+ crsf_token_data = cookies.split('; ').map {|x| x.split('=', 0) }.to_h
49
+
50
+
51
+ @csrf_token = crsf_token_data['csrftoken']
52
+ end
38
53
  end
39
-
40
- HELPS = {
41
- '_' => <<-DOC,
42
- #{'查询任意一至多个词'.bold.colorize(COLORS[:t])}
43
- 只要在你想要插入字词的位置使用* (星号)来查询一个词,或是_ (底线)来查询零至多个词就可以了。
44
-
45
- #{ 'present a method _
46
- listen _ music'.colorize(COLORS[:e]) }
47
-
48
-
49
- DOC
50
-
51
- '?' => <<-DOC,
52
- #{'使用问号查询是否需要某个字'.blue}
53
- 小明在报告里写了这么一句话:“I would like to discuss about the issue.”
54
- 回头检查时,总是觉得哪里怪怪的,他很怀疑“discuss”后面到底需不需要
55
- 加“about” ⋯⋯这时只要使用Linggle,在你不确定的字前加上? (问号),Linggle就会告诉你需不需要这个字啰!
56
-
57
- #{'discuss ?about the issue'.colorize(COLORS[:e]) }
58
-
59
-
60
- DOC
61
-
62
- '/' => <<-DOC,
63
- #{'使用斜线号查询如何替换某字词'.blue}
64
- 介系词这么多常常搞不清楚要用哪一个吗?太多字可以用却不知道哪一个比较适合吗?
65
- 让Linggle来告诉你该怎么选吧!很简单,只需要在你要抉择的两个字中间加入/ (斜线号)
66
- 就可以了!不只是介系词,其他词性也可以!
67
-
68
- #{ 'in/at the afternoon
69
- play _ important role/part'.colorize(COLORS[:e]) }
70
-
71
-
72
- DOC
73
-
74
- 'w.' => <<-DOC,
75
- #{'使用任意词性查询搭配词'.blue}
76
- Linggle最大的特色,就是可以使用「关键词」配合「特定词性」来查询各种搭配词。比如说,
77
- 可以用v. death penalty来查询“death penalty”搭配的动词,效果不逊于一般搭配词典喔!
78
-
79
- #{ 'v. death penalty
80
- acquire n.'.colorize(COLORS[:e]) }
81
-
82
-
83
- DOC
84
-
85
- 'more' => <<-DOC,
86
- #{'精准的搭配词查询'.blue}
87
- 想要更加精准的查询搭配词,我们就会建议使用复杂的查询式,例如在想要查询的动词前
88
- 加上一个“to”,让Linggle 不至于会错意。
89
-
90
- #{'to v. ?prep. ?det. difficulty/difficulties'.colorize(COLORS[:e]) }
91
-
92
-
93
- DOC
94
- }.freeze
95
54
  end
@@ -0,0 +1,58 @@
1
+ module Linggle
2
+ HELPS = {
3
+ '_' => <<-DOC,
4
+ #{'查询任意一至多个词'.bold.colorize(COLORS[:t])}
5
+ 只要在你想要插入字词的位置使用* (星号)来查询一个词,或是_ (底线)来查询零至多个词就可以了。
6
+
7
+ #{ 'present a method _
8
+ listen _ music'.colorize(COLORS[:e]) }
9
+
10
+
11
+ DOC
12
+
13
+ '?' => <<-DOC,
14
+ #{'使用问号查询是否需要某个字'.blue}
15
+ 小明在报告里写了这么一句话:“I would like to discuss about the issue.”
16
+ 回头检查时,总是觉得哪里怪怪的,他很怀疑“discuss”后面到底需不需要
17
+ 加“about” ⋯⋯这时只要使用Linggle,在你不确定的字前加上? (问号),Linggle就会告诉你需不需要这个字啰!
18
+
19
+ #{'discuss ?about the issue'.colorize(COLORS[:e]) }
20
+
21
+
22
+ DOC
23
+
24
+ '/' => <<-DOC,
25
+ #{'使用斜线号查询如何替换某字词'.blue}
26
+ 介系词这么多常常搞不清楚要用哪一个吗?太多字可以用却不知道哪一个比较适合吗?
27
+ 让Linggle来告诉你该怎么选吧!很简单,只需要在你要抉择的两个字中间加入/ (斜线号)
28
+ 就可以了!不只是介系词,其他词性也可以!
29
+
30
+ #{ 'in/at the afternoon
31
+ play _ important role/part'.colorize(COLORS[:e]) }
32
+
33
+
34
+ DOC
35
+
36
+ 'w.' => <<-DOC,
37
+ #{'使用任意词性查询搭配词'.blue}
38
+ Linggle最大的特色,就是可以使用「关键词」配合「特定词性」来查询各种搭配词。比如说,
39
+ 可以用v. death penalty来查询“death penalty”搭配的动词,效果不逊于一般搭配词典喔!
40
+
41
+ #{ 'v. death penalty
42
+ acquire n.'.colorize(COLORS[:e]) }
43
+
44
+
45
+ DOC
46
+
47
+ 'more' => <<-DOC,
48
+ #{'精准的搭配词查询'.blue}
49
+ 想要更加精准的查询搭配词,我们就会建议使用复杂的查询式,例如在想要查询的动词前
50
+ 加上一个“to”,让Linggle 不至于会错意。
51
+
52
+ #{'to v. ?prep. ?det. difficulty/difficulties'.colorize(COLORS[:e]) }
53
+
54
+
55
+ DOC
56
+ }.freeze
57
+
58
+ end
@@ -34,7 +34,8 @@ class String
34
34
  # 55.6%
35
35
  Strips = '|' * 100
36
36
  def percent_line
37
- count = (self[/\d+\.?\d+/].to_f).round
38
- "[#{ Strips[0..count].red } #{ ' ' * (100 - count) }]"
37
+ count = self.to_i
38
+ puts count
39
+ "[#{ Strips[0..count].red } #{ ' ' * (100 - count) }]"
39
40
  end
40
41
  end
@@ -1,3 +1,3 @@
1
1
  module Linggle
2
- VERSION = "0.03"
2
+ VERSION = "0.04"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linggle
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.03'
4
+ version: '0.04'
5
5
  platform: ruby
6
6
  authors:
7
7
  - fangxing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-27 00:00:00.000000000 Z
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.8.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: use linggle in your command line
70
84
  email:
71
85
  - fangxing204@gmail.com
@@ -79,7 +93,7 @@ files:
79
93
  - bin/linggle-console
80
94
  - lib/linggle.rb
81
95
  - lib/linggle/api.rb
82
- - lib/linggle/prints.rb
96
+ - lib/linggle/help_doc.rb
83
97
  - lib/linggle/string.rb
84
98
  - lib/linggle/version.rb
85
99
  - spec/linggle_spec.rb
@@ -104,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
118
  version: '0'
105
119
  requirements: []
106
120
  rubyforge_project:
107
- rubygems_version: 2.6.11
121
+ rubygems_version: 2.6.14
108
122
  signing_key:
109
123
  specification_version: 4
110
124
  summary: http://linggle.com/ in cli
@@ -1,5 +0,0 @@
1
- module Linggle
2
- def self.percents_str
3
-
4
- end
5
- end