bosonnlp 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 643cfad815401b8bfad8866dad58d6d0dbde67db
4
+ data.tar.gz: d29f2d1fe81816b6f499c2c8c4bec7ba67f9b391
5
+ SHA512:
6
+ metadata.gz: d5fe955ae337be4f594c4380b98908c7acc6c83850323413f8ecfad64f03eb9d0f1061fb9d41a759f21870908058e46fc375eccbd337b87ce602170c54a88a5d
7
+ data.tar.gz: 8954ef43d134f6160dc2cb385d3021c4131ead3327672973dc3358bab3babe957fd1346d5d2aee7f443826cb893cf016d9caf8e3eca6dcf2549cc88dfac9110b
@@ -0,0 +1,212 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'json'
4
+ require 'httpclient'
5
+ require 'securerandom'
6
+
7
+ require 'bosonnlp/util'
8
+ require 'bosonnlp/mixin'
9
+ require 'bosonnlp/version'
10
+
11
+ # Main class
12
+ class Bosonnlp
13
+ include Util
14
+ # Operated task not found on server
15
+ class TaskNotFoundError < StandardError
16
+ end
17
+ # Other task error return by server
18
+ class TaskError < StandardError
19
+ end
20
+ # Http error
21
+ class HTTPError < RuntimeError
22
+ end
23
+ # Task time out.
24
+ class TimeoutError < StandardError
25
+ end
26
+
27
+ # Multiple text engine handler
28
+ class MultipleHandler
29
+ include Util
30
+ def initialize(name, http_session, head, multiple_url)
31
+ @name = name
32
+ @http_session = http_session
33
+ @head = head
34
+ @id = SecureRandom.uuid
35
+ @multiple_url = multiple_url
36
+ @alpha = 0.8
37
+ @beta = 0.45
38
+ end
39
+
40
+ def pre_process(data)
41
+ fail ArgumentError, 'Wrong args, data must be a list'\
42
+ ' which contains more than one element.'\
43
+ unless Array === data and data.size > 1
44
+ fail ArgumentError, 'Wrong args, data elements must be list of string '\
45
+ 'or {_id, text} Hash.'\
46
+ unless String === data[0] or Hash === data[0]
47
+
48
+ processd = []
49
+ if String === data[0]
50
+ data.each do |d|
51
+ hash_t = {}
52
+ hash_t['_id'] = SecureRandom.uuid
53
+ hash_t['text'] = d
54
+ processd << hash_t
55
+ end
56
+ else
57
+ processd = data
58
+ end
59
+
60
+ processd
61
+ end
62
+
63
+ def get_url(method_name)
64
+ @multiple_url % [@name, method_name.to_s, @id]
65
+ end
66
+
67
+ def push(*args)
68
+ processd = pre_process args[0]
69
+ request_args = get_hash_args(*args)
70
+
71
+ is_ok = false
72
+ processd.each_slice 100 do |s|
73
+ request_args[:body] = s
74
+ res = _http_request(:post, __method__, request_args)
75
+ is_ok = res.ok?
76
+ # TODO, fail for one exception?
77
+ end
78
+ is_ok
79
+ end
80
+
81
+ def analysis(*args)
82
+ request_args = get_hash_args(*args)
83
+ request_args['alpha'] ||= @alpha
84
+ request_args['beta'] ||= @beta
85
+ res = _http_request(:get, __method__, *args)
86
+ res.ok?
87
+ end
88
+
89
+ def status(*args)
90
+ res = _http_request(:get, __method__, *args)
91
+ status = JSON.load(res.content)['status']
92
+ case status.to_s.downcase
93
+ when 'not found'
94
+ raise TaskNotFoundError, "Operated task #{@id} not found on server."
95
+ when 'error'
96
+ raise TaskError, "Task #{@id} failed on server."
97
+ end
98
+ status
99
+ end
100
+
101
+ def _wait(timeout)
102
+ time_elapsed = 0.0
103
+ sleep_lenth = 1.0
104
+ while true
105
+ sleep sleep_lenth
106
+ serv_status = status
107
+ return if serv_status.downcase == "done"
108
+ time_elapsed += sleep_lenth
109
+ raise TimeoutError if timeout and time_elapsed > timeout
110
+ sleep_lenth *= 1.5 if sleep_lenth < 2**6
111
+ end
112
+ end
113
+
114
+ def result(*args)
115
+ _wait get_hash_args(*args)['timeout']
116
+ res = _http_request(:get, __method__, *args)
117
+ JSON.load(res.content)
118
+ end
119
+
120
+ def clear(*args)
121
+ res = _http_request(:get, __method__, *args)
122
+ res.ok?
123
+ end
124
+ end
125
+
126
+ def initialize(token = nil)
127
+ @token = token
128
+ @env_token = ENV['BOSON_API_TOKEN']
129
+ proxy = ENV['HTTP_PROXY']
130
+ @http_session = HTTPClient.new(proxy)
131
+
132
+ @readable = false
133
+ @fail_on_exception = false
134
+ @base_url = 'http://api.bosonnlp.com'
135
+ @analysis_url = @base_url + '/%s/analysis'
136
+ @multiple_url = @base_url + '/%s/%s/%s'
137
+
138
+ @token ||= @env_token
139
+ fail 'No API token given or found in environment variables, run '\
140
+ '`export BOSON_API_TOKEN="<your api token>"` in your shell first.'\
141
+ unless @token
142
+
143
+ headers = {}
144
+ headers['Accept'] = 'application/json'
145
+ headers['User-Agent'] = "bosonnlp-ruby #{Bosonnlp::VERSION} "\
146
+ "(httpclient #{HTTPClient::VERSION})"
147
+ headers['Content-Type'] = 'application/json'
148
+ headers['X-Token'] = @token
149
+ @head = headers
150
+
151
+ # TODO, args to change default values
152
+ end
153
+
154
+ def common_api(name, *args)
155
+ fail ArgumentError, 'Wrong args, first arg must be a list of string.'\
156
+ unless Array === args[0] and String === args[0][0]
157
+
158
+ url = @analysis_url % name
159
+
160
+ request_args = args[1] || {}
161
+ request_args[:body] = args[0]
162
+
163
+ res = _http_request(:post, url, request_args)
164
+ JSON.load(res.content)
165
+ end
166
+
167
+ def create_multiple(name)
168
+ MultipleHandler.new name.to_s, @http_session, @head, @multiple_url
169
+ end
170
+
171
+ def multiple_api(name, *args)
172
+ begin
173
+ mh = create_multiple(name)
174
+ mh.push(*args)
175
+ mh.analysis(*args)
176
+ return mh.result
177
+ rescue => e
178
+ puts e.message
179
+ puts e.backtrace.inspect
180
+ ensure
181
+ mh.clear(*args) if mh
182
+ end
183
+ end
184
+
185
+ def single_api(name, *args)
186
+ fail ArgumentError, 'Wrong args, data must be a list'\
187
+ ' which contains one and only one string element.'\
188
+ unless Array === args[0] and String === args[0][0] and args[0].size == 1
189
+
190
+ url = @analysis_url % name
191
+
192
+ request_args = get_hash_args(*args)
193
+ request_args[:body] = args[0][0]
194
+
195
+ res = _http_request(:post, url, request_args)
196
+ JSON.load(res.content)
197
+ # :query, :body, :header, :follow_redirect
198
+ end
199
+
200
+ def method_missing(name, *args)
201
+ case name.to_s
202
+ when /^c_(.+)$/
203
+ common_api($1, *args)
204
+ when /^m_(.+)$/
205
+ multiple_api($1, *args)
206
+ when /^s_(.+)$/
207
+ single_api($1, *args)
208
+ else
209
+ super
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,28 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'bosonnlp'
3
+
4
+ # Mixin module
5
+ module BosonnlpMixin
6
+ def method_missing(name, *args)
7
+ case name.to_s
8
+ when /^(c_|m_|s_)(.+)$/
9
+ @@nlp ||= Bosonnlp.new
10
+ data = self
11
+ data = [data] if self.class == String
12
+
13
+ @@nlp.send name, data, *args
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
19
+
20
+ # Mixin it
21
+ class Array
22
+ include BosonnlpMixin
23
+ end
24
+
25
+ # Mixin it
26
+ class String
27
+ include BosonnlpMixin
28
+ end
@@ -0,0 +1,29 @@
1
+ class Bosonnlp
2
+ # 'Utils'
3
+ module Util
4
+ def get_hash_args(*args)
5
+ if Hash === args[-1]
6
+ h = args[-1]
7
+ else
8
+ h = {}
9
+ end
10
+ h
11
+ end
12
+
13
+ # :query, :body, :header, :follow_redirect
14
+ def _http_request(method, url, *args)
15
+ request_args = get_hash_args(*args)
16
+ url = get_url url unless url.to_s.start_with?('http://')
17
+ request_args[:header] = @head
18
+ request_args[:body] = \
19
+ JSON.dump(request_args[:body]) if request_args[:body]
20
+
21
+ res = @http_session.request(method, url, request_args)
22
+ status = res.status
23
+ raise HTTPError, 'HTTPError: %s %s'\
24
+ % [status, (JSON.load(res.content)['message'] rescue res.reason)]\
25
+ if status >= 400 && status < 600
26
+ res
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ class Bosonnlp
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bosonnlp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - CC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: bosonnlp.com
14
+ email: chcoalc@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/bosonnlp.rb
20
+ - lib/bosonnlp/mixin.rb
21
+ - lib/bosonnlp/util.rb
22
+ - lib/bosonnlp/version.rb
23
+ homepage: http://github.com/alal/bosonnlp
24
+ licenses:
25
+ - Apache
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '1.9'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Bosonnlp Ruby SDK
47
+ test_files: []