linggle 0.03 → 0.04
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/linggle.rb +18 -13
- data/lib/linggle/api.rb +48 -89
- data/lib/linggle/help_doc.rb +58 -0
- data/lib/linggle/string.rb +3 -2
- data/lib/linggle/version.rb +1 -1
- metadata +18 -4
- data/lib/linggle/prints.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bac3fc9284ee9f67e5a2792391dec79770f0c19
|
4
|
+
data.tar.gz: 14e2ab157b3e80ca3ef9ed476ab6130266d27f04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c98cf1957b340f3d984807646b3d1dbccf07304b4e2cbe107699e6f95f47fcfe4ff57766bb3467cb324a04e735a49b03bd7503d624c039420b559c806456466
|
7
|
+
data.tar.gz: b7c3a663ee4202a4ea547e45ddd666b31e4030b8627053a6f8d1690ae581c25dcb7009f6d5e5014915858f81a02b4afacb7e6886f0cf8aba2f445dac3975773e
|
data/lib/linggle.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'json'
|
2
|
-
require '
|
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
|
-
|
18
|
-
|
18
|
+
|
19
19
|
# TODO rescue network exception, and follow redirections..
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
data/lib/linggle/api.rb
CHANGED
@@ -1,95 +1,54 @@
|
|
1
1
|
module Linggle
|
2
|
-
|
3
|
-
TIMEOUT =
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
data/lib/linggle/string.rb
CHANGED
@@ -34,7 +34,8 @@ class String
|
|
34
34
|
# 55.6%
|
35
35
|
Strips = '|' * 100
|
36
36
|
def percent_line
|
37
|
-
count =
|
38
|
-
|
37
|
+
count = self.to_i
|
38
|
+
puts count
|
39
|
+
"[#{ Strips[0..count].red } #{ ' ' * (100 - count) }]"
|
39
40
|
end
|
40
41
|
end
|
data/lib/linggle/version.rb
CHANGED
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.
|
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-
|
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/
|
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.
|
121
|
+
rubygems_version: 2.6.14
|
108
122
|
signing_key:
|
109
123
|
specification_version: 4
|
110
124
|
summary: http://linggle.com/ in cli
|