textrazor 0.0.8 → 1.0.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.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/README.md +26 -2
- data/Rakefile +10 -2
- data/lib/textrazor.rb +5 -0
- data/lib/textrazor/client.rb +38 -5
- data/lib/textrazor/configuration.rb +1 -1
- data/lib/textrazor/entailment.rb +24 -0
- data/lib/textrazor/property.rb +22 -0
- data/lib/textrazor/relation.rb +25 -0
- data/lib/textrazor/relation_param.rb +18 -0
- data/lib/textrazor/request.rb +6 -2
- data/lib/textrazor/response.rb +110 -34
- data/lib/textrazor/sentence.rb +24 -0
- data/lib/textrazor/topic.rb +8 -6
- data/lib/textrazor/util.rb +1 -1
- data/lib/textrazor/version.rb +1 -1
- data/lib/textrazor/word.rb +1 -1
- data/spec/functional/service_spec.rb +29 -0
- data/spec/lib/textrazor/client_spec.rb +113 -65
- data/spec/lib/textrazor/entailment_spec.rb +36 -0
- data/spec/lib/textrazor/entity_spec.rb +50 -26
- data/spec/lib/textrazor/phrase_spec.rb +8 -4
- data/spec/lib/textrazor/property_spec.rb +30 -0
- data/spec/lib/textrazor/relation_param_spec.rb +29 -0
- data/spec/lib/textrazor/relation_spec.rb +37 -0
- data/spec/lib/textrazor/request_spec.rb +7 -4
- data/spec/lib/textrazor/response_spec.rb +604 -49
- data/spec/lib/textrazor/sentence_spec.rb +41 -0
- data/spec/lib/textrazor/topic_spec.rb +12 -5
- data/textrazor.gemspec +1 -0
- metadata +35 -2
@@ -6,16 +6,20 @@ module TextRazor
|
|
6
6
|
|
7
7
|
context "#create_from_hash" do
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
let(:phrase_hash) do
|
10
|
+
{"id" => 1, "wordPositions" => [0, 1, 2]}
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:words) do
|
14
|
+
[
|
12
15
|
OpenStruct.new(token: 'Word1'),
|
13
16
|
OpenStruct.new(token: 'Word2'),
|
14
17
|
OpenStruct.new(token: 'Word3')
|
15
18
|
]
|
19
|
+
end
|
16
20
|
|
21
|
+
it "should create a new instance" do
|
17
22
|
phrase = Phrase.create_from_hash(phrase_hash, words)
|
18
|
-
|
19
23
|
expect(phrase.id).to eq(1)
|
20
24
|
end
|
21
25
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module TextRazor
|
4
|
+
|
5
|
+
describe Property do
|
6
|
+
|
7
|
+
context "#create_from_hash" do
|
8
|
+
let(:property_hash) do
|
9
|
+
{
|
10
|
+
id: 1,
|
11
|
+
wordPositions: [9, 11, 12],
|
12
|
+
propertyPositions: [10]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:property) do
|
17
|
+
Property.create_from_hash(property_hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create a new instance" do
|
21
|
+
expect(property.id).to eq(1)
|
22
|
+
expect(property.word_positions).to eq([9,11,12])
|
23
|
+
expect(property.property_positions).to eq([10])
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module TextRazor
|
4
|
+
|
5
|
+
describe RelationParam do
|
6
|
+
|
7
|
+
context "#create_from_hash" do
|
8
|
+
let(:relation_param_hash) do
|
9
|
+
{
|
10
|
+
relation: "SUBJECT",
|
11
|
+
wordPositions: [18, 19, 20, 21]
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:relation_param) do
|
16
|
+
RelationParam.create_from_hash(relation_param_hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create a new instance" do
|
20
|
+
expect(relation_param.relation).to eq 'SUBJECT'
|
21
|
+
expect(relation_param.word_positions).to eq [18, 19, 20, 21]
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module TextRazor
|
4
|
+
|
5
|
+
describe Relation do
|
6
|
+
|
7
|
+
context "#create_from_hash" do
|
8
|
+
let(:relation_hash) do
|
9
|
+
{
|
10
|
+
id: 0,
|
11
|
+
wordPositions: [1, 6],
|
12
|
+
params: [{
|
13
|
+
relation: "SUBJECT",
|
14
|
+
wordPositions: [18, 19, 20, 21]
|
15
|
+
},
|
16
|
+
{
|
17
|
+
relation: "OBJECT",
|
18
|
+
wordPositions: [2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
|
19
|
+
}]
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:relation) do
|
24
|
+
Relation.create_from_hash(relation_hash)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should create a new instance" do
|
28
|
+
expect(relation.number_of_relation_params).to eq 2
|
29
|
+
expect(relation.word_positions).to eq [1, 6]
|
30
|
+
expect(relation.relation_params.first).to be_instance_of RelationParam
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -57,13 +57,16 @@ module TextRazor
|
|
57
57
|
context "custom options" do
|
58
58
|
|
59
59
|
it "should make correct calls" do
|
60
|
-
options = {api_key: 'api_key', extractors: %w(entities topics words),
|
61
|
-
|
60
|
+
options = {api_key: 'api_key', extractors: %w(entities topics words), cleanup_mode: 'raw',
|
61
|
+
cleanup_return_cleaned: true, cleanup_return_raw: true, language: 'fre',
|
62
|
+
filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2), allow_overlap: false,
|
63
|
+
enrichment_queries: 'queries'}
|
62
64
|
|
63
65
|
expect(::RestClient).to receive(:post).
|
64
66
|
with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
|
65
|
-
"
|
66
|
-
"entities.filterFreebaseTypes" => "type2"
|
67
|
+
"cleanup.mode" => "raw", "cleanup.returnCleaned" => true, "cleanup.returnRaw" => true, "languageOverride" => 'fre',
|
68
|
+
"entities.filterDbpediaTypes" => "type1", "entities.filterFreebaseTypes" => "type2" , "entities.allowOverlap" => false,
|
69
|
+
"entities.enrichmentQueries" => "queries"},
|
67
70
|
accept_encoding: 'gzip')
|
68
71
|
|
69
72
|
Request.post('text', options)
|
@@ -4,7 +4,15 @@ module TextRazor
|
|
4
4
|
|
5
5
|
describe Response do
|
6
6
|
|
7
|
-
|
7
|
+
let(:http_response) do
|
8
|
+
::OpenStruct.new(code: 200, body: body)
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:response) do
|
12
|
+
Response.new(http_response)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#initialize" do
|
8
16
|
|
9
17
|
context "when HTTP response code is 200" do
|
10
18
|
|
@@ -13,8 +21,8 @@ module TextRazor
|
|
13
21
|
http_response = ::OpenStruct.new code: 200, body: body
|
14
22
|
|
15
23
|
expect(JSON).to receive(:parse).
|
16
|
-
with(body).
|
17
|
-
and_return({"response"=>"{}"})
|
24
|
+
with(body, {symbolize_names: true}).
|
25
|
+
and_return({"response" => "{}"})
|
18
26
|
|
19
27
|
Response.new(http_response)
|
20
28
|
end
|
@@ -56,17 +64,191 @@ module TextRazor
|
|
56
64
|
|
57
65
|
end
|
58
66
|
|
59
|
-
|
67
|
+
describe '#time' do
|
60
68
|
|
61
|
-
|
69
|
+
it 'returns time taken to process request' do
|
70
|
+
body = {time: "0.013219"}.to_json
|
71
|
+
http_response = ::OpenStruct.new code: 200, body: body
|
72
|
+
response = Response.new(http_response)
|
62
73
|
|
63
|
-
|
64
|
-
|
74
|
+
time = response.time
|
75
|
+
|
76
|
+
expect(time).to eq(0.013219)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#ok?' do
|
82
|
+
|
83
|
+
context 'when successfully analysed' do
|
65
84
|
|
85
|
+
it 'returns true' do
|
86
|
+
body = {ok: true}.to_json
|
66
87
|
http_response = ::OpenStruct.new code: 200, body: body
|
88
|
+
response = Response.new(http_response)
|
89
|
+
|
90
|
+
expect(response).to be_ok
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when unsuccessfully analysed' do
|
67
96
|
|
97
|
+
it 'returns false' do
|
98
|
+
body = {ok: false}.to_json
|
99
|
+
http_response = ::OpenStruct.new code: 200, body: body
|
68
100
|
response = Response.new(http_response)
|
69
101
|
|
102
|
+
expect(response).to_not be_ok
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#custom_annotation_output' do
|
110
|
+
|
111
|
+
it 'returns raw text' do
|
112
|
+
body = {
|
113
|
+
response: {
|
114
|
+
customAnnotationOutput: 'custom annotation output'
|
115
|
+
}
|
116
|
+
}.to_json
|
117
|
+
|
118
|
+
http_response = ::OpenStruct.new code: 200, body: body
|
119
|
+
response = Response.new(http_response)
|
120
|
+
|
121
|
+
expect(response.custom_annotation_output).to eq 'custom annotation output'
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
describe '#cleaned_text' do
|
127
|
+
|
128
|
+
it 'returns cleaned text' do
|
129
|
+
body = {
|
130
|
+
response: {
|
131
|
+
cleanedText: 'cleaned text'
|
132
|
+
}
|
133
|
+
}.to_json
|
134
|
+
|
135
|
+
http_response = ::OpenStruct.new code: 200, body: body
|
136
|
+
response = Response.new(http_response)
|
137
|
+
|
138
|
+
expect(response.cleaned_text).to eq 'cleaned text'
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#raw_text' do
|
144
|
+
|
145
|
+
it 'returns raw text' do
|
146
|
+
body = {
|
147
|
+
response:{
|
148
|
+
rawText: 'raw text'
|
149
|
+
}
|
150
|
+
}.to_json
|
151
|
+
|
152
|
+
http_response = ::OpenStruct.new code: 200, body: body
|
153
|
+
response = Response.new(http_response)
|
154
|
+
|
155
|
+
expect(response.raw_text).to eq 'raw text'
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
describe 'entailments' do
|
161
|
+
|
162
|
+
let(:http_response) { ::OpenStruct.new(code: 200, body: body) }
|
163
|
+
let(:response) { Response.new(http_response) }
|
164
|
+
|
165
|
+
context 'when response has entailments' do
|
166
|
+
|
167
|
+
let(:body) {
|
168
|
+
{
|
169
|
+
"time"=>"0.013219",
|
170
|
+
"response"=>{
|
171
|
+
"language"=>"eng",
|
172
|
+
"languageIsReliable"=>true,
|
173
|
+
"entailments"=>[{
|
174
|
+
"id"=>2, "wordPositions"=>[1],
|
175
|
+
"entailedWords"=>["misrepresentation"],
|
176
|
+
"entailedTree"=>{
|
177
|
+
"word"=>"misrepresentation", "wordId"=>0, "parentRelation"=>-1
|
178
|
+
},
|
179
|
+
"priorScore"=>0.00132419,
|
180
|
+
"contextScore"=>0.0694058,
|
181
|
+
"score"=>0.154246
|
182
|
+
}]
|
183
|
+
}
|
184
|
+
}.to_json
|
185
|
+
}
|
186
|
+
|
187
|
+
it 'returns entailments' do
|
188
|
+
entailments = response.entailments
|
189
|
+
|
190
|
+
expect(entailments).to_not be_nil
|
191
|
+
expect(entailments.size).to eq(1)
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
context 'when response does not have entailments' do
|
197
|
+
|
198
|
+
let(:body) {
|
199
|
+
{
|
200
|
+
"time"=>"0.013219",
|
201
|
+
"response"=>{
|
202
|
+
"language"=>"eng", "languageIsReliable"=>true
|
203
|
+
}
|
204
|
+
}.to_json
|
205
|
+
}
|
206
|
+
|
207
|
+
it 'returns nil' do
|
208
|
+
expect(response.entailments).to be_nil
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
describe "#topics" do
|
216
|
+
|
217
|
+
let(:http_response) do
|
218
|
+
::OpenStruct.new(code: 200, body: body)
|
219
|
+
end
|
220
|
+
|
221
|
+
let(:response) do
|
222
|
+
Response.new(http_response)
|
223
|
+
end
|
224
|
+
|
225
|
+
context "if there are topics returned from api" do
|
226
|
+
|
227
|
+
let(:body) do
|
228
|
+
{
|
229
|
+
"time" => "0.013219",
|
230
|
+
"response" => {
|
231
|
+
"language" => "eng",
|
232
|
+
"languageIsReliable" => true,
|
233
|
+
"topics" => [
|
234
|
+
{
|
235
|
+
"id" => 0,
|
236
|
+
"label" => "Airlines ",
|
237
|
+
"wikiLink" => "http://en.wikipedia.org/Category:Airlines_by_country",
|
238
|
+
"score" => 0.199069
|
239
|
+
},
|
240
|
+
{
|
241
|
+
"id" => 1,
|
242
|
+
"label" => "Companies ",
|
243
|
+
"wikiLink" => "http://en.wikipedia.org/Category:Companies_by_year_of_establishment",
|
244
|
+
"score" => 0.136068
|
245
|
+
}
|
246
|
+
]
|
247
|
+
}
|
248
|
+
}.to_json
|
249
|
+
end
|
250
|
+
|
251
|
+
it "returns topics" do
|
70
252
|
topics = response.topics
|
71
253
|
|
72
254
|
expect(topics).to_not be_nil
|
@@ -77,13 +259,17 @@ module TextRazor
|
|
77
259
|
|
78
260
|
context "if there are no topics returned from api" do
|
79
261
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
262
|
+
let(:body) do
|
263
|
+
{
|
264
|
+
"time" => "0.013219",
|
265
|
+
"response" => {
|
266
|
+
"language" => "eng",
|
267
|
+
"languageIsReliable" => true
|
268
|
+
}
|
269
|
+
}.to_json
|
270
|
+
end
|
86
271
|
|
272
|
+
it "returns nil" do
|
87
273
|
expect(response.topics).to be_nil
|
88
274
|
end
|
89
275
|
|
@@ -91,17 +277,43 @@ module TextRazor
|
|
91
277
|
|
92
278
|
end
|
93
279
|
|
94
|
-
|
280
|
+
describe "#coarse_topics" do
|
95
281
|
|
96
|
-
|
282
|
+
let(:http_response) do
|
283
|
+
::OpenStruct.new(code: 200, body: body)
|
284
|
+
end
|
97
285
|
|
98
|
-
|
99
|
-
|
286
|
+
let(:response) do
|
287
|
+
Response.new(http_response)
|
288
|
+
end
|
100
289
|
|
101
|
-
|
290
|
+
context "if there are topics returned from api" do
|
102
291
|
|
103
|
-
|
292
|
+
let(:body) do
|
293
|
+
{
|
294
|
+
"time" => "0.013219",
|
295
|
+
"response" => {
|
296
|
+
"language" => "eng",
|
297
|
+
"languageIsReliable"=>true,
|
298
|
+
"coarseTopics"=> [
|
299
|
+
{
|
300
|
+
"id"=>0,
|
301
|
+
"label" => "Airlines ",
|
302
|
+
"wikiLink" => "http://en.wikipedia.org/Category:Airlines_by_country",
|
303
|
+
"score"=>0.199069
|
304
|
+
},
|
305
|
+
{
|
306
|
+
"id"=>1,
|
307
|
+
"label" => "Companies ",
|
308
|
+
"wikiLink" => "http://en.wikipedia.org/Category:Companies_by_year_of_establishment",
|
309
|
+
"score"=>0.136068
|
310
|
+
}
|
311
|
+
]
|
312
|
+
}
|
313
|
+
}.to_json
|
314
|
+
end
|
104
315
|
|
316
|
+
it "should return topics" do
|
105
317
|
topics = response.coarse_topics
|
106
318
|
|
107
319
|
expect(topics).to_not be_nil
|
@@ -112,31 +324,95 @@ module TextRazor
|
|
112
324
|
|
113
325
|
context "if there are no topics returned from api" do
|
114
326
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
327
|
+
let(:body) do
|
328
|
+
{
|
329
|
+
"time" => "0.013219",
|
330
|
+
"response" => {
|
331
|
+
"language" => "eng",
|
332
|
+
"languageIsReliable"=>true
|
333
|
+
}
|
334
|
+
}.to_json
|
335
|
+
end
|
121
336
|
|
122
|
-
|
337
|
+
it "should return nil" do
|
338
|
+
expect(response.coarse_topics).to be_nil
|
123
339
|
end
|
124
340
|
|
125
341
|
end
|
126
342
|
|
127
343
|
end
|
128
344
|
|
129
|
-
|
345
|
+
describe "#entities" do
|
130
346
|
|
131
|
-
|
347
|
+
let(:http_response) do
|
348
|
+
::OpenStruct.new(code: 200, body: body)
|
349
|
+
end
|
132
350
|
|
133
|
-
|
134
|
-
|
351
|
+
let(:response) do
|
352
|
+
Response.new(http_response)
|
353
|
+
end
|
135
354
|
|
136
|
-
|
355
|
+
context "if there are any entities returned" do
|
137
356
|
|
138
|
-
|
357
|
+
let(:body) do
|
358
|
+
{
|
359
|
+
"time" => "0.013219",
|
360
|
+
"response"=>{
|
361
|
+
"language" => "eng",
|
362
|
+
"languageIsReliable"=>true,
|
363
|
+
"entities"=>[
|
364
|
+
{
|
365
|
+
"id"=>0,
|
366
|
+
"matchingTokens"=>[0],
|
367
|
+
"entityId" => "European Union",
|
368
|
+
"freebaseTypes" => [
|
369
|
+
"/award/award_winner",
|
370
|
+
"/book/author",
|
371
|
+
"/location/country",
|
372
|
+
"/organization/organization_scope",
|
373
|
+
"/book/book_subject",
|
374
|
+
"/location/dated_location",
|
375
|
+
"/people/ethnicity",
|
376
|
+
"/projects/project_participant",
|
377
|
+
"/location/statistical_region",
|
378
|
+
"/organization/organization",
|
379
|
+
"/organization/organization_member",
|
380
|
+
"/government/governmental_jurisdiction",
|
381
|
+
"/organization/membership_organization",
|
382
|
+
"/internet/website_category",
|
383
|
+
"/internet/website_owner",
|
384
|
+
"business/employer",
|
385
|
+
"/location/location"
|
386
|
+
],
|
387
|
+
"confidenceScore" => 1.01581,
|
388
|
+
"wikiLink" => "http://en.wikipedia.org/wiki/European_Union",
|
389
|
+
"matchedText" => "eu",
|
390
|
+
"freebaseId" => "/m/02jxk",
|
391
|
+
"relevanceScore"=>0.567223,
|
392
|
+
"entityEnglishId" => "European Union",
|
393
|
+
"startingPos"=>0,
|
394
|
+
"endingPos"=>2
|
395
|
+
},
|
396
|
+
{
|
397
|
+
"id"=>1,
|
398
|
+
"matchingTokens"=>[1, 2],
|
399
|
+
"entityId" => "Foreign minister",
|
400
|
+
"freebaseTypes"=>["government/government_office_or_title"],
|
401
|
+
"confidenceScore"=>0.897858,
|
402
|
+
"wikiLink" => "http://en.wikipedia.org/wiki/Foreign_minister",
|
403
|
+
"matchedText" => "foreign ministers",
|
404
|
+
"freebaseId" => "/m/01t_55",
|
405
|
+
"relevanceScore"=>0.311479,
|
406
|
+
"entityEnglishId" => "Foreign minister",
|
407
|
+
"startingPos"=>3,
|
408
|
+
"endingPos"=>20
|
409
|
+
}
|
410
|
+
]
|
411
|
+
}
|
412
|
+
}.to_json
|
413
|
+
end
|
139
414
|
|
415
|
+
it "should return entities" do
|
140
416
|
entities = response.entities
|
141
417
|
|
142
418
|
expect(entities).to_not be_nil
|
@@ -147,13 +423,17 @@ module TextRazor
|
|
147
423
|
|
148
424
|
context "if there are no entities returned" do
|
149
425
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
426
|
+
let(:body) do
|
427
|
+
{
|
428
|
+
"time" => "0.013219",
|
429
|
+
"response"=> {
|
430
|
+
"language" => "eng",
|
431
|
+
"languageIsReliable" => true
|
432
|
+
}
|
433
|
+
}.to_json
|
434
|
+
end
|
156
435
|
|
436
|
+
it "should return nil" do
|
157
437
|
expect(response.entities).to be_nil
|
158
438
|
end
|
159
439
|
|
@@ -161,17 +441,72 @@ module TextRazor
|
|
161
441
|
|
162
442
|
end
|
163
443
|
|
164
|
-
|
444
|
+
describe "#words" do
|
165
445
|
|
166
|
-
|
446
|
+
let(:http_response) do
|
447
|
+
::OpenStruct.new(code: 200, body: body)
|
448
|
+
end
|
167
449
|
|
168
|
-
|
169
|
-
|
450
|
+
let(:response) do
|
451
|
+
Response.new(http_response)
|
452
|
+
end
|
170
453
|
|
171
|
-
|
454
|
+
context "if there are any words returned" do
|
172
455
|
|
173
|
-
|
456
|
+
let(:body) do
|
457
|
+
{
|
458
|
+
"time" => "0.013219",
|
459
|
+
"response" => {
|
460
|
+
"language" => "eng",
|
461
|
+
"languageIsReliable" => true,
|
462
|
+
"sentences"=>[
|
463
|
+
{
|
464
|
+
"position"=>1,
|
465
|
+
"words"=>[
|
466
|
+
{
|
467
|
+
"position"=>0,
|
468
|
+
"startingPos"=>0,
|
469
|
+
"endingPos"=>3,
|
470
|
+
"stem" => "the",
|
471
|
+
"lemma" => "the",
|
472
|
+
"token" => "The",
|
473
|
+
"partOfSpeech" => "DT"
|
474
|
+
},
|
475
|
+
{
|
476
|
+
"position"=>1,
|
477
|
+
"startingPos"=>4,
|
478
|
+
"endingPos"=>7,
|
479
|
+
"stem" => "two",
|
480
|
+
"lemma" => "two",
|
481
|
+
"token" => "two",
|
482
|
+
"partOfSpeech" => "CD"
|
483
|
+
},
|
484
|
+
{
|
485
|
+
"position"=>2,
|
486
|
+
"startingPos"=>8,
|
487
|
+
"endingPos"=>11,
|
488
|
+
"stem" => "men",
|
489
|
+
"lemma" => "man",
|
490
|
+
"token" => "men",
|
491
|
+
"partOfSpeech" => "NNS"
|
492
|
+
},
|
493
|
+
{
|
494
|
+
"position"=>3,
|
495
|
+
"startingPos"=>12,
|
496
|
+
"endingPos"=>19,
|
497
|
+
"stem" => "accus",
|
498
|
+
"lemma" => "accuse",
|
499
|
+
"token" => "accused",
|
500
|
+
"partOfSpeech" => "VBN"
|
501
|
+
}
|
502
|
+
]
|
503
|
+
}
|
504
|
+
]
|
505
|
+
}
|
506
|
+
}.to_json
|
507
|
+
end
|
174
508
|
|
509
|
+
it "returns words" do
|
175
510
|
words = response.words
|
176
511
|
|
177
512
|
expect(words).to_not be_nil
|
@@ -183,16 +518,236 @@ module TextRazor
|
|
183
518
|
|
184
519
|
context "if there are no words returned" do
|
185
520
|
|
186
|
-
|
187
|
-
|
521
|
+
let(:body) do
|
522
|
+
{
|
523
|
+
"time" => "0.013219",
|
524
|
+
"response" => {
|
525
|
+
"language" => "eng",
|
526
|
+
"languageIsReliable"=>true
|
527
|
+
}
|
528
|
+
}.to_json
|
529
|
+
end
|
188
530
|
|
189
|
-
|
531
|
+
it "returns nil" do
|
532
|
+
expect(response.words).to be_nil
|
533
|
+
end
|
190
534
|
|
191
|
-
|
535
|
+
end
|
192
536
|
|
193
|
-
|
537
|
+
end
|
538
|
+
|
539
|
+
describe '#properties' do
|
540
|
+
|
541
|
+
let(:http_response) do
|
542
|
+
::OpenStruct.new(code: 200, body: body)
|
543
|
+
end
|
544
|
+
|
545
|
+
let(:response) do
|
546
|
+
Response.new(http_response)
|
547
|
+
end
|
548
|
+
|
549
|
+
context 'if there are properties' do
|
550
|
+
|
551
|
+
let(:body) do
|
552
|
+
{
|
553
|
+
"time" => "0.013219",
|
554
|
+
"response" => {
|
555
|
+
"language" => "eng",
|
556
|
+
"languageIsReliable"=>true,
|
557
|
+
"properties" => [
|
558
|
+
{
|
559
|
+
"id" => 0,
|
560
|
+
"wordPositions" => [9,10,12],
|
561
|
+
"propertyPositions" => [12]
|
562
|
+
}
|
563
|
+
]
|
564
|
+
}
|
565
|
+
}.to_json
|
194
566
|
end
|
195
567
|
|
568
|
+
it 'returns properties' do
|
569
|
+
properties = response.properties
|
570
|
+
|
571
|
+
expect(properties).to_not be_nil
|
572
|
+
expect(properties.size).to eq(1)
|
573
|
+
expect(properties.first).to be_instance_of(Property)
|
574
|
+
end
|
575
|
+
|
576
|
+
end
|
577
|
+
|
578
|
+
context 'if there are no properties returned' do
|
579
|
+
|
580
|
+
let(:body) do
|
581
|
+
{
|
582
|
+
"time" => "0.013219",
|
583
|
+
"response" => {
|
584
|
+
"language" => "eng",
|
585
|
+
"languageIsReliable"=>true
|
586
|
+
}
|
587
|
+
}.to_json
|
588
|
+
end
|
589
|
+
|
590
|
+
it 'returns nil' do
|
591
|
+
expect(response.properties).to be_nil
|
592
|
+
end
|
593
|
+
|
594
|
+
end
|
595
|
+
|
596
|
+
end
|
597
|
+
|
598
|
+
describe '#relations' do
|
599
|
+
|
600
|
+
context 'if there are relations returned' do
|
601
|
+
|
602
|
+
let(:body) do
|
603
|
+
{
|
604
|
+
"time" => "0.013219",
|
605
|
+
"response" => {
|
606
|
+
"language" => "eng",
|
607
|
+
"languageIsReliable"=>true,
|
608
|
+
"relations" => [{
|
609
|
+
id: 0,
|
610
|
+
wordPositions: [1, 6],
|
611
|
+
params: [{
|
612
|
+
relation: "SUBJECT",
|
613
|
+
wordPositions: [18, 19, 20, 21]
|
614
|
+
},
|
615
|
+
{
|
616
|
+
relation: "OBJECT",
|
617
|
+
wordPositions: [2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
|
618
|
+
}]
|
619
|
+
}]
|
620
|
+
}
|
621
|
+
}.to_json
|
622
|
+
end
|
623
|
+
|
624
|
+
it 'returns relations' do
|
625
|
+
relations = response.relations
|
626
|
+
|
627
|
+
expect(relations).to_not be_nil
|
628
|
+
expect(relations.size).to eq(1)
|
629
|
+
expect(relations.first).to be_instance_of(Relation)
|
630
|
+
end
|
631
|
+
|
632
|
+
end
|
633
|
+
|
634
|
+
context 'if there are no relations returned' do
|
635
|
+
|
636
|
+
let(:body) do
|
637
|
+
{
|
638
|
+
"time" => "0.013219",
|
639
|
+
"response" => {
|
640
|
+
"language" => "eng",
|
641
|
+
"languageIsReliable"=>true
|
642
|
+
}
|
643
|
+
}.to_json
|
644
|
+
end
|
645
|
+
|
646
|
+
it 'returns nil' do
|
647
|
+
expect(response.relations).to be_nil
|
648
|
+
end
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
describe '#sentences' do
|
653
|
+
|
654
|
+
context 'if there are sentences returned' do
|
655
|
+
|
656
|
+
let(:body) do
|
657
|
+
{
|
658
|
+
"time" => "0.013219",
|
659
|
+
"response" => {
|
660
|
+
"language" => "eng",
|
661
|
+
"languageIsReliable"=>true,
|
662
|
+
"sentences" => [{
|
663
|
+
:position=>0,
|
664
|
+
:words=>
|
665
|
+
[{
|
666
|
+
:position=>0,
|
667
|
+
:startingPos=>0,
|
668
|
+
:endingPos=>8,
|
669
|
+
:stem=>"barclay",
|
670
|
+
:lemma=>"barclay",
|
671
|
+
:token=>"Barclays",
|
672
|
+
:partOfSpeech=>"NNP",
|
673
|
+
:parentPosition=>1,
|
674
|
+
:relationToParent=>"nsubj"
|
675
|
+
}]
|
676
|
+
}]
|
677
|
+
}
|
678
|
+
}.to_json
|
679
|
+
end
|
680
|
+
|
681
|
+
it 'returns sentences' do
|
682
|
+
sentences = response.sentences
|
683
|
+
|
684
|
+
expect(sentences).to_not be_nil
|
685
|
+
expect(sentences.size).to eq(1)
|
686
|
+
expect(sentences.first).to be_instance_of(Sentence)
|
687
|
+
end
|
688
|
+
|
689
|
+
end
|
690
|
+
|
691
|
+
context 'if there are no sentences returned' do
|
692
|
+
|
693
|
+
let(:body) do
|
694
|
+
{
|
695
|
+
"time" => "0.013219",
|
696
|
+
"response" => {
|
697
|
+
"language" => "eng",
|
698
|
+
"languageIsReliable"=>true
|
699
|
+
}
|
700
|
+
}.to_json
|
701
|
+
end
|
702
|
+
|
703
|
+
it 'returns nil' do
|
704
|
+
expect(response.sentences).to be_nil
|
705
|
+
end
|
706
|
+
|
707
|
+
end
|
708
|
+
|
709
|
+
end
|
710
|
+
|
711
|
+
describe '#language' do
|
712
|
+
|
713
|
+
let(:body) do
|
714
|
+
{
|
715
|
+
"time" => "0.013219",
|
716
|
+
"response" => {
|
717
|
+
"language" => "eng",
|
718
|
+
"languageIsReliable"=>true
|
719
|
+
}
|
720
|
+
}.to_json
|
721
|
+
end
|
722
|
+
|
723
|
+
it 'returns language' do
|
724
|
+
expect(response.language).to eq 'eng'
|
725
|
+
end
|
726
|
+
|
727
|
+
end
|
728
|
+
|
729
|
+
describe '#language_is_reliable?' do
|
730
|
+
|
731
|
+
let(:body) do
|
732
|
+
{
|
733
|
+
"time" => "0.013219",
|
734
|
+
"response" => {
|
735
|
+
"language" => "eng",
|
736
|
+
"languageIsReliable"=>true
|
737
|
+
}
|
738
|
+
}.to_json
|
739
|
+
end
|
740
|
+
|
741
|
+
let(:http_response) do
|
742
|
+
::OpenStruct.new(code: 200, body: body)
|
743
|
+
end
|
744
|
+
|
745
|
+
let(:response) do
|
746
|
+
Response.new(http_response)
|
747
|
+
end
|
748
|
+
|
749
|
+
it 'returns language_is_reliable?' do
|
750
|
+
expect(response).to be_language_is_reliable
|
196
751
|
end
|
197
752
|
|
198
753
|
end
|